1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | 0 | { |
29 | 0 | registerSourceType(byte[].class); |
30 | 0 | setReturnClass(MimeMessage.class); |
31 | 0 | } |
32 | |
|
33 | |
protected Object doTransform(Object src, String encoding) throws TransformerException |
34 | |
{ |
35 | |
try |
36 | |
{ |
37 | 0 | byte[] bytes = (byte[]) src; |
38 | 0 | return new MimeMessage(getSession(), new ByteArrayInputStream(bytes)); |
39 | |
} |
40 | 0 | catch (MessagingException e) |
41 | |
{ |
42 | 0 | throw new TransformerException(this, e); |
43 | |
} |
44 | |
} |
45 | |
|
46 | |
protected Session getSession() throws TransformerException |
47 | |
{ |
48 | 0 | if (null == endpoint) |
49 | |
{ |
50 | 0 | throw new TransformerException(this, |
51 | |
new IllegalStateException("The transformer is no associated with an endpoint.")); |
52 | |
} |
53 | 0 | UMOConnector connector = endpoint.getConnector(); |
54 | 0 | if (!(connector instanceof AbstractMailConnector)) |
55 | |
{ |
56 | 0 | throw new TransformerException(this, |
57 | |
new IllegalStateException("The transformer is not associated with an email endpoint.")); |
58 | |
} |
59 | 0 | return ((AbstractMailConnector) connector).getSessionDetails(endpoint).getSession(); |
60 | |
} |
61 | |
|
62 | |
} |