Coverage Report - org.mule.providers.email.transformers.ObjectToMimeMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectToMimeMessage
12%
5/42
6%
1/16
3.5
 
 1  
 /*
 2  
  * $Id: ObjectToMimeMessage.java 10478 2008-01-23 09:57:16Z holger $
 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 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.providers.email.transformers;
 12  
 
 13  
 import org.mule.providers.email.MailMessageAdapter;
 14  
 import org.mule.transformers.simple.SerializableToByteArray;
 15  
 import org.mule.umo.UMOEventContext;
 16  
 import org.mule.umo.transformer.TransformerException;
 17  
 import org.mule.util.StringUtils;
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Serializable;
 21  
 import java.util.Iterator;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.activation.DataHandler;
 25  
 import javax.activation.DataSource;
 26  
 import javax.activation.FileDataSource;
 27  
 import javax.mail.BodyPart;
 28  
 import javax.mail.Message;
 29  
 import javax.mail.MessagingException;
 30  
 import javax.mail.internet.MimeBodyPart;
 31  
 import javax.mail.internet.MimeMultipart;
 32  
 import javax.mail.util.ByteArrayDataSource;
 33  
 
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 /**
 38  
  * Transforms a javax.mail.Message to a UMOMessage, with support for attachments
 39  
  */
 40  18
 public class ObjectToMimeMessage extends StringToEmailMessage
 41  
 {
 42  
 
 43  18
     private Log logger = LogFactory.getLog(getClass());
 44  
     
 45  
     // @Override
 46  
     protected void setContent(Object payload, Message msg, String contentType, UMOEventContext context)
 47  
         throws Exception
 48  
     {
 49  
 
 50  4
         if (context.getMessage().getAttachmentNames().size() > 0)
 51  
         {
 52  
             // The content type must be multipart/mixed
 53  0
             MimeMultipart multipart = new MimeMultipart("mixed");
 54  0
             multipart.addBodyPart(getPayloadBodyPart(payload, contentType));
 55  0
             for (Iterator it = context.getMessage().getAttachmentNames().iterator(); it.hasNext();)
 56  
             {
 57  0
                 String name = (String)it.next();
 58  0
                 BodyPart part = getBodyPartForAttachment(context.getMessage().getAttachment(name), name);
 59  
                 // Check message props for extra headers
 60  0
                 addBodyPartHeaders(part, name, context);
 61  0
                 multipart.addBodyPart(part);
 62  0
             }
 63  
             // the payload must be set to the constructed MimeMultipart message
 64  0
             payload = multipart;
 65  
             // the ContentType of the message to be sent, must be the multipart
 66  0
             contentType = multipart.getContentType();
 67  
             // content type
 68  
         }
 69  
         // now the message will contain the multipart payload, and the multipart
 70  
         // contentType
 71  4
         super.setContent(payload, msg, contentType, context);
 72  4
     }
 73  
 
 74  
     protected void addBodyPartHeaders(BodyPart part, String name, UMOEventContext context)
 75  
     {
 76  0
         Map headers = (Map)context.getMessage().getProperty(
 77  
             name + MailMessageAdapter.ATTACHMENT_HEADERS_PROPERTY_POSTFIX);
 78  
 
 79  0
         if (null != headers)
 80  
         {
 81  0
             for (Iterator it = headers.keySet().iterator(); it.hasNext();)
 82  
             {
 83  
                 try
 84  
                 {
 85  0
                     String key = (String)it.next();
 86  0
                     part.setHeader(key, (String)headers.get(key));
 87  
                 }
 88  0
                 catch (MessagingException me)
 89  
                 {
 90  0
                     logger.error("Failed to set bodypart header", me);
 91  0
                 }
 92  
             }
 93  
         }
 94  0
     }
 95  
 
 96  
     protected BodyPart getBodyPartForAttachment(DataHandler handler, String name) throws MessagingException
 97  
     {
 98  0
         BodyPart part = new MimeBodyPart();
 99  0
         part.setDataHandler(handler);
 100  0
         part.setDescription(name);
 101  
 
 102  0
         DataSource source = handler.getDataSource();
 103  
 
 104  
         // Only set the file name if the DataSource is a file
 105  0
         if (source instanceof FileDataSource)
 106  
         {
 107  0
             part.setFileName(StringUtils.defaultString(handler.getName(), name));
 108  
         }
 109  0
         return part;
 110  
     }
 111  
 
 112  
     protected BodyPart getPayloadBodyPart(Object payload, String contentType)
 113  
         throws MessagingException, TransformerException, IOException
 114  
     {
 115  
 
 116  
         DataHandler handler;
 117  0
         if (payload instanceof String)
 118  
         {
 119  0
             handler = new DataHandler(new ByteArrayDataSource((String)payload, contentType));
 120  
         }
 121  0
         else if (payload instanceof byte[])
 122  
         {
 123  0
             handler = new DataHandler(new ByteArrayDataSource((byte[])payload, contentType));
 124  
         }
 125  0
         else if (payload instanceof Serializable)
 126  
         {
 127  0
             handler = new DataHandler(new ByteArrayDataSource(
 128  
                 (byte[])new SerializableToByteArray().transform(payload), contentType));
 129  
         }
 130  
         else
 131  
         {
 132  0
             throw new IllegalArgumentException();
 133  
         }
 134  0
         BodyPart part = new MimeBodyPart();
 135  0
         part.setDataHandler(handler);
 136  0
         part.setDescription("Payload");
 137  0
         return part;
 138  
     }
 139  
 
 140  
 }