1 10 11 package org.mule.providers.email.transformers; 12 13 import java.util.Calendar ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Properties ; 18 19 import javax.mail.Message ; 20 import javax.mail.Session ; 21 import javax.mail.internet.MimeMessage ; 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 42 public class StringToEmailMessage extends AbstractEventAwareTransformer 43 { 44 47 private static final long serialVersionUID = -2687301871292220521L; 48 49 52 protected final transient Log logger = LogFactory.getLog(getClass()); 53 54 protected TemplateParser templateParser = TemplateParser.createAntStyleParser(); 55 56 public StringToEmailMessage() 57 { 58 registerSourceType(String .class); 59 setReturnClass(Message .class); 60 } 61 62 67 public Object transform(Object src, String encoding, UMOEventContext context) throws TransformerException 68 { 69 String endpointAddress = endpoint.getEndpointURI().getAddress(); 70 SmtpConnector connector = (SmtpConnector)endpoint.getConnector(); 71 UMOMessage eventMsg = context.getMessage(); 72 String to = eventMsg.getStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress); 73 String cc = eventMsg.getStringProperty(MailProperties.CC_ADDRESSES_PROPERTY, 74 connector.getCcAddresses()); 75 String bcc = eventMsg.getStringProperty(MailProperties.BCC_ADDRESSES_PROPERTY, 76 connector.getBccAddresses()); 77 String from = eventMsg.getStringProperty(MailProperties.FROM_ADDRESS_PROPERTY, 78 connector.getFromAddress()); 79 String replyTo = eventMsg.getStringProperty(MailProperties.REPLY_TO_ADDRESSES_PROPERTY, 80 connector.getReplyToAddresses()); 81 String subject = eventMsg.getStringProperty(MailProperties.SUBJECT_PROPERTY, connector.getSubject()); 82 String contentType = eventMsg.getStringProperty(MailProperties.CONTENT_TYPE_PROPERTY, 83 connector.getContentType()); 84 85 Properties headers = new Properties (); 86 Properties customHeaders = connector.getCustomHeaders(); 87 88 if (customHeaders != null && !customHeaders.isEmpty()) 89 { 90 headers.putAll(customHeaders); 91 } 92 93 Properties otherHeaders = (Properties )eventMsg.getProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY); 94 if (otherHeaders != null && !otherHeaders.isEmpty()) 95 { 96 Map props = new HashMap (MuleManager.getInstance().getProperties()); 97 for (Iterator iterator = eventMsg.getPropertyNames().iterator(); iterator.hasNext();) 98 { 99 String propertyKey = (String )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 buf = new StringBuffer (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 email = new MimeMessage ((Session )endpoint.getConnector() 124 .getDispatcher(endpoint) 125 .getDelegateSession()); 126 127 email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to)); 128 129 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 iterator = headers.entrySet().iterator(); iterator.hasNext();) 155 { 156 Map.Entry entry = (Map.Entry )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 e) 165 { 166 throw new TransformerException(this, e); 167 } 168 } 169 170 protected void setContent(Object payload, Message msg, String contentType, UMOEventContext context) 171 throws Exception 172 { 173 msg.setContent(payload, contentType); 174 } 175 176 } 177 | Popular Tags |