1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.email; |
12 | |
|
13 | |
import org.mule.providers.AbstractMessageAdapter; |
14 | |
import org.mule.umo.MessagingException; |
15 | |
|
16 | |
import java.util.Enumeration; |
17 | |
import java.util.HashMap; |
18 | |
import java.util.Map; |
19 | |
import java.util.TreeMap; |
20 | |
import java.util.Iterator; |
21 | |
|
22 | |
import javax.mail.Header; |
23 | |
import javax.mail.Message; |
24 | |
import javax.mail.Multipart; |
25 | |
import javax.mail.Part; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class MailMessageAdapter extends SimpleMailMessageAdapter |
35 | |
{ |
36 | |
|
37 | |
private static final long serialVersionUID = -6013198455030918360L; |
38 | |
public static final String ATTACHMENT_HEADERS_PROPERTY_POSTFIX = "Headers"; |
39 | |
|
40 | |
public MailMessageAdapter(Object object) throws MessagingException |
41 | |
{ |
42 | 0 | super(object); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
protected void handleMessage(Message message) throws Exception |
50 | |
{ |
51 | 0 | Object content = message.getContent(); |
52 | |
|
53 | 0 | if (content instanceof Multipart) |
54 | |
{ |
55 | 0 | TreeMap attachments = new TreeMap(); |
56 | 0 | MailUtils.getAttachments((Multipart)content, attachments); |
57 | |
|
58 | 0 | logger.debug("Received Multipart message"); |
59 | 0 | int i = 0; |
60 | 0 | for (Iterator iterator = attachments.entrySet().iterator(); iterator.hasNext();i++) |
61 | |
{ |
62 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
63 | 0 | Part part = (Part)entry.getValue(); |
64 | 0 | String name = entry.getKey().toString(); |
65 | 0 | if(i==0) |
66 | |
{ |
67 | 0 | setMessage(part); |
68 | |
} |
69 | |
else |
70 | |
{ |
71 | 0 | addAttachment(name, part.getDataHandler()); |
72 | 0 | addAttachmentHeaders(name, part); |
73 | |
} |
74 | |
|
75 | |
} |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | setMessage(message); |
80 | |
} |
81 | 0 | } |
82 | |
|
83 | |
protected void addAttachmentHeaders(String name, Part part) throws javax.mail.MessagingException |
84 | |
{ |
85 | 0 | Map headers = new HashMap(4); |
86 | 0 | for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();) |
87 | |
{ |
88 | 0 | Header h = (Header)e.nextElement(); |
89 | 0 | headers.put(h.getName(), h.getValue()); |
90 | |
} |
91 | 0 | if (headers.size() > 0) |
92 | |
{ |
93 | 0 | setProperty(name + ATTACHMENT_HEADERS_PROPERTY_POSTFIX, headers); |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
} |