KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > email > transformers > ObjectToMimeMessage


1 /*
2  * $Id: ObjectToMimeMessage.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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 java.io.IOException JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.activation.DataHandler JavaDoc;
19 import javax.mail.BodyPart JavaDoc;
20 import javax.mail.Message JavaDoc;
21 import javax.mail.MessagingException JavaDoc;
22 import javax.mail.internet.MimeBodyPart JavaDoc;
23 import javax.mail.internet.MimeMultipart JavaDoc;
24
25 import org.mule.providers.email.MailMessageAdapter;
26 import org.mule.transformers.simple.SerializableToByteArray;
27 import org.mule.umo.UMOEventContext;
28 import org.mule.umo.transformer.TransformerException;
29 import org.mule.util.StringUtils;
30
31 /**
32  * Transforms a javax.mail.Message to a UMOMessage, with support for attachments
33  */

34 public class ObjectToMimeMessage extends StringToEmailMessage
35 {
36     /**
37      * Serial version
38      */

39     private static final long serialVersionUID = 7225142214620572674L;
40
41     protected void setContent(Object JavaDoc payload, Message JavaDoc msg, String JavaDoc contentType, UMOEventContext context)
42         throws Exception JavaDoc
43     {
44
45         if (context.getMessage().getAttachmentNames().size() > 0)
46         {
47             // The content type must be multipart/mixed
48
MimeMultipart JavaDoc multipart = new MimeMultipart JavaDoc("mixed");
49             multipart.addBodyPart(getPayloadBodyPart(payload, contentType));
50             for (Iterator JavaDoc it = context.getMessage().getAttachmentNames().iterator(); it.hasNext();)
51             {
52                 String JavaDoc name = (String JavaDoc)it.next();
53                 BodyPart JavaDoc part = getBodyPartForAttachment(context.getMessage().getAttachment(name), name);
54                 // Check message props for extra headers
55
addBodyPartHeaders(part, name, context);
56                 multipart.addBodyPart(part);
57             }
58             // the payload must be set to the constructed MimeMultipart message
59
payload = multipart;
60             // the ContentType of the message to be sent, must be the multipart
61
contentType = multipart.getContentType();
62             // content type
63
}
64         // now the message will contain the multipart payload, and the multipart
65
// contentType
66
super.setContent(payload, msg, contentType, context);
67     }
68
69     protected void addBodyPartHeaders(BodyPart JavaDoc part, String JavaDoc name, UMOEventContext context)
70     {
71
72         Map JavaDoc headers = (Map JavaDoc)context.getMessage().getProperty(
73             name + MailMessageAdapter.ATTACHMENT_HEADERS_PROPERTY_POSTFIX);
74         if (null != headers)
75         {
76             for (Iterator JavaDoc it = headers.keySet().iterator(); it.hasNext();)
77             {
78                 try
79                 {
80                     String JavaDoc key = (String JavaDoc)it.next();
81                     part.setHeader(key, (String JavaDoc)headers.get(key));
82                 }
83                 catch (MessagingException JavaDoc me)
84                 {
85                     logger.error("Failed to set bodypart header", me);
86                 }
87             }
88         }
89     }
90
91     protected BodyPart JavaDoc getBodyPartForAttachment(DataHandler JavaDoc handler, String JavaDoc name) throws MessagingException JavaDoc
92     {
93         BodyPart JavaDoc part = new MimeBodyPart JavaDoc();
94         part.setDataHandler(handler);
95         part.setDescription(name);
96         part.setFileName(StringUtils.defaultString(handler.getName(), name));
97         return part;
98     }
99
100     protected BodyPart JavaDoc getPayloadBodyPart(Object JavaDoc payload, String JavaDoc contentType)
101         throws MessagingException JavaDoc, TransformerException, IOException JavaDoc
102     {
103
104         DataHandler JavaDoc handler;
105         if (payload instanceof String JavaDoc)
106         {
107             handler = new DataHandler JavaDoc(new PlainTextDataSource(contentType, payload.toString()));
108         }
109         else if (payload instanceof byte[])
110         {
111             handler = new DataHandler JavaDoc(new ByteArrayDataSource((byte[])payload, contentType));
112         }
113         else if (payload instanceof Serializable JavaDoc)
114         {
115             handler = new DataHandler JavaDoc(new ByteArrayDataSource(
116                 (byte[])new SerializableToByteArray().transform(payload), contentType));
117         }
118         else
119         {
120             throw new IllegalArgumentException JavaDoc();
121         }
122         BodyPart JavaDoc part = new MimeBodyPart JavaDoc();
123         part.setDataHandler(handler);
124         part.setDescription("Payload");
125         return part;
126     }
127
128 }
129
Popular Tags