1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.core.mail; |
18 | |
|
19 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
20 | |
import org.springframework.beans.factory.config.AbstractFactoryBean; |
21 | |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
22 | |
|
23 | |
import javax.mail.Authenticator; |
24 | |
import javax.mail.PasswordAuthentication; |
25 | |
import javax.mail.Session; |
26 | |
import java.util.Properties; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
@SuppressWarnings("unchecked") |
36 | 0 | public class MailSenderFactoryBean extends AbstractFactoryBean { |
37 | |
|
38 | 0 | protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(MailSenderFactoryBean.class); |
39 | |
|
40 | |
private static final String MAIL_PREFIX = "mail"; |
41 | |
private static final String USERNAME_PROPERTY = "mail.smtp.username"; |
42 | |
private static final String PASSWORD_PROPERTY = "mail.smtp.password"; |
43 | |
private static final String HOST_PROPERTY = "mail.smtp.host"; |
44 | |
private static final String PORT_PROPERTY = "mail.smtp.port"; |
45 | |
|
46 | |
private Session mailSession; |
47 | |
|
48 | |
@Override |
49 | |
protected Object createInstance() throws Exception { |
50 | |
|
51 | 0 | Properties properties = new Properties(); |
52 | 0 | Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); |
53 | 0 | LOG.debug("createInstance(): collecting mail properties."); |
54 | 0 | for (Object keyObj : configProps.keySet()) { |
55 | 0 | if (keyObj instanceof String) { |
56 | 0 | String key = (String)keyObj; |
57 | 0 | if (key.startsWith(MAIL_PREFIX)){ |
58 | 0 | properties.put(key, configProps.get(key)); |
59 | |
} |
60 | 0 | } |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | String username = properties.getProperty(USERNAME_PROPERTY); |
66 | 0 | String password = properties.getProperty(PASSWORD_PROPERTY); |
67 | 0 | if (username != null && password != null) { |
68 | 0 | mailSession = Session.getInstance(properties, new SimpleAuthenticator(username, password)); |
69 | 0 | LOG.info("createInstance(): Initializing mail session using SMTP authentication."); |
70 | |
} else { |
71 | 0 | mailSession = Session.getInstance(properties); |
72 | 0 | LOG.info("createInstance(): Initializing mail session. No SMTP authentication."); |
73 | |
} |
74 | |
|
75 | |
|
76 | 0 | JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); |
77 | 0 | LOG.debug("createInstance(): setting SMTP host."); |
78 | 0 | mailSender.setHost(properties.getProperty(HOST_PROPERTY)); |
79 | 0 | if (properties.getProperty(PORT_PROPERTY) != null) { |
80 | 0 | LOG.debug("createInstance(): setting SMTP port."); |
81 | 0 | int smtpPort = Integer.parseInt(properties.getProperty(PORT_PROPERTY).trim()); |
82 | 0 | mailSender.setPort(smtpPort); |
83 | |
} |
84 | 0 | mailSender.setSession(mailSession); |
85 | |
|
86 | 0 | LOG.info("createInstance(): Mail Sender Factory Bean initialized."); |
87 | 0 | return mailSender; |
88 | |
} |
89 | |
|
90 | 0 | private class SimpleAuthenticator extends Authenticator { |
91 | |
|
92 | |
private final PasswordAuthentication passwordAuthentication; |
93 | |
|
94 | 0 | private SimpleAuthenticator(String username, String password) { |
95 | 0 | this.passwordAuthentication = new PasswordAuthentication(username, password); |
96 | 0 | } |
97 | |
|
98 | |
public PasswordAuthentication getPasswordAuthentication() { |
99 | 0 | return passwordAuthentication; |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
@SuppressWarnings("unchecked") |
104 | |
@Override |
105 | |
public Class getObjectType() { |
106 | 0 | return JavaMailSenderImpl.class; |
107 | |
} |
108 | |
|
109 | |
} |