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