1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.email.transformers; |
12 | |
|
13 | |
import org.mule.RegistryContext; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.transformer.TransformerException; |
16 | |
import org.mule.transformer.AbstractMessageAwareTransformer; |
17 | |
import org.mule.transport.email.MailProperties; |
18 | |
import org.mule.transport.email.MailUtils; |
19 | |
import org.mule.transport.email.SmtpConnector; |
20 | |
import org.mule.util.MapUtils; |
21 | |
import org.mule.util.StringUtils; |
22 | |
import org.mule.util.TemplateParser; |
23 | |
|
24 | |
import java.util.Calendar; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Map; |
27 | |
import java.util.Properties; |
28 | |
|
29 | |
import javax.mail.Message; |
30 | |
import javax.mail.internet.MimeMessage; |
31 | |
|
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class StringToEmailMessage extends AbstractMessageAwareTransformer |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | 26 | private final Log logger = LogFactory.getLog(getClass()); |
46 | |
|
47 | 26 | private TemplateParser templateParser = TemplateParser.createAntStyleParser(); |
48 | |
|
49 | |
public StringToEmailMessage() |
50 | 26 | { |
51 | 26 | this.registerSourceType(String.class); |
52 | 26 | this.setReturnClass(Message.class); |
53 | 26 | } |
54 | |
|
55 | |
|
56 | |
public Object transform(MuleMessage message, String outputEncoding) throws TransformerException |
57 | |
{ |
58 | 14 | String endpointAddress = endpoint.getEndpointURI().getAddress(); |
59 | 14 | SmtpConnector connector = (SmtpConnector) endpoint.getConnector(); |
60 | 14 | String to = message.getStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress); |
61 | 14 | String cc = message.getStringProperty(MailProperties.CC_ADDRESSES_PROPERTY, |
62 | |
connector.getCcAddresses()); |
63 | 14 | String bcc = message.getStringProperty(MailProperties.BCC_ADDRESSES_PROPERTY, |
64 | |
connector.getBccAddresses()); |
65 | 14 | String from = message.getStringProperty(MailProperties.FROM_ADDRESS_PROPERTY, |
66 | |
connector.getFromAddress()); |
67 | 14 | String replyTo = message.getStringProperty(MailProperties.REPLY_TO_ADDRESSES_PROPERTY, |
68 | |
connector.getReplyToAddresses()); |
69 | 14 | String subject = message.getStringProperty(MailProperties.SUBJECT_PROPERTY, connector.getSubject()); |
70 | 14 | String contentType = message.getStringProperty(MailProperties.CONTENT_TYPE_PROPERTY, |
71 | |
connector.getContentType()); |
72 | |
|
73 | 14 | Properties headers = new Properties(); |
74 | 14 | Properties customHeaders = connector.getCustomHeaders(); |
75 | |
|
76 | 14 | if (customHeaders != null && !customHeaders.isEmpty()) |
77 | |
{ |
78 | 0 | headers.putAll(customHeaders); |
79 | |
} |
80 | |
|
81 | 14 | Properties otherHeaders = (Properties) message.getProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY); |
82 | 14 | if (otherHeaders != null && !otherHeaders.isEmpty()) |
83 | |
{ |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 0 | headers.putAll(templateParser.parse(new TemplateParser.TemplateCallback() |
92 | |
{ |
93 | 0 | public Object match(String token) |
94 | |
{ |
95 | 0 | return RegistryContext.getRegistry().lookupObject(token); |
96 | |
} |
97 | |
}, otherHeaders)); |
98 | |
|
99 | |
} |
100 | |
|
101 | 14 | if (logger.isDebugEnabled()) |
102 | |
{ |
103 | 0 | StringBuffer buf = new StringBuffer(); |
104 | 0 | buf.append("Constructing email using:\n"); |
105 | 0 | buf.append("To: ").append(to); |
106 | 0 | buf.append("From: ").append(from); |
107 | 0 | buf.append("CC: ").append(cc); |
108 | 0 | buf.append("BCC: ").append(bcc); |
109 | 0 | buf.append("Subject: ").append(subject); |
110 | 0 | buf.append("ReplyTo: ").append(replyTo); |
111 | 0 | buf.append("Content type: ").append(contentType); |
112 | 0 | buf.append("Payload type: ").append(message.getPayload().getClass().getName()); |
113 | 0 | buf.append("Custom Headers: ").append(MapUtils.toString(headers, false)); |
114 | 0 | logger.debug(buf.toString()); |
115 | |
} |
116 | |
|
117 | |
try |
118 | |
{ |
119 | 14 | Message email = new MimeMessage(((SmtpConnector) endpoint.getConnector()).getSessionDetails(endpoint).getSession()); |
120 | |
|
121 | 14 | email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to)); |
122 | |
|
123 | |
|
124 | 14 | email.setSentDate(Calendar.getInstance().getTime()); |
125 | |
|
126 | 14 | if (StringUtils.isNotBlank(from)) |
127 | |
{ |
128 | 0 | email.setFrom(MailUtils.stringToInternetAddresses(from)[0]); |
129 | |
} |
130 | |
|
131 | 14 | if (StringUtils.isNotBlank(cc)) |
132 | |
{ |
133 | 0 | email.setRecipients(Message.RecipientType.CC, MailUtils.stringToInternetAddresses(cc)); |
134 | |
} |
135 | |
|
136 | 14 | if (StringUtils.isNotBlank(bcc)) |
137 | |
{ |
138 | 0 | email.setRecipients(Message.RecipientType.BCC, MailUtils.stringToInternetAddresses(bcc)); |
139 | |
} |
140 | |
|
141 | 14 | if (StringUtils.isNotBlank(replyTo)) |
142 | |
{ |
143 | 0 | email.setReplyTo(MailUtils.stringToInternetAddresses(replyTo)); |
144 | |
} |
145 | |
|
146 | 14 | email.setSubject(subject); |
147 | |
|
148 | 14 | for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();) |
149 | |
{ |
150 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
151 | 0 | email.setHeader(entry.getKey().toString(), entry.getValue().toString()); |
152 | 0 | } |
153 | |
|
154 | 14 | setContent(message.getPayload(), email, contentType, message); |
155 | |
|
156 | 14 | return email; |
157 | |
} |
158 | 0 | catch (Exception e) |
159 | |
{ |
160 | 0 | throw new TransformerException(this, e); |
161 | |
} |
162 | |
} |
163 | |
|
164 | |
protected void setContent(Object payload, Message msg, String contentType, MuleMessage message) |
165 | |
throws Exception |
166 | |
{ |
167 | 14 | msg.setContent(payload, contentType); |
168 | 14 | } |
169 | |
|
170 | |
} |