View Javadoc

1   /*
2    * $Id: EmailMessageToString.java 7963 2007-08-21 08:53:15Z 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.transformers.AbstractTransformer;
14  import org.mule.umo.transformer.TransformerException;
15  
16  import javax.mail.Message;
17  import javax.mail.internet.MimeMultipart;
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      public EmailMessageToString()
27      {
28          registerSourceType(Message.class);
29          setReturnClass(String.class);
30      }
31  
32      /*
33       * (non-Javadoc)
34       * 
35       * @see org.mule.transformers.AbstractTransformer#doTransform(java.lang.Object)
36       */
37      public Object doTransform(Object src, String encoding) throws TransformerException
38      {
39          Message msg = (Message) src;
40          try
41          {
42              /*
43               * Other information about the message such as cc addresses, attachments
44               * are handled by the mail message adapter. If Transformers need access
45               * to these properties they should extends from
46               * AbstractEventAwareTransformer and extract these properties from the
47               * passed UMOEventContext
48               */
49  
50              // For this impl we just pass back the email content
51              Object result = msg.getContent();
52              if (result instanceof String)
53              {
54                  return result;
55              }
56              else
57              {
58                  // very simplisitic, only gets first part
59                  MimeMultipart part = (MimeMultipart)result;
60                  String transMsg = (String) part.getBodyPart(0).getContent();
61                  return transMsg;
62              }
63          }
64          catch (Exception e)
65          {
66              throw new TransformerException(this, e);
67          }
68      }
69  }