View Javadoc

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