KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > email > transformers > StringToEmailMessage


1 /*
2  * $Id: StringToEmailMessage.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.email.transformers;
12
13 import java.util.Calendar JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import javax.mail.Message JavaDoc;
20 import javax.mail.Session JavaDoc;
21 import javax.mail.internet.MimeMessage JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.mule.MuleManager;
27 import org.mule.providers.email.MailProperties;
28 import org.mule.providers.email.MailUtils;
29 import org.mule.providers.email.SmtpConnector;
30 import org.mule.transformers.AbstractEventAwareTransformer;
31 import org.mule.umo.UMOEventContext;
32 import org.mule.umo.UMOMessage;
33 import org.mule.umo.transformer.TransformerException;
34 import org.mule.util.MapUtils;
35 import org.mule.util.TemplateParser;
36
37 /**
38  * <code>StringToEmailMessage</code> will convert a String to a JavaMail Message,
39  * using the String as the contents. This implementation uses properties on the
40  * transformer to determine the To: and Subject: fields.
41  */

42 public class StringToEmailMessage extends AbstractEventAwareTransformer
43 {
44     /**
45      * Serial version
46      */

47     private static final long serialVersionUID = -2687301871292220521L;
48
49     /**
50      * logger used by this class
51      */

52     protected final transient Log logger = LogFactory.getLog(getClass());
53
54     protected TemplateParser templateParser = TemplateParser.createAntStyleParser();
55
56     public StringToEmailMessage()
57     {
58         registerSourceType(String JavaDoc.class);
59         setReturnClass(Message JavaDoc.class);
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see org.mule.transformers.AbstractTransformer#doTransform(java.lang.Object)
66      */

67     public Object JavaDoc transform(Object JavaDoc src, String JavaDoc encoding, UMOEventContext context) throws TransformerException
68     {
69         String JavaDoc endpointAddress = endpoint.getEndpointURI().getAddress();
70         SmtpConnector connector = (SmtpConnector)endpoint.getConnector();
71         UMOMessage eventMsg = context.getMessage();
72         String JavaDoc to = eventMsg.getStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress);
73         String JavaDoc cc = eventMsg.getStringProperty(MailProperties.CC_ADDRESSES_PROPERTY,
74             connector.getCcAddresses());
75         String JavaDoc bcc = eventMsg.getStringProperty(MailProperties.BCC_ADDRESSES_PROPERTY,
76             connector.getBccAddresses());
77         String JavaDoc from = eventMsg.getStringProperty(MailProperties.FROM_ADDRESS_PROPERTY,
78             connector.getFromAddress());
79         String JavaDoc replyTo = eventMsg.getStringProperty(MailProperties.REPLY_TO_ADDRESSES_PROPERTY,
80             connector.getReplyToAddresses());
81         String JavaDoc subject = eventMsg.getStringProperty(MailProperties.SUBJECT_PROPERTY, connector.getSubject());
82         String JavaDoc contentType = eventMsg.getStringProperty(MailProperties.CONTENT_TYPE_PROPERTY,
83             connector.getContentType());
84
85         Properties JavaDoc headers = new Properties JavaDoc();
86         Properties JavaDoc customHeaders = connector.getCustomHeaders();
87
88         if (customHeaders != null && !customHeaders.isEmpty())
89         {
90             headers.putAll(customHeaders);
91         }
92
93         Properties JavaDoc otherHeaders = (Properties JavaDoc)eventMsg.getProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY);
94         if (otherHeaders != null && !otherHeaders.isEmpty())
95         {
96             Map JavaDoc props = new HashMap JavaDoc(MuleManager.getInstance().getProperties());
97             for (Iterator JavaDoc iterator = eventMsg.getPropertyNames().iterator(); iterator.hasNext();)
98             {
99                 String JavaDoc propertyKey = (String JavaDoc)iterator.next();
100                 props.put(propertyKey, eventMsg.getProperty(propertyKey));
101             }
102             headers.putAll(templateParser.parse(props, otherHeaders));
103         }
104
105         if (logger.isDebugEnabled())
106         {
107             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(256);
108             buf.append("Constructing email using:\n");
109             buf.append("To: ").append(to);
110             buf.append("From: ").append(from);
111             buf.append("CC: ").append(cc);
112             buf.append("BCC: ").append(bcc);
113             buf.append("Subject: ").append(subject);
114             buf.append("ReplyTo: ").append(replyTo);
115             buf.append("Content type: ").append(contentType);
116             buf.append("Payload type: ").append(src.getClass().getName());
117             buf.append("Custom Headers: ").append(MapUtils.toString(headers, false));
118             logger.debug(buf.toString());
119         }
120
121         try
122         {
123             Message JavaDoc email = new MimeMessage JavaDoc((Session JavaDoc)endpoint.getConnector()
124                 .getDispatcher(endpoint)
125                 .getDelegateSession());
126
127             email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to));
128
129             // sent date
130
email.setSentDate(Calendar.getInstance().getTime());
131
132             if (StringUtils.isNotBlank(from))
133             {
134                 email.setFrom(MailUtils.stringToInternetAddresses(from)[0]);
135             }
136
137             if (StringUtils.isNotBlank(cc))
138             {
139                 email.setRecipients(Message.RecipientType.CC, MailUtils.stringToInternetAddresses(cc));
140             }
141
142             if (StringUtils.isNotBlank(bcc))
143             {
144                 email.setRecipients(Message.RecipientType.BCC, MailUtils.stringToInternetAddresses(bcc));
145             }
146
147             if (StringUtils.isNotBlank(replyTo))
148             {
149                 email.setReplyTo(MailUtils.stringToInternetAddresses(replyTo));
150             }
151
152             email.setSubject(subject);
153
154             for (Iterator JavaDoc iterator = headers.entrySet().iterator(); iterator.hasNext();)
155             {
156                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
157                 email.setHeader(entry.getKey().toString(), entry.getValue().toString());
158             }
159
160             setContent(src, email, contentType, context);
161
162             return email;
163         }
164         catch (Exception JavaDoc e)
165         {
166             throw new TransformerException(this, e);
167         }
168     }
169
170     protected void setContent(Object JavaDoc payload, Message JavaDoc msg, String JavaDoc contentType, UMOEventContext context)
171         throws Exception JavaDoc
172     {
173         msg.setContent(payload, contentType);
174     }
175
176 }
177
Popular Tags