KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EmailMessageToString.java 3798 2006-11-04 04:07:14Z aperepel $
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 javax.mail.Message JavaDoc;
14 import javax.mail.internet.MimeMultipart JavaDoc;
15
16 import org.mule.transformers.AbstractTransformer;
17 import org.mule.umo.transformer.TransformerException;
18
19 /**
20  * <code>EmailMessageToString</code> extracts a java mail Message contents and
21  * returns a string.
22  */

23 public class EmailMessageToString extends AbstractTransformer
24 {
25     /**
26      * Serial version
27      */

28     private static final long serialVersionUID = 8940364998861971795L;
29
30     public EmailMessageToString()
31     {
32         registerSourceType(Message JavaDoc.class);
33         registerSourceType(String JavaDoc.class);
34         setReturnClass(String JavaDoc.class);
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.mule.transformers.AbstractTransformer#doTransform(java.lang.Object)
41      */

42     public Object JavaDoc doTransform(Object JavaDoc src, String JavaDoc encoding) throws TransformerException
43     {
44         Message JavaDoc msg = (Message JavaDoc)src;
45         try
46         {
47             // Other information about the message such as cc addresses, attachments
48
// are handled
49
// By the Mail MEssage adapter. If Transformers need access to these
50
// properties
51
// they should implement the AbstractEventAwareTransformer and extract
52
// these properties
53
// from the passed UMOEventContext
54

55             // For this impl we just pass back the email content
56
Object JavaDoc result = msg.getContent();
57             if (result instanceof String JavaDoc)
58             {
59                 return result;
60             }
61             else
62             {
63                 // very simplisitic, only gets first part
64
MimeMultipart JavaDoc part = (MimeMultipart JavaDoc)result;
65                 String JavaDoc transMsg = (String JavaDoc)part.getBodyPart(0).getContent();
66                 return transMsg;
67             }
68         }
69         catch (Exception JavaDoc e)
70         {
71             throw new TransformerException(this, e);
72         }
73     }
74 }
75
Popular Tags