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