1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.mail; |
18 |
|
|
19 |
|
import java.util.Iterator; |
20 |
|
import java.util.Map; |
21 |
|
import java.util.Set; |
22 |
|
|
23 |
|
import javax.mail.Address; |
24 |
|
import javax.mail.Message; |
25 |
|
import javax.mail.MessagingException; |
26 |
|
import javax.mail.internet.InternetAddress; |
27 |
|
import javax.mail.internet.MimeMessage; |
28 |
|
|
29 |
|
import org.apache.camel.Exchange; |
30 |
|
import org.apache.camel.converter.ObjectConverter; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
9 |
public class MailBinding { |
39 |
|
public void populateMailMessage(MailEndpoint endpoint, MimeMessage mimeMessage, Exchange exchange) { |
40 |
|
try { |
41 |
3 |
appendHeadersFromCamel(mimeMessage, exchange, exchange.getIn()); |
42 |
|
|
43 |
3 |
String destination = endpoint.getConfiguration().getDestination(); |
44 |
3 |
if (destination != null) { |
45 |
3 |
mimeMessage.setRecipients(Message.RecipientType.TO, destination); |
46 |
|
} |
47 |
|
|
48 |
3 |
if (empty(mimeMessage.getFrom())) { |
49 |
|
|
50 |
1 |
String from = endpoint.getConfiguration().getFrom(); |
51 |
1 |
mimeMessage.setFrom(new InternetAddress(from)); |
52 |
|
} |
53 |
3 |
mimeMessage.setText(exchange.getIn().getBody(String.class)); |
54 |
0 |
} catch (Exception e) { |
55 |
0 |
throw new RuntimeMailException("Failed to populate body due to: " + e + ". Exchange: " + exchange, e); |
56 |
3 |
} |
57 |
3 |
} |
58 |
|
|
59 |
|
protected boolean empty(Address[] addresses) { |
60 |
3 |
return addresses == null || addresses.length == 0; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
public Object extractBodyFromMail(MailExchange exchange, Message message) { |
70 |
|
try { |
71 |
6 |
return message.getContent(); |
72 |
0 |
} catch (Exception e) { |
73 |
0 |
throw new RuntimeMailException("Failed to extract body due to: " + e + ". Message: " + message, e); |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
protected void appendHeadersFromCamel(MimeMessage mimeMessage, Exchange exchange, org.apache.camel.Message camelMessage) throws MessagingException { |
81 |
3 |
Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet(); |
82 |
3 |
for (Map.Entry<String, Object> entry : entries) { |
83 |
17 |
String headerName = entry.getKey(); |
84 |
17 |
Object headerValue = entry.getValue(); |
85 |
17 |
if (headerValue != null) { |
86 |
17 |
if (shouldOutputHeader(camelMessage, headerName, headerValue)) { |
87 |
|
|
88 |
|
|
89 |
17 |
if (ObjectConverter.isCollection(headerValue)) { |
90 |
0 |
Iterator iter = ObjectConverter.iterator(headerValue); |
91 |
0 |
while (iter.hasNext()) { |
92 |
0 |
Object value = iter.next(); |
93 |
0 |
mimeMessage.addHeader(headerName, asString(exchange, value)); |
94 |
0 |
} |
95 |
0 |
} else { |
96 |
17 |
mimeMessage.setHeader(headerName, asString(exchange, headerValue)); |
97 |
|
} |
98 |
|
} |
99 |
|
} |
100 |
17 |
} |
101 |
3 |
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
protected String asString(Exchange exchange, Object value) { |
107 |
17 |
return exchange.getContext().getTypeConverter().convertTo(String.class, value); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
protected boolean shouldOutputHeader(org.apache.camel.Message camelMessage, String headerName, Object headerValue) { |
114 |
17 |
return true; |
115 |
|
} |
116 |
|
} |