1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.email; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.transport.AbstractMuleMessageFactory; |
13 | |
import org.mule.util.StringUtils; |
14 | |
|
15 | |
import java.util.Date; |
16 | |
import java.util.Enumeration; |
17 | |
import java.util.HashMap; |
18 | |
import java.util.LinkedList; |
19 | |
import java.util.List; |
20 | |
import java.util.Map; |
21 | |
import java.util.TreeMap; |
22 | |
|
23 | |
import javax.mail.Address; |
24 | |
import javax.mail.Header; |
25 | |
import javax.mail.Message; |
26 | |
import javax.mail.Message.RecipientType; |
27 | |
import javax.mail.MessagingException; |
28 | |
import javax.mail.Multipart; |
29 | |
import javax.mail.Part; |
30 | |
import javax.mail.internet.MimeMessage; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
|
35 | |
public class MailMuleMessageFactory extends AbstractMuleMessageFactory |
36 | |
{ |
37 | |
public static final String HEADER_LIST_PREFIX = "List:"; |
38 | |
|
39 | 0 | private static Log log = LogFactory.getLog(MailMuleMessageFactory.class); |
40 | |
|
41 | |
public MailMuleMessageFactory(MuleContext context) |
42 | |
{ |
43 | 0 | super(context); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
protected Class<?>[] getSupportedTransportMessageTypes() |
48 | |
{ |
49 | 0 | return new Class[]{Message.class}; |
50 | |
} |
51 | |
|
52 | |
@Override |
53 | |
protected Object extractPayload(Object transportMessage, String encoding) throws Exception |
54 | |
{ |
55 | 0 | return transportMessage; |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
protected void addProperties(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception |
60 | |
{ |
61 | 0 | super.addProperties(muleMessage, transportMessage); |
62 | |
|
63 | 0 | Message mailMessage = (Message) transportMessage; |
64 | |
|
65 | 0 | addRecipientProperty(muleMessage, mailMessage, RecipientType.TO, MailProperties.INBOUND_TO_ADDRESSES_PROPERTY); |
66 | 0 | addRecipientProperty(muleMessage, mailMessage, RecipientType.CC, MailProperties.INBOUND_CC_ADDRESSES_PROPERTY); |
67 | 0 | addRecipientProperty(muleMessage, mailMessage, RecipientType.BCC, MailProperties.INBOUND_BCC_ADDRESSES_PROPERTY); |
68 | |
|
69 | 0 | addReplyToProperty(muleMessage, mailMessage); |
70 | 0 | addFromProperty(muleMessage, mailMessage); |
71 | |
|
72 | 0 | muleMessage.setOutboundProperty(MailProperties.INBOUND_SUBJECT_PROPERTY, |
73 | |
StringUtils.defaultIfEmpty(mailMessage.getSubject(), "(no subject)")); |
74 | 0 | muleMessage.setOutboundProperty(MailProperties.INBOUND_CONTENT_TYPE_PROPERTY, |
75 | |
StringUtils.defaultIfEmpty(mailMessage.getContentType(), "text/plain")); |
76 | |
|
77 | 0 | addSentDateProperty(muleMessage, mailMessage); |
78 | 0 | addMailHeadersToMessageProperties(mailMessage, muleMessage); |
79 | 0 | } |
80 | |
|
81 | |
protected void addRecipientProperty(MuleMessage muleMessage, Message mailMessage, |
82 | |
RecipientType recipientType, String property) throws MessagingException |
83 | |
{ |
84 | 0 | MimeMessage mimeMessage = null; |
85 | 0 | if (mailMessage instanceof MimeMessage) |
86 | |
{ |
87 | 0 | mimeMessage = (MimeMessage) mailMessage; |
88 | |
} |
89 | |
|
90 | |
try |
91 | |
{ |
92 | 0 | Address[] recipients = mailMessage.getRecipients(recipientType); |
93 | 0 | muleMessage.setOutboundProperty(property, MailUtils.mailAddressesToString(recipients)); |
94 | |
} |
95 | 0 | catch (MessagingException e) |
96 | |
{ |
97 | 0 | if (mimeMessage != null) |
98 | |
{ |
99 | 0 | String[] header = mimeMessage.getHeader(recipientType.toString()); |
100 | 0 | String recipients = StringUtils.join(header, ", "); |
101 | 0 | muleMessage.setOutboundProperty(property, recipients); |
102 | |
} |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
protected void addReplyToProperty(DefaultMuleMessage muleMessage, Message mailMessage) |
107 | |
{ |
108 | |
try |
109 | |
{ |
110 | 0 | muleMessage.setOutboundProperty(MailProperties.INBOUND_REPLY_TO_ADDRESSES_PROPERTY, |
111 | |
MailUtils.mailAddressesToString(mailMessage.getReplyTo())); |
112 | |
} |
113 | 0 | catch (MessagingException me) |
114 | |
{ |
115 | 0 | log.warn("Invalid address found in ReplyTo header:", me); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | |
protected void addFromProperty(DefaultMuleMessage muleMessage, Message mailMessage) |
120 | |
{ |
121 | |
try |
122 | |
{ |
123 | 0 | muleMessage.setOutboundProperty(MailProperties.INBOUND_FROM_ADDRESS_PROPERTY, |
124 | |
MailUtils.mailAddressesToString(mailMessage.getFrom())); |
125 | |
} |
126 | 0 | catch (javax.mail.MessagingException me) |
127 | |
{ |
128 | 0 | log.warn("Invalid address found in From header:", me); |
129 | 0 | } |
130 | 0 | } |
131 | |
|
132 | |
protected void addSentDateProperty(DefaultMuleMessage muleMessage, Message mailMessage) |
133 | |
throws MessagingException |
134 | |
{ |
135 | 0 | Date sentDate = mailMessage.getSentDate(); |
136 | 0 | if (sentDate == null) |
137 | |
{ |
138 | 0 | sentDate = new Date(); |
139 | |
} |
140 | 0 | muleMessage.setOutboundProperty(MailProperties.SENT_DATE_PROPERTY, sentDate); |
141 | 0 | } |
142 | |
|
143 | |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
144 | |
protected void addMailHeadersToMessageProperties(Message mailMessage, DefaultMuleMessage muleMessage) |
145 | |
throws MessagingException |
146 | |
{ |
147 | 0 | for (Enumeration<?> e = mailMessage.getAllHeaders(); e.hasMoreElements();) |
148 | |
{ |
149 | 0 | Header header = (Header) e.nextElement(); |
150 | |
|
151 | 0 | String name = header.getName(); |
152 | 0 | String listName = MailUtils.toListHeader(name); |
153 | 0 | String value = header.getValue(); |
154 | |
|
155 | 0 | if (null == muleMessage.getOutboundProperty(name)) |
156 | |
{ |
157 | 0 | muleMessage.setOutboundProperty(name, value); |
158 | |
} |
159 | |
|
160 | 0 | Object listPropertyValue = muleMessage.getOutboundProperty(listName); |
161 | 0 | if (null == listPropertyValue) |
162 | |
{ |
163 | 0 | listPropertyValue = new LinkedList<Object>(); |
164 | 0 | muleMessage.setOutboundProperty(listName, listPropertyValue); |
165 | |
} |
166 | 0 | if (listPropertyValue instanceof List<?>) |
167 | |
{ |
168 | 0 | ((List) listPropertyValue).add(header.getValue()); |
169 | |
} |
170 | 0 | } |
171 | 0 | } |
172 | |
|
173 | |
@Override |
174 | |
protected void addAttachments(DefaultMuleMessage muleMessage, Object transportMessage) throws Exception |
175 | |
{ |
176 | 0 | super.addAttachments(muleMessage, transportMessage); |
177 | |
|
178 | 0 | Object content = ((Message) transportMessage).getContent(); |
179 | 0 | if (content instanceof Multipart) |
180 | |
{ |
181 | 0 | Multipart multipart = (Multipart) content; |
182 | |
|
183 | 0 | TreeMap<String, Part> attachments = new TreeMap<String, Part>(); |
184 | 0 | MailUtils.getAttachments(multipart, attachments); |
185 | |
|
186 | 0 | log.debug("Received Multipart message. Adding attachments"); |
187 | 0 | for (Map.Entry<String, Part> entry : attachments.entrySet()) |
188 | |
{ |
189 | 0 | Part part = entry.getValue(); |
190 | 0 | String name = entry.getKey(); |
191 | |
|
192 | 0 | muleMessage.addInboundAttachment(name, part.getDataHandler()); |
193 | 0 | addAttachmentHeaders(name, part, muleMessage); |
194 | 0 | } |
195 | |
} |
196 | 0 | } |
197 | |
|
198 | |
protected void addAttachmentHeaders(String name, Part part, MuleMessage muleMessage) throws javax.mail.MessagingException |
199 | |
{ |
200 | 0 | Map<String, String> headers = new HashMap<String, String>(); |
201 | 0 | for (Enumeration<?> e = part.getAllHeaders(); e.hasMoreElements();) |
202 | |
{ |
203 | 0 | Header h = (Header) e.nextElement(); |
204 | 0 | headers.put(h.getName(), h.getValue()); |
205 | 0 | } |
206 | |
|
207 | 0 | if (headers.size() > 0) |
208 | |
{ |
209 | 0 | muleMessage.setOutboundProperty(name + AbstractMailConnector.ATTACHMENT_HEADERS_PROPERTY_POSTFIX, |
210 | |
headers); |
211 | |
} |
212 | 0 | } |
213 | |
} |