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