View Javadoc

1   /*
2    * $Id: EmailMessageToString.java 19250 2010-08-30 16:53:14Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  import org.mule.transformer.types.DataTypeFactory;
16  
17  import javax.mail.Message;
18  import javax.mail.internet.MimeMultipart;
19  
20  /**
21   * <code>EmailMessageToString</code> extracts a java mail Message contents and
22   * returns a string.
23   */
24  public class EmailMessageToString extends AbstractTransformer
25  {
26  
27      public EmailMessageToString()
28      {
29          registerSourceType(DataTypeFactory.create(Message.class));
30          setReturnDataType(DataTypeFactory.STRING);
31      }
32  
33      @Override
34      public Object doTransform(Object src, String outputEncoding) throws TransformerException
35      {
36          Message msg = (Message) src;
37          try
38          {
39              /*
40               * Other information about the message such as cc addresses, attachments
41               * are handled by the mail mule message factory.
42               */
43  
44              // For this impl we just pass back the email content
45              Object result = msg.getContent();
46              if (result instanceof String)
47              {
48                  return result;
49              }
50              else
51              {
52                  // very simplisitic, only gets first part
53                  MimeMultipart part = (MimeMultipart)result;
54                  String transMsg = (String) part.getBodyPart(0).getContent();
55                  return transMsg;
56              }
57          }
58          catch (Exception e)
59          {
60              throw new TransformerException(this, e);
61          }
62      }
63  }