1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.email; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.ThreadSafeAccess; |
15 | |
import org.mule.providers.AbstractMessageAdapter; |
16 | |
import org.mule.umo.MessagingException; |
17 | |
import org.mule.umo.provider.MessageTypeNotSupportedException; |
18 | |
import org.mule.util.IOUtils; |
19 | |
import org.mule.util.StringUtils; |
20 | |
import org.mule.util.SystemUtils; |
21 | |
|
22 | |
import java.io.BufferedInputStream; |
23 | |
import java.io.BufferedReader; |
24 | |
import java.io.ByteArrayOutputStream; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.util.Date; |
28 | |
import java.util.Enumeration; |
29 | |
import java.util.LinkedList; |
30 | |
import java.util.List; |
31 | |
|
32 | |
import javax.mail.Header; |
33 | |
import javax.mail.Message; |
34 | |
import javax.mail.Part; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public class SimpleMailMessageAdapter extends AbstractMessageAdapter |
55 | |
{ |
56 | |
|
57 | |
private static final long serialVersionUID = 8002607243523460556L; |
58 | |
public static final String HEADER_LIST_PREFIX = "List:"; |
59 | |
private Part message; |
60 | 0 | private byte[] cache = null; |
61 | |
|
62 | |
public SimpleMailMessageAdapter(Object object) throws MessagingException |
63 | 0 | { |
64 | 0 | Message message = assertMessageType(object); |
65 | |
|
66 | |
try |
67 | |
{ |
68 | 0 | setMessageDetails(message); |
69 | 0 | handleMessage(message); |
70 | |
} |
71 | 0 | catch (Exception e) |
72 | |
{ |
73 | 0 | throw new MessagingException(CoreMessages.failedToCreate("Message Adapter"), e); |
74 | 0 | } |
75 | 0 | } |
76 | |
|
77 | |
protected SimpleMailMessageAdapter(SimpleMailMessageAdapter template) |
78 | |
{ |
79 | 0 | super(template); |
80 | 0 | message = template.message; |
81 | 0 | cache = template.cache; |
82 | 0 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
protected void handleMessage(Message message) throws Exception |
89 | |
{ |
90 | 0 | setMessage(message); |
91 | 0 | } |
92 | |
|
93 | |
protected void setMessage(Part message) |
94 | |
{ |
95 | 0 | this.message = message; |
96 | 0 | } |
97 | |
|
98 | |
public Object getPayload() { |
99 | 0 | return message; |
100 | |
} |
101 | |
|
102 | |
public byte[] getPayloadAsBytes() throws Exception |
103 | |
{ |
104 | 0 | return buildCache(getEncoding()); |
105 | |
} |
106 | |
|
107 | |
public String getPayloadAsString(String encoding) throws Exception |
108 | |
{ |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | 0 | return new String(buildCache(encoding), encoding); |
115 | |
} |
116 | |
|
117 | |
private byte[] buildCache(String encoding) throws Exception |
118 | |
{ |
119 | 0 | if (null == cache) |
120 | |
{ |
121 | 0 | if (message.getContentType().startsWith("text/")) |
122 | |
{ |
123 | 0 | cache = textPayload(encoding); |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | cache = binaryPayload(); |
128 | |
} |
129 | |
} |
130 | 0 | return cache; |
131 | |
} |
132 | |
|
133 | |
private static Message assertMessageType(Object message) throws MessageTypeNotSupportedException |
134 | |
{ |
135 | 0 | if (message instanceof Message) |
136 | |
{ |
137 | 0 | return (Message)message; |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | 0 | throw new MessageTypeNotSupportedException(message, MailMessageAdapter.class); |
142 | |
} |
143 | |
} |
144 | |
|
145 | |
private void setMessageDetails(Message message) throws javax.mail.MessagingException |
146 | |
{ |
147 | 0 | setProperty(MailProperties.TO_ADDRESSES_PROPERTY, |
148 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))); |
149 | 0 | setProperty(MailProperties.CC_ADDRESSES_PROPERTY, |
150 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))); |
151 | 0 | setProperty(MailProperties.BCC_ADDRESSES_PROPERTY, |
152 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC))); |
153 | 0 | setProperty(MailProperties.REPLY_TO_ADDRESSES_PROPERTY, |
154 | |
MailUtils.mailAddressesToString(message.getReplyTo())); |
155 | 0 | setProperty(MailProperties.FROM_ADDRESS_PROPERTY, |
156 | |
MailUtils.mailAddressesToString(message.getFrom())); |
157 | 0 | setProperty(MailProperties.SUBJECT_PROPERTY, |
158 | |
StringUtils.defaultIfEmpty(message.getSubject(), "(no subject)")); |
159 | 0 | setProperty(MailProperties.CONTENT_TYPE_PROPERTY, |
160 | |
StringUtils.defaultIfEmpty(message.getContentType(), "text/plain")); |
161 | |
|
162 | 0 | Date sentDate = message.getSentDate(); |
163 | 0 | if (sentDate == null) |
164 | |
{ |
165 | 0 | sentDate = new Date(); |
166 | |
} |
167 | 0 | setProperty(MailProperties.SENT_DATE_PROPERTY, sentDate); |
168 | |
|
169 | 0 | for (Enumeration e = message.getAllHeaders(); e.hasMoreElements();) |
170 | |
{ |
171 | 0 | Header header = (Header)e.nextElement(); |
172 | 0 | String name = header.getName(); |
173 | 0 | String listName = toListHeader(name); |
174 | 0 | String value = header.getValue(); |
175 | |
|
176 | 0 | if (null == getProperty(name)) |
177 | |
{ |
178 | 0 | setProperty(name, value); |
179 | |
} |
180 | |
|
181 | 0 | if (null == getProperty(listName)) |
182 | |
{ |
183 | 0 | setProperty(listName, new LinkedList()); |
184 | |
} |
185 | 0 | if (getProperty(listName) instanceof List) |
186 | |
{ |
187 | 0 | ((List) getProperty(listName)).add(header.getValue()); |
188 | |
} |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static boolean isListHeader(String name) |
201 | |
{ |
202 | 0 | return null != name && name.startsWith(HEADER_LIST_PREFIX); |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public static String toHeader(String name) |
212 | |
{ |
213 | 0 | if (isListHeader(name)) |
214 | |
{ |
215 | 0 | return name.substring(HEADER_LIST_PREFIX.length()); |
216 | |
} |
217 | |
else |
218 | |
{ |
219 | 0 | return name; |
220 | |
} |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public static String toListHeader(String header) |
230 | |
{ |
231 | 0 | if (isListHeader(header)) |
232 | |
{ |
233 | 0 | return header; |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | return HEADER_LIST_PREFIX + header; |
238 | |
} |
239 | |
} |
240 | |
|
241 | |
private static InputStream addBuffer(InputStream stream) |
242 | |
{ |
243 | 0 | if (!(stream instanceof BufferedInputStream)) |
244 | |
{ |
245 | 0 | stream = new BufferedInputStream(stream); |
246 | |
} |
247 | 0 | return stream; |
248 | |
} |
249 | |
|
250 | |
private byte[] binaryPayload() throws Exception |
251 | |
{ |
252 | 0 | InputStream stream = addBuffer(message.getInputStream()); |
253 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); |
254 | 0 | IOUtils.copy(stream, baos); |
255 | 0 | return baos.toByteArray(); |
256 | |
} |
257 | |
|
258 | |
private byte[] textPayload(String encoding) throws Exception |
259 | |
{ |
260 | 0 | InputStream stream = addBuffer(message.getInputStream()); |
261 | 0 | BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); |
262 | 0 | StringBuffer buffer = new StringBuffer(32768); |
263 | |
|
264 | |
String line; |
265 | 0 | while ((line = reader.readLine()) != null) |
266 | |
{ |
267 | 0 | buffer.append(line).append(SystemUtils.LINE_SEPARATOR); |
268 | |
} |
269 | |
|
270 | 0 | return buffer.toString().getBytes(encoding); |
271 | |
} |
272 | |
|
273 | |
public ThreadSafeAccess newThreadCopy() |
274 | |
{ |
275 | 0 | return new SimpleMailMessageAdapter(this); |
276 | |
} |
277 | |
|
278 | |
} |