View Javadoc

1   /*
2    * $Id: Rfc822ByteArraytoMimeMessage.java 7976 2007-08-21 14:26:13Z 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.providers.email.AbstractMailConnector;
14  import org.mule.transformers.AbstractTransformer;
15  import org.mule.umo.provider.UMOConnector;
16  import org.mule.umo.transformer.TransformerException;
17  
18  import java.io.ByteArrayInputStream;
19  
20  import javax.mail.MessagingException;
21  import javax.mail.Session;
22  import javax.mail.internet.MimeMessage;
23  
24  public class Rfc822ByteArraytoMimeMessage extends AbstractTransformer
25  {
26  
27      public Rfc822ByteArraytoMimeMessage()
28      {
29          registerSourceType(byte[].class);
30          setReturnClass(MimeMessage.class);
31      }
32  
33      protected Object doTransform(Object src, String encoding) throws TransformerException
34      {
35          try
36          {
37              byte[] bytes = (byte[]) src;
38              return new MimeMessage(getSession(), new ByteArrayInputStream(bytes));
39          }
40          catch (MessagingException e)
41          {
42              throw new TransformerException(this, e);
43          }
44      }
45  
46      protected Session getSession() throws TransformerException
47      {
48          if (null == endpoint)
49          {
50              throw new TransformerException(this,
51                      new IllegalStateException("The transformer is no associated with an endpoint."));
52          }
53          UMOConnector connector = endpoint.getConnector();
54          if (!(connector instanceof AbstractMailConnector))
55          {
56              throw new TransformerException(this,
57                      new IllegalStateException("The transformer is not associated with an email endpoint."));
58          }
59          return ((AbstractMailConnector) connector).getSessionDetails(endpoint).getSession();
60      }
61  
62  }