1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.component.mail; |
19 |
|
|
20 |
|
import org.apache.camel.Consumer; |
21 |
|
import org.apache.camel.Processor; |
22 |
|
import org.apache.camel.Producer; |
23 |
|
import org.apache.camel.impl.ScheduledPollEndpoint; |
24 |
|
import org.springframework.mail.javamail.JavaMailSender; |
25 |
|
|
26 |
|
import javax.mail.Message; |
27 |
|
import javax.mail.Folder; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
1 |
public class MailEndpoint extends ScheduledPollEndpoint<MailExchange> { |
33 |
|
private MailBinding binding; |
34 |
|
private MailConfiguration configuration; |
35 |
|
|
36 |
|
public MailEndpoint(String uri, MailComponent component, MailConfiguration configuration) { |
37 |
12 |
super(uri, component); |
38 |
12 |
this.configuration = configuration; |
39 |
12 |
} |
40 |
|
|
41 |
|
public Producer<MailExchange> createProducer() throws Exception { |
42 |
3 |
JavaMailSender sender = configuration.createJavaMailConnection(this); |
43 |
3 |
return createProducer(sender); |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public Producer<MailExchange> createProducer(JavaMailSender sender) throws Exception { |
50 |
3 |
return new MailProducer(this, sender); |
51 |
|
} |
52 |
|
|
53 |
|
public Consumer<MailExchange> createConsumer(Processor processor) throws Exception { |
54 |
4 |
JavaMailConnection connection = configuration.createJavaMailConnection(this); |
55 |
4 |
String protocol = getConfiguration().getProtocol(); |
56 |
4 |
if (protocol.equals("smtp")) { |
57 |
4 |
protocol = "pop3"; |
58 |
|
} |
59 |
4 |
String folderName = getConfiguration().getFolderName(); |
60 |
4 |
Folder folder = connection.getFolder(protocol, folderName); |
61 |
4 |
if (folder == null) { |
62 |
0 |
throw new IllegalArgumentException("No folder for protocol: " + protocol + " and name: " + folderName); |
63 |
|
} |
64 |
4 |
return createConsumer(processor, folder); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public Consumer<MailExchange> createConsumer(Processor processor, Folder folder) throws Exception { |
76 |
4 |
MailConsumer answer = new MailConsumer(this, processor, folder); |
77 |
4 |
configureConsumer(answer); |
78 |
4 |
return answer; |
79 |
|
} |
80 |
|
|
81 |
|
public MailExchange createExchange() { |
82 |
1 |
return new MailExchange(getContext(), getBinding()); |
83 |
|
} |
84 |
|
|
85 |
|
public MailExchange createExchange(Message message) { |
86 |
6 |
return new MailExchange(getContext(), getBinding(), message); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
public MailBinding getBinding() { |
92 |
10 |
if (binding == null) { |
93 |
9 |
binding = new MailBinding(); |
94 |
|
} |
95 |
10 |
return binding; |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
public void setBinding(MailBinding binding) { |
104 |
0 |
this.binding = binding; |
105 |
0 |
} |
106 |
|
|
107 |
|
public boolean isSingleton() { |
108 |
12 |
return false; |
109 |
|
} |
110 |
|
|
111 |
|
public MailConfiguration getConfiguration() { |
112 |
31 |
return configuration; |
113 |
|
} |
114 |
|
} |