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