1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.email.transformers; |
12 | |
|
13 | |
import org.mule.api.transformer.TransformerException; |
14 | |
import org.mule.api.transport.Connector; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.transformer.AbstractTransformer; |
17 | |
import org.mule.transport.email.AbstractMailConnector; |
18 | |
|
19 | |
import java.io.ByteArrayInputStream; |
20 | |
import java.io.InputStream; |
21 | |
|
22 | |
import javax.mail.MessagingException; |
23 | |
import javax.mail.Session; |
24 | |
import javax.mail.internet.MimeMessage; |
25 | |
|
26 | |
public class Rfc822ByteArraytoMimeMessage extends AbstractTransformer |
27 | |
{ |
28 | |
|
29 | |
public Rfc822ByteArraytoMimeMessage() |
30 | 4 | { |
31 | 4 | registerSourceType(byte[].class); |
32 | 4 | registerSourceType(InputStream.class); |
33 | 4 | setReturnClass(MimeMessage.class); |
34 | 4 | } |
35 | |
|
36 | |
protected Object doTransform(Object src, String encoding) throws TransformerException |
37 | |
{ |
38 | |
try |
39 | |
{ |
40 | 4 | if (src instanceof byte[]) |
41 | |
{ |
42 | 4 | byte[] bytes = (byte[]) src; |
43 | 4 | return new MimeMessage(getSession(), new ByteArrayInputStream(bytes)); |
44 | |
} |
45 | 0 | else if (src instanceof InputStream) |
46 | |
{ |
47 | 0 | return new MimeMessage(getSession(), (InputStream)src); |
48 | |
} |
49 | |
else |
50 | |
{ |
51 | 0 | throw new TransformerException( |
52 | |
CoreMessages.transformOnObjectUnsupportedTypeOfEndpoint(this.getName(), src.getClass(), endpoint)); |
53 | |
} |
54 | |
} |
55 | 0 | catch (MessagingException e) |
56 | |
{ |
57 | 0 | throw new TransformerException(this, e); |
58 | |
} |
59 | |
} |
60 | |
|
61 | |
protected Session getSession() throws TransformerException |
62 | |
{ |
63 | 4 | if (null == endpoint) |
64 | |
{ |
65 | 0 | throw new TransformerException(this, |
66 | |
new IllegalStateException("The transformer is no associated with an endpoint.")); |
67 | |
} |
68 | 4 | Connector connector = endpoint.getConnector(); |
69 | 4 | if (!(connector instanceof AbstractMailConnector)) |
70 | |
{ |
71 | 0 | throw new TransformerException(this, |
72 | |
new IllegalStateException("The transformer is not associated with an email endpoint.")); |
73 | |
} |
74 | 4 | return ((AbstractMailConnector) connector).getSessionDetails(endpoint).getSession(); |
75 | |
} |
76 | |
|
77 | |
} |