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