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