1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.email; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
18 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
19 | |
|
20 | |
import javax.mail.Flags; |
21 | |
import javax.mail.Folder; |
22 | |
import javax.mail.Message; |
23 | |
import javax.mail.MessagingException; |
24 | |
import javax.mail.Store; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class RetrieveMessageDispatcher extends AbstractMessageDispatcher |
34 | |
{ |
35 | |
private Folder folder; |
36 | |
|
37 | |
public RetrieveMessageDispatcher(UMOImmutableEndpoint endpoint) |
38 | |
{ |
39 | 0 | super(endpoint); |
40 | 0 | } |
41 | |
|
42 | |
private AbstractRetrieveMailConnector castConnector() |
43 | |
{ |
44 | 0 | return (AbstractRetrieveMailConnector) getConnector(); |
45 | |
} |
46 | |
|
47 | |
protected void doConnect() throws Exception |
48 | |
{ |
49 | 0 | if (folder == null || !folder.isOpen()) |
50 | |
{ |
51 | |
|
52 | 0 | Store store = castConnector().getSessionDetails(endpoint).newStore(); |
53 | |
|
54 | 0 | UMOEndpointURI uri = endpoint.getEndpointURI(); |
55 | 0 | store.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword()); |
56 | |
|
57 | 0 | folder = store.getFolder(castConnector().getMailboxFolder()); |
58 | 0 | if (!folder.isOpen()) |
59 | |
{ |
60 | |
try |
61 | |
{ |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | folder.open(Folder.READ_WRITE); |
67 | |
} |
68 | 0 | catch (MessagingException e) |
69 | |
{ |
70 | 0 | logger.warn("Failed to open folder: " + folder.getFullName(), e); |
71 | 0 | } |
72 | |
} |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
protected void doDisconnect() throws Exception |
77 | |
{ |
78 | |
|
79 | |
try |
80 | |
{ |
81 | 0 | if (folder != null) |
82 | |
{ |
83 | |
try |
84 | |
{ |
85 | 0 | folder.expunge(); |
86 | |
} |
87 | 0 | catch (MessagingException e) |
88 | |
{ |
89 | 0 | if (logger.isDebugEnabled()) |
90 | |
{ |
91 | 0 | logger.debug("ignoring exception on expunge: " + e.getMessage()); |
92 | |
} |
93 | 0 | } |
94 | 0 | if (folder.isOpen()) |
95 | |
{ |
96 | 0 | folder.close(true); |
97 | |
} |
98 | |
} |
99 | |
} |
100 | 0 | catch (Exception e) |
101 | |
{ |
102 | 0 | logger.error("Failed to close inbox: " + e.getMessage(), e); |
103 | 0 | } |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected void doDispatch(UMOEvent event) throws Exception |
111 | |
{ |
112 | 0 | throw new UnsupportedOperationException("Cannot dispatch from a Pop3 connection"); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
121 | |
{ |
122 | 0 | throw new UnsupportedOperationException("Cannot send from a Pop3 connection"); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
protected UMOMessage doReceive(long timeout) throws Exception |
138 | |
{ |
139 | 0 | long t0 = System.currentTimeMillis(); |
140 | 0 | if (timeout < 0) |
141 | |
{ |
142 | 0 | timeout = Long.MAX_VALUE; |
143 | |
} |
144 | |
|
145 | |
do |
146 | |
{ |
147 | 0 | if (hasMessages(folder)) |
148 | |
{ |
149 | 0 | int count = getMessageCount(folder); |
150 | 0 | if (count > 0) |
151 | |
{ |
152 | 0 | Message message = getNextMessage(folder); |
153 | |
|
154 | 0 | flagMessage(folder, message); |
155 | |
|
156 | 0 | return new MuleMessage(castConnector().getMessageAdapter(message)); |
157 | |
} |
158 | 0 | else if (count == -1) |
159 | |
{ |
160 | 0 | throw new MessagingException("Cannot monitor folder: " + folder.getFullName() |
161 | |
+ " as folder is closed"); |
162 | |
} |
163 | |
} |
164 | |
|
165 | 0 | long sleep = |
166 | |
Math.min(castConnector().getCheckFrequency(), |
167 | |
timeout - (System.currentTimeMillis() - t0)); |
168 | |
|
169 | 0 | if (sleep > 0) |
170 | |
{ |
171 | 0 | if (logger.isDebugEnabled()) |
172 | |
{ |
173 | 0 | logger.debug("No results, sleeping for " + sleep); |
174 | |
} |
175 | 0 | Thread.sleep(sleep); |
176 | |
} |
177 | |
else |
178 | |
{ |
179 | |
|
180 | 0 | logger.debug("Timeout"); |
181 | 0 | return null; |
182 | |
} |
183 | |
|
184 | |
} |
185 | 0 | while (true); |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
protected void flagMessage(Folder folder, Message message) throws MessagingException |
197 | |
{ |
198 | 0 | message.setFlag(Flags.Flag.DELETED, true); |
199 | 0 | } |
200 | |
|
201 | |
protected Message getNextMessage(Folder folder) throws MessagingException |
202 | |
{ |
203 | 0 | return folder.getMessage(1); |
204 | |
} |
205 | |
|
206 | |
protected int getMessageCount(Folder folder) throws MessagingException |
207 | |
{ |
208 | 0 | return folder.getMessageCount(); |
209 | |
} |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
protected boolean hasMessages(Folder folder) throws MessagingException |
221 | |
{ |
222 | 0 | return getMessageCount(folder) > 0; |
223 | |
} |
224 | |
|
225 | |
protected void doDispose() |
226 | |
{ |
227 | 0 | if (null != folder && folder.isOpen()) |
228 | |
{ |
229 | |
try |
230 | |
{ |
231 | |
|
232 | 0 | folder.close(true); |
233 | |
} |
234 | 0 | catch (Exception e) |
235 | |
{ |
236 | 0 | logger.debug("ignoring exception: " + e.getMessage(), e); |
237 | 0 | } |
238 | |
} |
239 | 0 | } |
240 | |
|
241 | |
} |