Coverage Report - org.mule.transport.email.transformers.StringToEmailMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
StringToEmailMessage
0%
0/62
0%
0/22
0
StringToEmailMessage$1
0%
0/2
N/A
0
 
 1  
 /*
 2  
  * $Id: StringToEmailMessage.java 19250 2010-08-30 16:53:14Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.email.transformers;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.transformer.AbstractMessageTransformer;
 16  
 import org.mule.transformer.types.DataTypeFactory;
 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  
  * <code>StringToEmailMessage</code> will convert a String to a JavaMail Message,
 37  
  * using the String as the contents. This implementation uses properties on the
 38  
  * transformer to determine the To: and Subject: fields.
 39  
  */
 40  0
 public class StringToEmailMessage extends AbstractMessageTransformer
 41  
 {
 42  
     /**
 43  
      * logger used by this class
 44  
      */
 45  0
     private final Log logger = LogFactory.getLog(getClass());
 46  
 
 47  0
     private TemplateParser templateParser = TemplateParser.createMuleStyleParser();
 48  
 
 49  
     public StringToEmailMessage()
 50  0
     {
 51  0
         this.registerSourceType(DataTypeFactory.STRING);
 52  0
         this.setReturnDataType(DataTypeFactory.create(Message.class));
 53  0
     }
 54  
 
 55  
 
 56  
     @Override
 57  
     public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
 58  
     {
 59  0
         String endpointAddress = endpoint.getEndpointURI().getAddress();
 60  0
         SmtpConnector connector = (SmtpConnector) endpoint.getConnector();
 61  0
         String to = lookupProperty(message, MailProperties.TO_ADDRESSES_PROPERTY, endpointAddress);
 62  0
         String cc = lookupProperty(message, MailProperties.CC_ADDRESSES_PROPERTY, connector.getCcAddresses());
 63  0
         String bcc = lookupProperty(message, MailProperties.BCC_ADDRESSES_PROPERTY, connector.getBccAddresses());
 64  0
         String from = lookupProperty(message, MailProperties.FROM_ADDRESS_PROPERTY, connector.getFromAddress());
 65  0
         String replyTo = lookupProperty(message, MailProperties.REPLY_TO_ADDRESSES_PROPERTY, connector.getReplyToAddresses());
 66  0
         String subject = lookupProperty(message, MailProperties.SUBJECT_PROPERTY, connector.getSubject());
 67  0
         String contentType = lookupProperty(message, MailProperties.CONTENT_TYPE_PROPERTY, connector.getContentType());
 68  
 
 69  0
         Properties headers = new Properties();
 70  0
         Properties customHeaders = connector.getCustomHeaders();
 71  
 
 72  0
         if (customHeaders != null && !customHeaders.isEmpty())
 73  
         {
 74  0
             headers.putAll(customHeaders);
 75  
         }
 76  
 
 77  0
         Properties otherHeaders = message.getOutboundProperty(MailProperties.CUSTOM_HEADERS_MAP_PROPERTY);
 78  0
         if (otherHeaders != null && !otherHeaders.isEmpty())
 79  
         {
 80  
                 //TODO Whats going on here?
 81  
 //                final MuleContext mc = context.getMuleContext();
 82  
 //                for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
 83  
 //                {
 84  
 //                    String propertyKey = (String) iterator.next();
 85  
 //                    mc.getRegistry().registerObject(propertyKey, message.getProperty(propertyKey), mc);
 86  
 //                }
 87  0
                 headers.putAll(templateParser.parse(new TemplateParser.TemplateCallback()
 88  0
                 {
 89  
                     public Object match(String token)
 90  
                     {
 91  0
                         return muleContext.getRegistry().lookupObject(token);
 92  
                     }
 93  
                 }, otherHeaders));
 94  
 
 95  
         }
 96  
 
 97  0
         if (logger.isDebugEnabled())
 98  
         {
 99  0
             StringBuffer buf = new StringBuffer();
 100  0
             buf.append("Constructing email using:\n");
 101  0
             buf.append("To: ").append(to);
 102  0
             buf.append(", From: ").append(from);
 103  0
             buf.append(", CC: ").append(cc);
 104  0
             buf.append(", BCC: ").append(bcc);
 105  0
             buf.append(", Subject: ").append(subject);
 106  0
             buf.append(", ReplyTo: ").append(replyTo);
 107  0
             buf.append(", Content type: ").append(contentType);
 108  0
             buf.append(", Payload type: ").append(message.getPayload().getClass().getName());
 109  0
             buf.append(", Custom Headers: ").append(MapUtils.toString(headers, false));
 110  0
             logger.debug(buf.toString());
 111  
         }
 112  
 
 113  
         try
 114  
         {
 115  0
             Message email = new MimeMessage(((SmtpConnector) endpoint.getConnector()).getSessionDetails(endpoint).getSession());
 116  
 
 117  0
             email.setRecipients(Message.RecipientType.TO, MailUtils.stringToInternetAddresses(to));
 118  
 
 119  
             // sent date
 120  0
             email.setSentDate(Calendar.getInstance().getTime());
 121  
 
 122  0
             if (StringUtils.isNotBlank(from))
 123  
             {
 124  0
                 email.setFrom(MailUtils.stringToInternetAddresses(from)[0]);
 125  
             }
 126  
 
 127  0
             if (StringUtils.isNotBlank(cc))
 128  
             {
 129  0
                 email.setRecipients(Message.RecipientType.CC, MailUtils.stringToInternetAddresses(cc));
 130  
             }
 131  
 
 132  0
             if (StringUtils.isNotBlank(bcc))
 133  
             {
 134  0
                 email.setRecipients(Message.RecipientType.BCC, MailUtils.stringToInternetAddresses(bcc));
 135  
             }
 136  
 
 137  0
             if (StringUtils.isNotBlank(replyTo))
 138  
             {
 139  0
                 email.setReplyTo(MailUtils.stringToInternetAddresses(replyTo));
 140  
             }
 141  
 
 142  0
             email.setSubject(subject);
 143  
 
 144  0
             for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();)
 145  
             {
 146  0
                 Map.Entry entry = (Map.Entry) iterator.next();
 147  0
                 email.setHeader(entry.getKey().toString(), entry.getValue().toString());
 148  0
             }
 149  
 
 150  0
             setContent(message.getPayload(), email, contentType, message);
 151  
 
 152  0
             return email;
 153  
         }
 154  0
         catch (Exception e)
 155  
         {
 156  0
             throw new TransformerException(this, e);
 157  
         }
 158  
     }
 159  
 
 160  
     /**
 161  
      * Searches in outbound, then invocation scope. If not found, returns a passed in default value.
 162  
      */
 163  
     protected String lookupProperty(MuleMessage message, String propName, String defaultValue)
 164  
     {
 165  0
         String value = message.getOutboundProperty(propName);
 166  0
         if (value == null)
 167  
         {
 168  0
             value = message.getInvocationProperty(propName, defaultValue);
 169  
         }
 170  0
         return value;
 171  
     }
 172  
 
 173  
     protected void setContent(Object payload, Message msg, String contentType, MuleMessage message)
 174  
         throws Exception
 175  
     {
 176  0
         msg.setContent(payload, contentType);
 177  0
     }
 178  
 
 179  
 }