1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.util.check;
21
22 import java.io.IOException;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.ListIterator;
30
31 import javax.mail.Header;
32 import javax.mail.Message;
33 import javax.mail.MessagingException;
34
35 import org.apache.jsieve.exception.SieveException;
36 import org.apache.jsieve.mail.Action;
37 import org.apache.jsieve.mail.MailAdapter;
38 import org.apache.jsieve.mail.MailUtils;
39 import org.apache.jsieve.mail.SieveMailException;
40 import org.apache.jsieve.parser.address.SieveAddressBuilder;
41 import org.apache.jsieve.parser.generated.address.ParseException;
42
43
44
45
46
47
48 public class ScriptCheckMailAdapter implements MailAdapter {
49
50 private final List actions;
51
52 private final List executedActions;
53
54 private Message mail = null;
55
56 public ScriptCheckMailAdapter() {
57 actions = new ArrayList();
58 executedActions = new ArrayList();
59 }
60
61
62
63
64
65
66 public Message getMail() {
67 return mail;
68 }
69
70
71
72
73
74
75
76
77 public void setMail(Message mail) {
78 this.mail = mail;
79 reset();
80 }
81
82
83
84
85
86
87
88 public void addAction(final Action action) {
89 actions.add(action);
90 }
91
92
93
94
95 public void executeActions() throws SieveException {
96 executedActions.clear();
97 executedActions.addAll(actions);
98 }
99
100
101
102
103
104
105
106
107 public List getExecutedActions() {
108 final ArrayList result = new ArrayList(executedActions);
109 return result;
110 }
111
112
113
114
115
116
117
118
119 public List getActions() {
120 final List result = Collections.unmodifiableList(actions);
121 return result;
122 }
123
124
125
126
127
128
129
130
131 public ListIterator getActionsIterator() {
132 final List unmodifiableActions = getActions();
133 final ListIterator result = unmodifiableActions.listIterator();
134 return result;
135 }
136
137
138
139
140
141 public void reset() {
142 executedActions.clear();
143 actions.clear();
144 }
145
146
147
148
149
150
151
152
153
154
155 public List getHeader(String name) throws SieveMailException {
156 List result = Collections.EMPTY_LIST;
157 if (mail != null) {
158 try {
159 String[] values = mail.getHeader(name);
160 if (values != null) {
161 result = Arrays.asList(values);
162 }
163 } catch (MessagingException e) {
164 throw new SieveMailException(e);
165 }
166 }
167 return result;
168 }
169
170
171
172
173
174
175
176
177
178 public List getHeaderNames() throws SieveMailException {
179 List results = Collections.EMPTY_LIST;
180 if (mail != null) {
181 try {
182 results = new ArrayList();
183 for (final Enumeration en = mail.getAllHeaders(); en
184 .hasMoreElements();) {
185 final Header header = (Header) en.nextElement();
186 final String name = header.getName();
187 if (!results.contains(name)) {
188 results.add(name);
189 }
190 }
191 } catch (MessagingException e) {
192 throw new SieveMailException(e);
193 }
194 }
195 return results;
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 public List getMatchingHeader(String name) throws SieveMailException {
217 List result = Collections.EMPTY_LIST;
218 if (mail != null) {
219 result = MailUtils.getMatchingHeader(this, name);
220 }
221 return result;
222 }
223
224
225
226
227
228
229
230 public int getSize() throws SieveMailException {
231 int result = 0;
232 if (mail != null) {
233 try {
234 result = mail.getSize();
235 } catch (MessagingException e) {
236 throw new SieveMailException(e);
237 }
238 }
239 return result;
240 }
241
242
243
244
245
246
247
248
249 public String getContentType() throws SieveMailException {
250 String result = null;
251 if (mail != null) {
252 try {
253 result = mail.getContentType();
254 } catch (MessagingException e) {
255 throw new SieveMailException(e);
256 }
257 }
258 return result;
259 }
260
261
262
263
264
265
266
267 public Object getContent() throws SieveMailException {
268 Object result = null;
269 if (mail != null) {
270 try {
271 result = mail.getContent();
272 } catch (MessagingException e) {
273 throw new SieveMailException(e);
274 } catch (IOException e) {
275 throw new SieveMailException(e);
276 }
277 }
278 return result;
279 }
280
281 public Address[] parseAddresses(String headerName)
282 throws SieveMailException {
283 return parseAddresses(headerName, mail);
284 }
285
286
287
288
289
290
291
292
293
294
295
296 public Address[] parseAddresses(final String headerName,
297 final Message message) throws SieveMailException {
298 try {
299 final SieveAddressBuilder builder = new SieveAddressBuilder();
300
301 for (Enumeration en = message.getAllHeaders(); en.hasMoreElements();) {
302 final Header header = (Header) en.nextElement();
303 final String name = header.getName();
304 if (name.trim().equalsIgnoreCase(headerName)) {
305 builder.addAddresses(header.getValue());
306 }
307 }
308
309 final Address[] results = builder.getAddresses();
310 return results;
311
312 } catch (MessagingException ex) {
313 throw new SieveMailException(ex);
314 } catch (ParseException ex) {
315 throw new SieveMailException(ex);
316 }
317 }
318
319 }