View Javadoc

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.james.transport.mailets;
21  
22  import java.util.Collection;
23  import java.util.HashSet;
24  
25  
26  import javax.mail.MessagingException;
27  import javax.mail.internet.InternetAddress;
28  
29  import org.apache.mailet.Mail;
30  import org.apache.mailet.MailAddress;
31  
32  
33  /***
34   * <P>A mailet providing configurable redirection services.</P>
35   * <P>Can produce listserver, forward and notify behaviour, with the original
36   * message intact, attached, appended or left out altogether.</P>
37   * <P>It differs from {@link Resend} because
38   * (i) some defaults are different,
39   * notably for the following parameters: <I>&lt;recipients&gt;</I>, <I>&lt;to&gt;</I>,
40   * <I>&lt;reversePath&gt;</I> and <I>&lt;inline&gt;</I>;
41   * (ii) because it allows the use of the <I>&lt;static&gt;</I> parameter;.<BR>
42   * Use <CODE>Resend</CODE> if you need full control, <CODE>Redirect</CODE> if
43   * the more automatic behaviour of some parameters is appropriate.</P>
44   * <P>This built in functionality is controlled by the configuration as laid out below.
45   * In the table please note that the parameters controlling message headers
46   * accept the <B>&quot;unaltered&quot;</B> value, whose meaning is to keep the associated
47   * header unchanged and, unless stated differently, corresponds to the assumed default
48   * if the parameter is missing.</P>
49   * <P>The configuration parameters are:</P>
50   * <TABLE width="75%" border="1" cellspacing="2" cellpadding="2">
51   * <TR valign=top>
52   * <TD width="20%">&lt;recipients&gt;</TD>
53   * <TD width="80%">
54   * A comma delimited list of addresses for recipients of
55   * this message; it will use the &quot;to&quot; list if not specified, and &quot;unaltered&quot;
56   * if none of the lists is specified.<BR>
57   * These addresses will only appear in the To: header if no &quot;to&quot; list is
58   * supplied.<BR>
59   * Such addresses can contain &quot;full names&quot;, like
60   * <I>Mr. John D. Smith &lt;john.smith@xyz.com&gt;</I>.<BR>
61   * The list can include constants &quot;sender&quot;, &quot;from&quot;, &quot;replyTo&quot;, &quot;postmaster&quot;, &quot;reversePath&quot;, &quot;recipients&quot;, &quot;to&quot;, &quot;null&quot; and &quot;unaltered&quot;;
62   * &quot;replyTo&quot; uses the ReplyTo header if available, otherwise the
63   * From header if available, otherwise the Sender header if available, otherwise the return-path;
64   * &quot;from&quot; is made equivalent to &quot;sender&quot;, and &quot;to&quot; is made equivalent to &quot;recipients&quot;;
65   * &quot;null&quot; is ignored.
66   * </TD>
67   * </TR>
68   * <TR valign=top>
69   * <TD width="20%">&lt;to&gt;</TD>
70   * <TD width="80%">
71   * A comma delimited list of addresses to appear in the To: header;
72   * the email will be delivered to any of these addresses if it is also in the recipients
73   * list.<BR>
74   * The recipients list will be used if this list is not supplied;
75   * if none of the lists is specified it will be &quot;unaltered&quot;.<BR>
76   * Such addresses can contain &quot;full names&quot;, like
77   * <I>Mr. John D. Smith &lt;john.smith@xyz.com&gt;</I>.<BR>
78   * The list can include constants &quot;sender&quot;, &quot;from&quot;, &quot;replyTo&quot;, &quot;postmaster&quot;, &quot;reversePath&quot;, &quot;recipients&quot;, &quot;to&quot;, &quot;null&quot; and &quot;unaltered&quot;;
79   * &quot;from&quot; uses the From header if available, otherwise the Sender header if available,
80   * otherwise the return-path;
81   * &quot;replyTo&quot; uses the ReplyTo header if available, otherwise the
82   * From header if available, otherwise the Sender header if available, otherwise the return-path;
83   * &quot;recipients&quot; is made equivalent to &quot;to&quot;;
84   * if &quot;null&quot; is specified alone it will remove this header.
85   * </TD>
86   * </TR>
87   * <TR valign=top>
88   * <TD width="20%">&lt;sender&gt;</TD>
89   * <TD width="80%">
90   * A single email address to appear in the From: and Return-Path: headers and become the sender.<BR>
91   * It can include constants &quot;sender&quot;, &quot;postmaster&quot; and &quot;unaltered&quot;;
92   * &quot;sender&quot; is equivalent to &quot;unaltered&quot;.<BR>
93   * Default: &quot;unaltered&quot;.
94   * </TD>
95   * </TR>
96   * <TR valign=top>
97   * <TD width="20%">&lt;message&gt;</TD>
98   * <TD width="80%">
99   * A text message to insert into the body of the email.<BR>
100  * Default: no message is inserted.
101  * </TD>
102  * </TR>
103  * <TR valign=top>
104  * <TD width="20%">&lt;inline&gt;</TD>
105  * <TD width="80%">
106  * <P>One of the following items:</P>
107  * <UL>
108  * <LI>unaltered &nbsp;&nbsp;&nbsp;&nbsp;The original message is the new
109  * message, for forwarding/aliasing</LI>
110  * <LI>heads&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The
111  * headers of the original message are appended to the message</LI>
112  * <LI>body&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The
113  * body of the original is appended to the new message</LI>
114  * <LI>all&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Both
115  * headers and body are appended</LI>
116  * <LI>none&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Neither
117  * body nor headers are appended</LI>
118  * </UL>
119  * Default: &quot;body&quot;.
120  * </TD>
121  * </TR>
122  * <TR valign=top>
123  * <TD width="20%">&lt;attachment&gt;</TD>
124  * <TD width="80%">
125  * <P>One of the following items:</P>
126  * <UL>
127  * <LI>heads&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The headers of the original
128  * are attached as text</LI>
129  * <LI>body&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The body of the original
130  * is attached as text</LI>
131  * <LI>all&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Both
132  * headers and body are attached as a single text file</LI>
133  * <LI>none&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nothing is attached</LI>
134  * <LI>message &nbsp;The original message is attached as type message/rfc822,
135  * this means that it can, in many cases, be opened, resent, fw'd, replied
136  * to etc by email client software.</LI>
137  * </UL>
138  * Default: &quot;none&quot;.
139  * </TD>
140  * </TR>
141  * <TR valign=top>
142  * <TD width="20%">&lt;passThrough&gt;</TD>
143  * <TD width="80%">
144  * true or false, if true the original message continues in the
145  * mailet processor after this mailet is finished. False causes the original
146  * to be stopped.<BR>
147  * Default: false.
148  * </TD>
149  * </TR>
150  * <TR valign=top>
151  * <TD width="20%">&lt;fakeDomainCheck&gt;</TD>
152  * <TD width="80%">
153  * true or false, if true will check if the sender domain is valid.<BR>
154  * Default: true.
155  * </TD>
156  * </TR>
157  * <TR valign=top>
158  * <TD width="20%">&lt;attachError&gt;</TD>
159  * <TD width="80%">
160  * true or false, if true any error message available to the
161  * mailet is appended to the message body (except in the case of inline ==
162  * unaltered).<BR>
163  * Default: false.
164  * </TD>
165  * </TR>
166  * <TR valign=top>
167  * <TD width="20%">&lt;replyTo&gt;</TD>
168  * <TD width="80%">
169  * A single email address to appear in the Reply-To: header.<BR>
170  * It can include constants &quot;sender&quot;, &quot;postmaster&quot; &quot;null&quot; and &quot;unaltered&quot;;
171  * if &quot;null&quot; is specified it will remove this header.<BR>
172  * Default: &quot;unaltered&quot;.
173  * </TD>
174  * </TR>
175  * </TR>
176  * <TR valign=top>
177  * <TD width="20%">&lt;reversePath&gt;</TD>
178  * <TD width="80%">
179  * A single email address to appear in the Return-Path: header.<BR>
180  * It can include constants &quot;sender&quot;, &quot;postmaster&quot; and &quot;null&quot;;
181  * if &quot;null&quot; is specified then it will set it to <>, meaning &quot;null return path&quot;.<BR>
182  * Notice: the &quot;unaltered&quot; value is <I>not allowed</I>.<BR>
183  * Default: the value of the <I>&lt;sender&gt;</I> parameter, if set, otherwise remains unaltered.
184  * </TD>
185  * </TR>
186  * <TR valign=top>
187  * <TD width="20%">&lt;subject&gt;</TD>
188  * <TD width="80%">
189  * An optional string to use as the subject.<BR>
190  * Default: keep the original message subject.
191  * </TD>
192  * </TR>
193  * <TR valign=top>
194  * <TD width="20%">&lt;prefix&gt;</TD>
195  * <TD width="80%">
196  * An optional subject prefix prepended to the original message
197  * subject, or to a new subject specified with the <I>&lt;subject&gt;</I> parameter.<BR>
198  * For example: <I>[Undeliverable mail]</I>.<BR>
199  * Default: &quot;&quot;.
200  * </TD>
201  * </TR>
202  * <TR valign=top>
203  * <TD width="20%">&lt;isReply&gt;</TD>
204  * <TD width="80%">
205  * true or false, if true the IN_REPLY_TO header will be set to the
206  * id of the current message.<BR>
207  * Default: false.
208  * </TD>
209  * </TR>
210  * <TR valign=top>
211  * <TD width="20%">&lt;debug&gt;</TD>
212  * <TD width="80%">
213  * true or false.  If this is true it tells the mailet to write some debugging
214  * information to the mailet log.<BR>
215  * Default: false.
216  * </TD>
217  * </TR>
218  * <TR valign=top>
219  * <TD width="20%">&lt;static&gt;</TD>
220  * <TD width="80%">
221  * true or false.  If this is true it tells the mailet that it can
222  * reuse all the initial parameters (to, from, etc) without re-calculating
223  * their values.  This will boost performance where a redirect task
224  * doesn't contain any dynamic values.  If this is false, it tells the
225  * mailet to recalculate the values for each e-mail processed.<BR>
226  * Default: false.
227  * </TD>
228  * </TR>
229  * </TABLE>
230  *
231  * <P>Example:</P>
232  * <PRE><CODE>
233  *  &lt;mailet match=&quot;RecipientIs=test@localhost&quot; class=&quot;Redirect&quot;&gt;
234  *    &lt;recipients&gt;x@localhost, y@localhost, z@localhost&lt;/recipients&gt;
235  *    &lt;to&gt;list@localhost&lt;/to&gt;
236  *    &lt;sender&gt;owner@localhost&lt;/sender&gt;
237  *    &lt;message&gt;sent on from James&lt;/message&gt;
238  *    &lt;inline&gt;unaltered&lt;/inline&gt;
239  *    &lt;passThrough&gt;FALSE&lt;/passThrough&gt;
240  *    &lt;replyTo&gt;postmaster&lt;/replyTo&gt;
241  *    &lt;prefix xml:space="preserve"&gt;[test mailing] &lt;/prefix&gt;
242  *    &lt;!-- note the xml:space="preserve" to preserve whitespace --&gt;
243  *    &lt;static&gt;TRUE&lt;/static&gt;
244  * &lt;/mailet&gt;
245  * </CODE></PRE>
246  * 
247  * <P>and:</P>
248  *
249  * <PRE><CODE>
250  *  &lt;mailet match=&quot;All&quot; class=&quot;Redirect&quot;&gt;
251  *    &lt;recipients&gt;x@localhost&lt;/recipients&gt;
252  *    &lt;sender&gt;postmaster&lt;/sender&gt;
253  *    &lt;message xml:space="preserve"&gt;Message marked as spam:&lt;/message&gt;
254  *    &lt;inline&gt;heads&lt;/inline&gt;
255  *    &lt;attachment&gt;message&lt;/attachment&gt;
256  *    &lt;passThrough&gt;FALSE&lt;/passThrough&gt;
257  *    &lt;attachError&gt;TRUE&lt;/attachError&gt;
258  *    &lt;replyTo&gt;postmaster&lt;/replyTo&gt;
259  *    &lt;prefix&gt;[spam notification]&lt;/prefix&gt;
260  *    &lt;static&gt;TRUE&lt;/static&gt;
261  *  &lt;/mailet&gt;
262  * </CODE></PRE>
263  * <P><I>replyto</I> can be used instead of
264  * <I>replyTo</I>; such name is kept for backward compatibility.</P>
265  *
266  * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (lun, 08 gen 2007) $
267  */
268 
269 public class Redirect extends AbstractRedirect {
270 
271     /***
272      * Returns a string describing this mailet.
273      *
274      * @return a string describing this mailet
275      */
276     public String getMailetInfo() {
277         return "Redirect Mailet";
278     }
279 
280     /*** Gets the expected init parameters. */
281     protected  String[] getAllowedInitParameters() {
282         String[] allowedArray = {
283             "static",
284             "debug",
285             "passThrough",
286             "fakeDomainCheck",
287             "inline",
288             "attachment",
289             "message",
290             "recipients",
291             "to",
292             "replyTo",
293             "replyto",
294             "reversePath",
295             "sender",
296             "subject",
297             "prefix",
298             "attachError",
299             "isReply"
300         };
301         return allowedArray;
302     }
303 
304     /* ******************************************************************** */
305     /* ****************** Begin of getX and setX methods ****************** */
306     /* ******************************************************************** */
307 
308     /***
309      * @return the <CODE>static</CODE> init parameter
310     */
311     protected boolean isStatic() {
312         return isStatic;
313     }
314 
315     /***
316      * @return the <CODE>inline</CODE> init parameter
317      */
318     protected int getInLineType() throws MessagingException {
319         return getTypeCode(getInitParameter("inline","body"));
320     }
321 
322     /***
323      * @return the <CODE>recipients</CODE> init parameter
324      * or the postmaster address
325      * or <CODE>SpecialAddress.SENDER</CODE>
326      * or <CODE>SpecialAddress.REVERSE_PATH</CODE>
327      * or <CODE>SpecialAddress.UNALTERED</CODE>
328      * or the <CODE>to</CODE> init parameter if missing
329      * or <CODE>null</CODE> if also the latter is missing
330      */
331     protected Collection getRecipients() throws MessagingException {
332         Collection newRecipients = new HashSet();
333         String addressList = getInitParameter("recipients",getInitParameter("to"));
334                                  
335         // if nothing was specified, return <CODE>null</CODE> meaning no change
336         if (addressList == null) {
337             return null;
338         }
339 
340         try {
341             InternetAddress[] iaarray = InternetAddress.parse(addressList, false);
342             for (int i = 0; i < iaarray.length; i++) {
343                 String addressString = iaarray[i].getAddress();
344                 MailAddress specialAddress = getSpecialAddress(addressString,
345                 new String[] {"postmaster", "sender", "from", "replyTo", "reversePath", "unaltered", "recipients", "to", "null"});
346                 if (specialAddress != null) {
347                     newRecipients.add(specialAddress);
348                 } else {
349                     newRecipients.add(new MailAddress(iaarray[i]));
350                 }
351             }
352         } catch (Exception e) {
353             throw new MessagingException("Exception thrown in getRecipients() parsing: " + addressList, e);
354         }
355         if (newRecipients.size() == 0) {
356             throw new MessagingException("Failed to initialize \"recipients\" list; empty <recipients> init parameter found.");
357         }
358 
359         return newRecipients;
360     }
361 
362     /***
363      * @return the <CODE>to</CODE> init parameter
364      * or the postmaster address
365      * or <CODE>SpecialAddress.SENDER</CODE>
366      * or <CODE>SpecialAddress.REVERSE_PATH</CODE>
367      * or <CODE>SpecialAddress.UNALTERED</CODE>
368      * or the <CODE>recipients</CODE> init parameter if missing
369      * or <CODE>null</CODE> if also the latter is missing
370      */
371     protected InternetAddress[] getTo() throws MessagingException {
372         InternetAddress[] iaarray = null;
373         String addressList = getInitParameter("to",getInitParameter("recipients"));
374 
375         // if nothing was specified, return null meaning no change
376         if (addressList == null) {
377             return null;
378         }
379 
380         try {
381             iaarray = InternetAddress.parse(addressList, false);
382             for(int i = 0; i < iaarray.length; ++i) {
383                 String addressString = iaarray[i].getAddress();
384                 MailAddress specialAddress = getSpecialAddress(addressString,
385                                                 new String[] {"postmaster", "sender", "from", "replyTo", "reversePath", "unaltered", "recipients", "to", "null"});
386                 if (specialAddress != null) {
387                     iaarray[i] = specialAddress.toInternetAddress();
388                 }
389             }
390         } catch (Exception e) {
391             throw new MessagingException("Exception thrown in getTo() parsing: " + addressList, e);
392         }
393         if (iaarray.length == 0) {
394             throw new MessagingException("Failed to initialize \"to\" list; empty <to> init parameter found.");
395         }
396 
397         return iaarray;
398     }
399 
400     /***
401      * @return the <CODE>reversePath</CODE> init parameter 
402      * or the postmaster address
403      * or <CODE>SpecialAddress.SENDER</CODE>
404      * or <CODE>SpecialAddress.NULL</CODE>
405      * or <CODE>null</CODE> if missing
406      */
407     protected MailAddress getReversePath() throws MessagingException {
408         String addressString = getInitParameter("reversePath");
409         if(addressString != null) {
410             MailAddress specialAddress = getSpecialAddress(addressString,
411                                             new String[] {"postmaster", "sender", "null"});
412             if (specialAddress != null) {
413                 return specialAddress;
414             }
415 
416             try {
417                 return new MailAddress(addressString);
418             } catch(Exception e) {
419                 throw new MessagingException("Exception thrown in getReversePath() parsing: " + addressString, e);
420             }
421         }
422 
423         return null;
424     }
425 
426     /***
427      * @return {@link AbstractRedirect#getReversePath()};
428      * if null return {@link AbstractRedirect#getSender(Mail)},
429      * meaning the new requested sender if any
430      */
431     protected MailAddress getReversePath(Mail originalMail) throws MessagingException {
432         MailAddress reversePath = super.getReversePath(originalMail);
433         if (reversePath == null) {
434             reversePath = getSender(originalMail);
435         }
436         return reversePath;
437     }
438 
439     /* ******************************************************************** */
440     /* ******************* End of getX and setX methods ******************* */
441     /* ******************************************************************** */
442 
443 }