View Javadoc

1   /*
2    * $Id: EmailMessageToString.java 10787 2008-02-12 18:51:50Z 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.transformer.TransformerException;
14  import org.mule.transformer.AbstractTransformer;
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.transformer.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.
45               */
46  
47              // For this impl we just pass back the email content
48              Object result = msg.getContent();
49              if (result instanceof String)
50              {
51                  return result;
52              }
53              else
54              {
55                  // very simplisitic, only gets first part
56                  MimeMultipart part = (MimeMultipart)result;
57                  String transMsg = (String) part.getBodyPart(0).getContent();
58                  return transMsg;
59              }
60          }
61          catch (Exception e)
62          {
63              throw new TransformerException(this, e);
64          }
65      }
66  }