View Javadoc

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  public class ObjectToMimeMessage extends StringToEmailMessage
40  {
41  
42      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          if (context.getMessage().getAttachmentNames().size() > 0)
50          {
51              // The content type must be multipart/mixed
52              MimeMultipart multipart = new MimeMultipart("mixed");
53              multipart.addBodyPart(getPayloadBodyPart(payload, contentType));
54              for (Iterator it = context.getMessage().getAttachmentNames().iterator(); it.hasNext();)
55              {
56                  String name = (String)it.next();
57                  BodyPart part = getBodyPartForAttachment(context.getMessage().getAttachment(name), name);
58                  // Check message props for extra headers
59                  addBodyPartHeaders(part, name, context);
60                  multipart.addBodyPart(part);
61              }
62              // the payload must be set to the constructed MimeMultipart message
63              payload = multipart;
64              // the ContentType of the message to be sent, must be the multipart
65              contentType = multipart.getContentType();
66              // content type
67          }
68          // now the message will contain the multipart payload, and the multipart
69          // contentType
70          super.setContent(payload, msg, contentType, context);
71      }
72  
73      protected void addBodyPartHeaders(BodyPart part, String name, UMOEventContext context)
74      {
75          Map headers = (Map)context.getMessage().getProperty(
76              name + MailMessageAdapter.ATTACHMENT_HEADERS_PROPERTY_POSTFIX);
77  
78          if (null != headers)
79          {
80              for (Iterator it = headers.keySet().iterator(); it.hasNext();)
81              {
82                  try
83                  {
84                      String key = (String)it.next();
85                      part.setHeader(key, (String)headers.get(key));
86                  }
87                  catch (MessagingException me)
88                  {
89                      logger.error("Failed to set bodypart header", me);
90                  }
91              }
92          }
93      }
94  
95      protected BodyPart getBodyPartForAttachment(DataHandler handler, String name) throws MessagingException
96      {
97          BodyPart part = new MimeBodyPart();
98          part.setDataHandler(handler);
99          part.setDescription(name);
100 
101         DataSource source = handler.getDataSource();
102 
103         // Only set the file name if the DataSource is a file
104         if (source instanceof FileDataSource)
105         {
106             part.setFileName(StringUtils.defaultString(handler.getName(), name));
107         }
108         return part;
109     }
110 
111     protected BodyPart getPayloadBodyPart(Object payload, String contentType)
112         throws MessagingException, TransformerException, IOException
113     {
114 
115         DataHandler handler;
116         if (payload instanceof String)
117         {
118             handler = new DataHandler(new PlainTextDataSource(contentType, payload.toString()));
119         }
120         else if (payload instanceof byte[])
121         {
122             handler = new DataHandler(new ByteArrayDataSource((byte[])payload, contentType));
123         }
124         else if (payload instanceof Serializable)
125         {
126             handler = new DataHandler(new ByteArrayDataSource(
127                 (byte[])new SerializableToByteArray().transform(payload), contentType));
128         }
129         else
130         {
131             throw new IllegalArgumentException();
132         }
133         BodyPart part = new MimeBodyPart();
134         part.setDataHandler(handler);
135         part.setDescription("Payload");
136         return part;
137     }
138 
139 }