1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.email; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.endpoint.EndpointURI; |
12 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.transport.AbstractConnector; |
15 | |
import org.mule.util.PropertiesUtils; |
16 | |
import org.mule.util.StringUtils; |
17 | |
|
18 | |
import java.io.UnsupportedEncodingException; |
19 | |
import java.net.URLDecoder; |
20 | |
import java.util.Enumeration; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Map; |
23 | |
import java.util.Properties; |
24 | |
|
25 | |
import javax.mail.Authenticator; |
26 | |
import javax.mail.Session; |
27 | |
import javax.mail.URLName; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public abstract class AbstractMailConnector extends AbstractConnector |
34 | |
{ |
35 | |
public static final String ATTACHMENT_HEADERS_PROPERTY_POSTFIX = "Headers"; |
36 | |
public static final String MAILBOX = "INBOX"; |
37 | |
|
38 | 0 | private Map<ImmutableEndpoint, SessionDetails> sessions = new HashMap<ImmutableEndpoint, SessionDetails>(); |
39 | |
private String mailboxFolder; |
40 | |
private int defaultPort; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | private Authenticator authenticator = null; |
48 | |
|
49 | |
public AbstractMailConnector(int defaultPort, String mailboxFolder, MuleContext context) |
50 | |
{ |
51 | 0 | super(context); |
52 | 0 | this.defaultPort = defaultPort; |
53 | 0 | this.mailboxFolder = mailboxFolder; |
54 | 0 | } |
55 | |
|
56 | |
public int getDefaultPort() |
57 | |
{ |
58 | 0 | return defaultPort; |
59 | |
} |
60 | |
|
61 | |
public Authenticator getAuthenticator() |
62 | |
{ |
63 | 0 | return authenticator; |
64 | |
} |
65 | |
|
66 | |
public void setAuthenticator(Authenticator authenticator) |
67 | |
{ |
68 | 0 | this.authenticator = authenticator; |
69 | 0 | } |
70 | |
|
71 | |
public String getMailboxFolder() |
72 | |
{ |
73 | 0 | return mailboxFolder; |
74 | |
} |
75 | |
|
76 | |
public void setMailboxFolder(String mailboxFolder) |
77 | |
{ |
78 | 0 | this.mailboxFolder = mailboxFolder; |
79 | 0 | } |
80 | |
|
81 | |
public SessionDetails getSessionDetails(ImmutableEndpoint endpoint) throws UnsupportedEncodingException |
82 | |
{ |
83 | |
|
84 | |
|
85 | 0 | synchronized (sessions) |
86 | |
{ |
87 | 0 | SessionDetails sessionDetails = sessions.get(endpoint); |
88 | 0 | if (null == sessionDetails) |
89 | |
{ |
90 | 0 | sessionDetails = newSession(endpoint); |
91 | 0 | sessions.put(endpoint, sessionDetails); |
92 | |
} |
93 | 0 | return sessionDetails; |
94 | 0 | } |
95 | |
} |
96 | |
|
97 | |
public URLName urlFromEndpoint(ImmutableEndpoint endpoint) throws UnsupportedEncodingException |
98 | |
{ |
99 | 0 | String inbox = endpoint.getEndpointURI().getPath(); |
100 | 0 | if (inbox.length() == 0) |
101 | |
{ |
102 | 0 | inbox = getMailboxFolder(); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | inbox = inbox.substring(1); |
107 | |
} |
108 | |
|
109 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
110 | 0 | String user = uri.getUser(); |
111 | 0 | if (user != null) |
112 | |
{ |
113 | 0 | user = URLDecoder.decode(user, endpoint.getEncoding()); |
114 | |
} |
115 | 0 | String pass = uri.getPassword(); |
116 | 0 | if (pass != null) |
117 | |
{ |
118 | 0 | pass = URLDecoder.decode(pass, endpoint.getEncoding()); |
119 | |
} |
120 | |
|
121 | 0 | return new URLName(uri.getScheme(), uri.getHost(), uri.getPort(), inbox, user, pass); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
protected String getBaseProtocol() |
131 | |
{ |
132 | 0 | return getProtocol(); |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
protected void extendPropertiesForSession(Properties global, Properties local, URLName url) |
144 | |
{ |
145 | 0 | int port = url.getPort(); |
146 | 0 | if (port == -1) |
147 | |
{ |
148 | 0 | port = this.getDefaultPort(); |
149 | |
} |
150 | 0 | local.setProperty("mail." + getBaseProtocol() + ".socketFactory.port", Integer.toString(port)); |
151 | |
|
152 | 0 | if (StringUtils.isNotBlank(url.getPassword())) |
153 | |
{ |
154 | 0 | local.setProperty("mail." + getBaseProtocol() + ".auth", "true"); |
155 | 0 | if (getAuthenticator() == null) |
156 | |
{ |
157 | 0 | setAuthenticator(new DefaultAuthenticator(url.getUsername(), url.getPassword())); |
158 | 0 | if (logger.isDebugEnabled()) |
159 | |
{ |
160 | 0 | logger.debug("No Authenticator set on connector: " + getName() + "; using default."); |
161 | |
} |
162 | |
} |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | 0 | local.setProperty("mail." + getBaseProtocol() + ".auth", "false"); |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | 0 | if (StringUtils.isNotBlank(url.getHost())) { |
174 | 0 | local.setProperty("mail." + getBaseProtocol() + ".host", url.getHost()); |
175 | |
} |
176 | 0 | local.setProperty("mail." + getBaseProtocol() + ".rsetbeforequit", "true"); |
177 | 0 | } |
178 | |
|
179 | |
protected SessionDetails newSession(ImmutableEndpoint endpoint) throws UnsupportedEncodingException |
180 | |
{ |
181 | 0 | URLName url = urlFromEndpoint(endpoint); |
182 | |
|
183 | 0 | Properties global = System.getProperties(); |
184 | 0 | Properties local = new Properties(); |
185 | |
|
186 | 0 | PropertiesUtils.getPropertiesWithPrefix(endpoint.getProperties(), "mail.", local); |
187 | |
Session session; |
188 | |
|
189 | |
|
190 | 0 | synchronized (global) |
191 | |
{ |
192 | 0 | extendPropertiesForSession(global, local, url); |
193 | 0 | session = Session.getInstance(local, getAuthenticator()); |
194 | 0 | } |
195 | |
|
196 | 0 | if (logger.isDebugEnabled()) |
197 | |
{ |
198 | 0 | local.setProperty("mail.debug", "true"); |
199 | |
|
200 | 0 | dumpProperties("MuleSession local properties", local, true); |
201 | 0 | dumpProperties("System global properties", global, true); |
202 | 0 | logger.debug("Creating mail session: host = " + url.getHost() + ", port = " + url.getPort() |
203 | |
+ ", user = " + url.getUsername() + ", pass = " + url.getPassword()); |
204 | |
} |
205 | |
|
206 | 0 | return new SessionDetails(session, url); |
207 | |
} |
208 | |
|
209 | |
protected void dumpProperties(String title, Properties properties, boolean filter) |
210 | |
{ |
211 | 0 | int skipped = 0; |
212 | 0 | logger.debug(title + " ============="); |
213 | 0 | Enumeration keys = properties.keys(); |
214 | 0 | while (keys.hasMoreElements()) |
215 | |
{ |
216 | 0 | String key = (String) keys.nextElement(); |
217 | 0 | if (!filter || key.startsWith("mule.") || key.startsWith("mail.") || key.startsWith("javax.")) |
218 | |
{ |
219 | 0 | String value = properties.getProperty(key); |
220 | 0 | logger.debug(key + ": " + value); |
221 | 0 | } |
222 | |
else |
223 | |
{ |
224 | 0 | ++skipped; |
225 | |
} |
226 | 0 | } |
227 | 0 | if (filter) |
228 | |
{ |
229 | 0 | logger.debug("skipped " + skipped); |
230 | |
} |
231 | 0 | } |
232 | |
|
233 | |
|
234 | |
|
235 | |
@Override |
236 | |
protected void doInitialise() throws InitialisationException |
237 | |
{ |
238 | |
|
239 | 0 | } |
240 | |
|
241 | |
@Override |
242 | |
protected void doDispose() |
243 | |
{ |
244 | |
|
245 | 0 | } |
246 | |
|
247 | |
@Override |
248 | |
protected void doConnect() throws Exception |
249 | |
{ |
250 | |
|
251 | 0 | } |
252 | |
|
253 | |
@Override |
254 | |
protected void doDisconnect() throws Exception |
255 | |
{ |
256 | |
|
257 | 0 | } |
258 | |
|
259 | |
@Override |
260 | |
protected void doStart() throws MuleException |
261 | |
{ |
262 | |
|
263 | 0 | } |
264 | |
|
265 | |
@Override |
266 | |
protected void doStop() throws MuleException |
267 | |
{ |
268 | |
|
269 | 0 | } |
270 | |
|
271 | |
} |