View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email.transformers;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.api.transport.Connector;
11  import org.mule.config.i18n.CoreMessages;
12  import org.mule.transformer.AbstractTransformer;
13  import org.mule.transformer.types.DataTypeFactory;
14  import org.mule.transport.email.AbstractMailConnector;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.InputStream;
18  import java.io.UnsupportedEncodingException;
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(DataTypeFactory.BYTE_ARRAY);
30          registerSourceType(DataTypeFactory.INPUT_STREAM);
31          setReturnDataType(DataTypeFactory.create(MimeMessage.class));
32      }
33  
34      @Override
35      protected Object doTransform(Object src, String encoding) throws TransformerException
36      {
37          try
38          {
39              if (src instanceof byte[])
40              {
41                  byte[] bytes = (byte[]) src;
42                  return new MimeMessage(getSession(), new ByteArrayInputStream(bytes));
43              }
44              else if (src instanceof InputStream)
45              {
46                  return new MimeMessage(getSession(), (InputStream)src);
47              }
48              else
49              {
50                  throw new TransformerException(
51                      CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(this.getName(), src.getClass(), endpoint));
52              }
53          }
54          catch (MessagingException e)
55          {
56              throw new TransformerException(this, e);
57          }
58      }
59  
60      protected Session getSession() throws TransformerException
61      {
62          if (null == endpoint)
63          {
64              throw new TransformerException(this,
65                      new IllegalStateException("The transformer is no associated with an endpoint."));
66          }
67          Connector connector = endpoint.getConnector();
68          if (!(connector instanceof AbstractMailConnector))
69          {
70              throw new TransformerException(this,
71                      new IllegalStateException("The transformer is not associated with an email endpoint."));
72          }
73          try
74          {
75              return ((AbstractMailConnector) connector).getSessionDetails(endpoint).getSession();
76          }
77          catch (UnsupportedEncodingException e)
78          {
79              throw new TransformerException(this, e);
80          }
81      }
82  
83  }