1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.email.transformers; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.transformer.simple.SerializableToByteArray; |
16 | |
import org.mule.transport.email.MailMessageAdapter; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.Serializable; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.activation.DataHandler; |
25 | |
import javax.activation.DataSource; |
26 | |
import javax.activation.FileDataSource; |
27 | |
import javax.mail.BodyPart; |
28 | |
import javax.mail.Message; |
29 | |
import javax.mail.MessagingException; |
30 | |
import javax.mail.internet.MimeBodyPart; |
31 | |
import javax.mail.internet.MimeMultipart; |
32 | |
import javax.mail.util.ByteArrayDataSource; |
33 | |
|
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 16 | public class ObjectToMimeMessage extends StringToEmailMessage |
41 | |
{ |
42 | |
|
43 | 16 | private Log logger = LogFactory.getLog(getClass()); |
44 | |
|
45 | |
|
46 | |
protected void setContent(Object payload, Message msg, String contentType, MuleMessage message) |
47 | |
throws Exception |
48 | |
{ |
49 | |
|
50 | 8 | if (message.getAttachmentNames().size() > 0) |
51 | |
{ |
52 | |
|
53 | 0 | MimeMultipart multipart = new MimeMultipart("mixed"); |
54 | 0 | multipart.addBodyPart(getPayloadBodyPart(message.getPayload(), contentType)); |
55 | 0 | for (Iterator it = message.getAttachmentNames().iterator(); it.hasNext();) |
56 | |
{ |
57 | 0 | String name = (String)it.next(); |
58 | 0 | BodyPart part = getBodyPartForAttachment(message.getAttachment(name), name); |
59 | |
|
60 | 0 | addBodyPartHeaders(part, name, message); |
61 | 0 | multipart.addBodyPart(part); |
62 | 0 | } |
63 | |
|
64 | 0 | payload = multipart; |
65 | |
|
66 | 0 | contentType = multipart.getContentType(); |
67 | |
|
68 | |
} |
69 | |
|
70 | |
|
71 | 8 | super.setContent(payload, msg, contentType, message); |
72 | 8 | } |
73 | |
|
74 | |
protected void addBodyPartHeaders(BodyPart part, String name, MuleMessage message) |
75 | |
{ |
76 | 0 | Map headers = (Map)message.getProperty( |
77 | |
name + MailMessageAdapter.ATTACHMENT_HEADERS_PROPERTY_POSTFIX); |
78 | |
|
79 | 0 | if (null != headers) |
80 | |
{ |
81 | 0 | for (Iterator it = headers.keySet().iterator(); it.hasNext();) |
82 | |
{ |
83 | |
try |
84 | |
{ |
85 | 0 | String key = (String)it.next(); |
86 | 0 | part.setHeader(key, (String)headers.get(key)); |
87 | |
} |
88 | 0 | catch (MessagingException me) |
89 | |
{ |
90 | 0 | logger.error("Failed to set bodypart header", me); |
91 | 0 | } |
92 | |
} |
93 | |
} |
94 | 0 | } |
95 | |
|
96 | |
protected BodyPart getBodyPartForAttachment(DataHandler handler, String name) throws MessagingException |
97 | |
{ |
98 | 0 | BodyPart part = new MimeBodyPart(); |
99 | 0 | part.setDataHandler(handler); |
100 | 0 | part.setDescription(name); |
101 | |
|
102 | 0 | DataSource source = handler.getDataSource(); |
103 | |
|
104 | |
|
105 | 0 | if (source instanceof FileDataSource) |
106 | |
{ |
107 | 0 | part.setFileName(StringUtils.defaultString(handler.getName(), name)); |
108 | |
} |
109 | 0 | return part; |
110 | |
} |
111 | |
|
112 | |
protected BodyPart getPayloadBodyPart(Object payload, String contentType) |
113 | |
throws MessagingException, TransformerException, IOException |
114 | |
{ |
115 | |
|
116 | |
DataHandler handler; |
117 | 0 | if (payload instanceof String) |
118 | |
{ |
119 | 0 | handler = new DataHandler(new ByteArrayDataSource((String) payload, contentType)); |
120 | |
} |
121 | 0 | else if (payload instanceof byte[]) |
122 | |
{ |
123 | 0 | handler = new DataHandler(new ByteArrayDataSource((byte[])payload, contentType)); |
124 | |
} |
125 | 0 | else if (payload instanceof Serializable) |
126 | |
{ |
127 | 0 | handler = new DataHandler(new ByteArrayDataSource( |
128 | |
(byte[])new SerializableToByteArray().transform(payload), contentType)); |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | throw new IllegalArgumentException(); |
133 | |
} |
134 | 0 | BodyPart part = new MimeBodyPart(); |
135 | 0 | part.setDataHandler(handler); |
136 | 0 | part.setDescription("Payload"); |
137 | 0 | return part; |
138 | |
} |
139 | |
|
140 | |
} |