View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import org.apache.mailet.MailAddress;
21  
22  import javax.mail.MessagingException;
23  import javax.mail.internet.InternetAddress;
24  import java.util.HashSet;
25  import java.util.Collection;
26  
27  /***
28   * <P>Sends a notification message to the Postmaster.</P>
29   * <P>A sender of the notification message can optionally be specified.
30   * If one is not specified, the postmaster's address will be used.<BR>
31   * The "To:" header of the notification message can be set to "unaltered";
32   * if missing will be set to the postmaster.<BR>
33   * A notice text can be specified, and in such case will be inserted into the
34   * notification inline text.<BR>
35   * If the notified message has an "error message" set, it will be inserted into the
36   * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
37   * is set to true, such error message will be attached to the notification message.<BR>
38   * The notified messages are attached in their entirety (headers and
39   * content) and the resulting MIME part type is "message/rfc822".</P>
40   * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
41   *
42   * <P>Sample configuration:</P>
43   * <PRE><CODE>
44   * &lt;mailet match="All" class="NotifyPostmaster">
45   *   &lt;sender&gt;<I>an address or postmaster or sender or unaltered, default=postmaster</I>&lt;/sender&gt;
46   *   &lt;attachError&gt;<I>true or false, default=false</I>&lt;/attachError&gt;
47   *   &lt;message&gt;<I>notice attached to the original message text (optional)</I>&lt;/message&gt;
48   *   &lt;prefix&gt;<I>optional subject prefix prepended to the original message, default="Re:"</I>&lt;/prefix&gt;
49   *   &lt;inline&gt;<I>see {@link Resend}, default=none</I>&lt;/inline&gt;
50   *   &lt;attachment&gt;<I>see {@link Resend}, default=message</I>&lt;/attachment&gt;
51   *   &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
52   *   &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
53   *   &lt;to&gt;<I>unaltered (optional, defaults to postmaster)</I>&lt;/to&gt;
54   *   &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
55   * &lt;/mailet&gt;
56   * </CODE></PRE>
57   *
58   * <P>The behaviour of this mailet is equivalent to using Resend with the following
59   * configuration:</P>
60   * <PRE><CODE>
61   * &lt;mailet match="All" class="Resend">
62   *   &lt;sender&gt;<I>an address or postmaster or sender or unaltered</I>&lt;/sender&gt;
63   *   &lt;attachError&gt;<I>true or false</I>&lt;/attachError&gt;
64   *   &lt;message&gt;<I><B>dynamically built</B></I>&lt;/message&gt;
65   *   &lt;prefix&gt;<I>a string</I>&lt;/prefix&gt;
66   *   &lt;passThrough&gt;<I>true or false</I>&lt;/passThrough&gt;
67   *   &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
68   *   &lt;to&gt;<I><B>unaltered or postmaster</B></I>&lt;/to&gt;
69   *   &lt;recipients&gt;<B>postmaster</B>&lt;/recipients&gt;
70   *   &lt;inline&gt;see {@link Resend}&lt;/inline&gt;
71   *   &lt;attachment&gt;see {@link Resend}&lt;/attachment&gt;
72   *   &lt;isReply&gt;true&lt;/isReply&gt;
73   *   &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
74   * &lt;/mailet&gt;
75   * </CODE></PRE>
76   * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
77   * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
78   *
79   * @version CVS $Revision: 382444 $ $Date: 2006-03-02 16:56:32 +0000 (gio, 02 mar 2006) $
80   */
81  public class NotifyPostmaster extends AbstractNotify {
82  
83      /***
84       * Return a string describing this mailet.
85       *
86       * @return a string describing this mailet
87       */
88      public String getMailetInfo() {
89          return "NotifyPostmaster Mailet";
90      }
91  
92      /*** Gets the expected init parameters. */
93      protected  String[] getAllowedInitParameters() {
94          String[] allowedArray = {
95  //            "static",
96              "debug",
97              "passThrough",
98              "fakeDomainCheck",
99              "inline",
100             "attachment",
101             "message",
102             "notice",
103             "sender",
104             "sendingAddress",
105             "prefix",
106             "attachError",
107             "attachStackTrace",
108             "to"
109         };
110         return allowedArray;
111     }
112     
113     /* ******************************************************************** */
114     /* ****************** Begin of getX and setX methods ****************** */
115     /* ******************************************************************** */
116 
117     /***
118      * @return the postmaster address
119      */
120     protected Collection getRecipients() {
121         Collection newRecipients = new HashSet();
122         newRecipients.add(getMailetContext().getPostmaster());
123         return newRecipients;
124     }
125 
126     /***
127      * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
128      */
129     protected InternetAddress[] getTo() throws MessagingException {
130         String addressList = getInitParameter("to");
131         InternetAddress[] iaarray = new InternetAddress[1];
132         iaarray[0] = getMailetContext().getPostmaster().toInternetAddress();
133         if (addressList != null) {
134             MailAddress specialAddress = getSpecialAddress(addressList,
135                                             new String[] {"postmaster", "unaltered"});
136             if (specialAddress != null) {
137                 iaarray[0] = specialAddress.toInternetAddress();
138             } else {
139                 log("\"to\" parameter ignored, set to postmaster");
140             }
141         }
142         return iaarray;
143     }
144 
145     /***
146      * @return the <CODE>attachStackTrace</CODE> init parameter, 
147      * or the <CODE>attachError</CODE> init parameter if missing,
148      * or false if missing 
149      */
150     protected boolean attachError() throws MessagingException {
151         String parameter = getInitParameter("attachStackTrace");
152         if (parameter == null) {
153             return super.attachError();
154         }        
155         return new Boolean(parameter).booleanValue();
156     }
157 
158     /* ******************************************************************** */
159     /* ******************* End of getX and setX methods ******************* */
160     /* ******************************************************************** */
161 
162 }
163