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.AbstractMailConnector; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.Serializable; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.activation.DataHandler; |
24 | |
import javax.mail.BodyPart; |
25 | |
import javax.mail.Message; |
26 | |
import javax.mail.MessagingException; |
27 | |
import javax.mail.internet.MimeBodyPart; |
28 | |
import javax.mail.internet.MimeMultipart; |
29 | |
import javax.mail.util.ByteArrayDataSource; |
30 | |
|
31 | |
import org.apache.commons.logging.Log; |
32 | |
import org.apache.commons.logging.LogFactory; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class ObjectToMimeMessage extends StringToEmailMessage |
38 | |
{ |
39 | 0 | private Log logger = LogFactory.getLog(getClass()); |
40 | 0 | private boolean useInboundAttachments = true; |
41 | 0 | private boolean useOutboundAttachments = true; |
42 | |
|
43 | |
@Override |
44 | |
protected void setContent(Object payload, Message msg, String contentType, MuleMessage message) |
45 | |
throws Exception |
46 | |
{ |
47 | 0 | boolean transformInboundAttachments = useInboundAttachments && message.getInboundAttachmentNames().size() > 0; |
48 | 0 | boolean transformOutboundAttachments = useOutboundAttachments && message.getOutboundAttachmentNames().size() > 0; |
49 | 0 | if (transformInboundAttachments || transformOutboundAttachments) |
50 | |
{ |
51 | |
|
52 | 0 | MimeMultipart multipart = new MimeMultipart("mixed"); |
53 | 0 | multipart.addBodyPart(getPayloadBodyPart(message.getPayload(), contentType)); |
54 | 0 | if (transformInboundAttachments) |
55 | |
{ |
56 | 0 | for (String name : message.getInboundAttachmentNames()) |
57 | |
{ |
58 | |
|
59 | 0 | if (!transformOutboundAttachments || message.getOutboundAttachment(name) == null) |
60 | |
{ |
61 | 0 | BodyPart part = getBodyPartForAttachment(message.getInboundAttachment(name), name); |
62 | |
|
63 | 0 | addBodyPartHeaders(part, name, message); |
64 | 0 | multipart.addBodyPart(part); |
65 | 0 | } |
66 | |
} |
67 | |
} |
68 | 0 | if (transformOutboundAttachments) |
69 | |
{ |
70 | 0 | for (String name : message.getOutboundAttachmentNames()) |
71 | |
{ |
72 | 0 | BodyPart part = getBodyPartForAttachment(message.getOutboundAttachment(name), name); |
73 | |
|
74 | 0 | addBodyPartHeaders(part, name, message); |
75 | 0 | multipart.addBodyPart(part); |
76 | 0 | } |
77 | |
} |
78 | |
|
79 | 0 | payload = multipart; |
80 | |
|
81 | 0 | contentType = multipart.getContentType(); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | 0 | super.setContent(payload, msg, contentType, message); |
87 | 0 | } |
88 | |
|
89 | |
protected void addBodyPartHeaders(BodyPart part, String name, MuleMessage message) |
90 | |
{ |
91 | 0 | Map<String, String> headers = message.getOutboundProperty( |
92 | |
name + AbstractMailConnector.ATTACHMENT_HEADERS_PROPERTY_POSTFIX); |
93 | |
|
94 | 0 | if (null != headers) |
95 | |
{ |
96 | 0 | for (String key : headers.keySet()) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | part.setHeader(key, headers.get(key)); |
101 | |
} |
102 | 0 | catch (MessagingException me) |
103 | |
{ |
104 | 0 | logger.error("Failed to set bodypart header", me); |
105 | 0 | } |
106 | |
} |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
protected BodyPart getBodyPartForAttachment(DataHandler handler, String name) throws MessagingException |
111 | |
{ |
112 | 0 | BodyPart part = new MimeBodyPart(); |
113 | 0 | part.setDataHandler(handler); |
114 | 0 | part.setDescription(name); |
115 | |
|
116 | 0 | part.setFileName(StringUtils.defaultString(handler.getName(), name)); |
117 | 0 | return part; |
118 | |
} |
119 | |
|
120 | |
protected BodyPart getPayloadBodyPart(Object payload, String contentType) |
121 | |
throws MessagingException, TransformerException, IOException |
122 | |
{ |
123 | |
DataHandler handler; |
124 | 0 | if (payload instanceof String) |
125 | |
{ |
126 | 0 | handler = new DataHandler(new ByteArrayDataSource((String) payload, contentType)); |
127 | |
} |
128 | 0 | else if (payload instanceof byte[]) |
129 | |
{ |
130 | 0 | handler = new DataHandler(new ByteArrayDataSource((byte[])payload, contentType)); |
131 | |
} |
132 | 0 | else if (payload instanceof Serializable) |
133 | |
{ |
134 | 0 | handler = new DataHandler(new ByteArrayDataSource( |
135 | |
(byte[])new SerializableToByteArray().transform(payload), contentType)); |
136 | |
} |
137 | |
else |
138 | |
{ |
139 | 0 | throw new IllegalArgumentException(); |
140 | |
} |
141 | 0 | BodyPart part = new MimeBodyPart(); |
142 | 0 | part.setDataHandler(handler); |
143 | 0 | part.setDescription("Payload"); |
144 | 0 | return part; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public void setUseInboundAttachments(boolean useInboundAttachments) |
151 | |
{ |
152 | 0 | this.useInboundAttachments = useInboundAttachments; |
153 | 0 | } |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public void setUseOutboundAttachments(boolean useOutboundAttachments) |
159 | |
{ |
160 | 0 | this.useOutboundAttachments = useOutboundAttachments; |
161 | 0 | } |
162 | |
} |