1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.jsieve.samples.james;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ListIterator;
30 import java.util.Map;
31 import java.util.Set;
32 import javax.mail.Header;
33 import javax.mail.MessagingException;
34 import javax.mail.internet.MimeMessage;
35 import org.apache.jsieve.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.mail.optional.EnvelopeAccessors;
41 import org.apache.jsieve.parser.address.SieveAddressBuilder;
42 import org.apache.mailet.Mail;
43 import org.apache.mailet.MailAddress;
44 import org.apache.mailet.MailetContext;
45 /***
46 * <p>
47 * Class <code>SieveMailAdapter</code> implements a <code>MailAdapter</code>
48 * for use in a Mailet environment.
49 * </p>
50 */
51 public class SieveMailAdapter implements MailAdapter, EnvelopeAccessors
52 {
53 /***
54 * The Mail being adapted.
55 */
56 private Mail fieldMail;
57 /***
58 * The MailetContext.
59 */
60 private MailetContext fieldMailetContext;
61 /***
62 * List of Actions to perform.
63 */
64 private List fieldActions;
65 /***
66 * Constructor for SieveMailAdapter.
67 */
68 private SieveMailAdapter()
69 {
70 super();
71 }
72 /***
73 * Constructor for SieveMailAdapter.
74 *
75 * @param aMail
76 * @param aMailetContext
77 */
78 public SieveMailAdapter(Mail aMail, MailetContext aMailetContext)
79 {
80 this();
81 setMail(aMail);
82 setMailetContext(aMailetContext);
83 }
84 /***
85 * Returns the message.
86 *
87 * @return MimeMessage
88 */
89 protected MimeMessage getMessage() throws MessagingException
90 {
91 return getMail().getMessage();
92 }
93 /***
94 * Returns the List of actions.
95 *
96 * @return List
97 */
98 public List getActions()
99 {
100 List actions = null;
101 if (null == (actions = getActionsBasic()))
102 {
103 updateActions();
104 return getActions();
105 }
106 return actions;
107 }
108 /***
109 * Returns a new List of actions.
110 *
111 * @return List
112 */
113 protected List computeActions()
114 {
115 return new ArrayList();
116 }
117 /***
118 * Returns the List of actions.
119 *
120 * @return List
121 */
122 private List getActionsBasic()
123 {
124 return fieldActions;
125 }
126 /***
127 * Adds an Action.
128 *
129 * @param action The action to set
130 */
131 public void addAction(Action action)
132 {
133 getActions().add(action);
134 }
135 /***
136 * @see org.apache.jsieve.mail.MailAdapter#executeActions()
137 */
138 public void executeActions() throws SieveException
139 {
140
141
142 ListIterator actionsIter = getActionsIterator();
143 while (actionsIter.hasNext())
144 {
145 Action action = (Action) actionsIter.next();
146 getMailetContext().log("Executing action: " + action.toString());
147 try
148 {
149 ActionDispatcher.getInstance().execute(action, getMail(),
150 getMailetContext());
151 }
152 catch (NoSuchMethodException e)
153 {
154 throw new SieveException(e.getMessage());
155 }
156 catch (IllegalAccessException e)
157 {
158 throw new SieveException(e.getMessage());
159 }
160 catch (InvocationTargetException e)
161 {
162 throw new SieveException(e.getMessage());
163 }
164 catch (MessagingException e)
165 {
166 throw new SieveException(e.getMessage());
167 }
168 }
169 }
170 /***
171 * Sets the actions.
172 *
173 * @param actions The actions to set
174 */
175 protected void setActions(List actions)
176 {
177 fieldActions = actions;
178 }
179 /***
180 * Updates the actions.
181 */
182 protected void updateActions()
183 {
184 setActions(computeActions());
185 }
186 /***
187 * @see org.apache.jsieve.mail.MailAdapter#getActionsIterator()
188 */
189 public ListIterator getActionsIterator()
190 {
191 return getActions().listIterator();
192 }
193 /***
194 * @see org.apache.jsieve.mail.MailAdapter#getHeader(String)
195 */
196 public List getHeader(String name) throws SieveMailException
197 {
198 try
199 {
200 String[] headers = getMessage().getHeader(name);
201 return (headers == null ? new ArrayList(0) : Arrays.asList(headers));
202 }
203 catch (MessagingException ex)
204 {
205 throw new SieveMailException(ex);
206 }
207 }
208 /***
209 * @see org.apache.jsieve.mail.MailAdapter#getHeaderNames()
210 */
211 public List getHeaderNames() throws SieveMailException
212 {
213 Set headerNames = new HashSet();
214 try
215 {
216 Enumeration allHeaders = getMessage().getAllHeaders();
217 while (allHeaders.hasMoreElements())
218 {
219 headerNames.add(((Header) allHeaders.nextElement()).getName());
220 }
221 return new ArrayList(headerNames);
222 }
223 catch (MessagingException ex)
224 {
225 throw new SieveMailException(ex);
226 }
227 }
228 /***
229 * @see org.apache.jsieve.mail.MailAdapter#getMatchingHeader(String)
230 */
231 public List getMatchingHeader(String name) throws SieveMailException
232 {
233 return MailUtils.getMatchingHeader(this, name);
234 }
235 /***
236 * @see org.apache.jsieve.mail.MailAdapter#getSize()
237 */
238 public int getSize() throws SieveMailException
239 {
240 try
241 {
242 return getMessage().getSize();
243 }
244 catch (MessagingException ex)
245 {
246 throw new SieveMailException(ex);
247 }
248 }
249 /***
250 * Method getEnvelopes.
251 *
252 * @return Map
253 */
254 protected Map getEnvelopes()
255 {
256 Map envelopes = new HashMap(2);
257 if (null != getEnvelopeFrom())
258 envelopes.put("From", getEnvelopeFrom());
259 if (null != getEnvelopeTo())
260 envelopes.put("To", getEnvelopeTo());
261 return envelopes;
262 }
263 /***
264 * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getEnvelope(String)
265 */
266 public List getEnvelope(String name) throws SieveMailException
267 {
268 List values = new ArrayList(1);
269 Object value = getEnvelopes().get(name);
270 if (null != value)
271 values.add(value);
272 return values;
273 }
274 /***
275 * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getEnvelopeNames()
276 */
277 public List getEnvelopeNames() throws SieveMailException
278 {
279 return new ArrayList(getEnvelopes().keySet());
280 }
281 /***
282 * @see org.apache.jsieve.mail.optional.EnvelopeAccessors#getMatchingEnvelope(String)
283 */
284 public List getMatchingEnvelope(String name) throws SieveMailException
285 {
286 Iterator envelopeNamesIter = getEnvelopeNames().iterator();
287 List matchedEnvelopeValues = new ArrayList(32);
288 while (envelopeNamesIter.hasNext())
289 {
290 String envelopeName = (String) envelopeNamesIter.next();
291 if (envelopeName.trim().equalsIgnoreCase(name))
292 matchedEnvelopeValues.addAll(getEnvelope(envelopeName));
293 }
294 return matchedEnvelopeValues;
295 }
296 /***
297 * Returns the from.
298 *
299 * @return String
300 */
301 public String getEnvelopeFrom()
302 {
303 MailAddress sender = getMail().getSender();
304 return (null == sender ? "" : sender.toString());
305 }
306 /***
307 * Returns the sole recipient or null if there isn't one.
308 *
309 * @return String
310 */
311 public String getEnvelopeTo()
312 {
313 String recipient = null;
314 Iterator recipientIter = getMail().getRecipients().iterator();
315 if (recipientIter.hasNext())
316 recipient = (String) recipientIter.next().toString();
317 return recipient;
318 }
319 /***
320 * Returns the mail.
321 *
322 * @return Mail
323 */
324 public Mail getMail()
325 {
326 return fieldMail;
327 }
328 /***
329 * Sets the mail.
330 *
331 * @param mail The mail to set
332 */
333 protected void setMail(Mail mail)
334 {
335 fieldMail = mail;
336 }
337 /***
338 * Returns the mailetContext.
339 *
340 * @return MailetContext
341 */
342 public MailetContext getMailetContext()
343 {
344 return fieldMailetContext;
345 }
346 /***
347 * Sets the mailetContext.
348 *
349 * @param mailetContext The mailetContext to set
350 */
351 protected void setMailetContext(MailetContext mailetContext)
352 {
353 fieldMailetContext = mailetContext;
354 }
355 /***
356 * @see java.lang.Object#toString()
357 */
358 public String toString()
359 {
360 String messageID = null;
361 try
362 {
363 messageID = getMail().getMessage().getMessageID();
364 }
365 catch (MessagingException e)
366 {
367 messageID = "<" + e.getMessage() + ">";
368 }
369 return getClass().getName() + " Envelope From: "
370 + (null == getEnvelopeFrom() ? "null" : getEnvelopeFrom())
371 + " Envelope To: "
372 + (null == getEnvelopeTo() ? "null" : getEnvelopeTo())
373 + " Message ID: " + (null == messageID ? "null" : messageID);
374 }
375 public Object getContent() throws SieveMailException {
376
377 return null;
378 }
379 public String getContentType() throws SieveMailException {
380
381 return null;
382 }
383 public Address[] parseAddresses(String headerName) throws SieveMailException {
384 try {
385 return SieveAddressBuilder.parseAddresses(headerName, getMail().getMessage());
386 } catch (MessagingException e) {
387 throw new SieveMailException(e);
388 }
389 }
390 }