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 org.apache.mailet.MailAddress;
23  
24  import javax.mail.MessagingException;
25  import javax.mail.internet.InternetAddress;
26  import java.util.HashSet;
27  import java.util.Collection;
28  
29  /***
30   * <P>Sends a notification message to the Postmaster.</P>
31   * <P>A sender of the notification message can optionally be specified.
32   * If one is not specified, the postmaster's address will be used.<BR>
33   * The "To:" header of the notification message can be set to "unaltered";
34   * if missing will be set to the postmaster.<BR>
35   * A notice text can be specified, and in such case will be inserted into the
36   * notification inline text.<BR>
37   * If the notified message has an "error message" set, it will be inserted into the
38   * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
39   * is set to true, such error message will be attached to the notification message.<BR>
40   * The notified messages are attached in their entirety (headers and
41   * content) and the resulting MIME part type is "message/rfc822".</P>
42   * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
43   *
44   * <P>Sample configuration:</P>
45   * <PRE><CODE>
46   * &lt;mailet match="All" class="NotifyPostmaster">
47   *   &lt;sender&gt;<I>an address or postmaster or sender or unaltered, default=postmaster</I>&lt;/sender&gt;
48   *   &lt;attachError&gt;<I>true or false, default=false</I>&lt;/attachError&gt;
49   *   &lt;message&gt;<I>notice attached to the original message text (optional)</I>&lt;/message&gt;
50   *   &lt;prefix&gt;<I>optional subject prefix prepended to the original message, default="Re:"</I>&lt;/prefix&gt;
51   *   &lt;inline&gt;<I>see {@link Resend}, default=none</I>&lt;/inline&gt;
52   *   &lt;attachment&gt;<I>see {@link Resend}, default=message</I>&lt;/attachment&gt;
53   *   &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
54   *   &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
55   *   &lt;to&gt;<I>unaltered (optional, defaults to postmaster)</I>&lt;/to&gt;
56   *   &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
57   * &lt;/mailet&gt;
58   * </CODE></PRE>
59   *
60   * <P>The behaviour of this mailet is equivalent to using Resend with the following
61   * configuration:</P>
62   * <PRE><CODE>
63   * &lt;mailet match="All" class="Resend">
64   *   &lt;sender&gt;<I>an address or postmaster or sender or unaltered</I>&lt;/sender&gt;
65   *   &lt;attachError&gt;<I>true or false</I>&lt;/attachError&gt;
66   *   &lt;message&gt;<I><B>dynamically built</B></I>&lt;/message&gt;
67   *   &lt;prefix&gt;<I>a string</I>&lt;/prefix&gt;
68   *   &lt;passThrough&gt;<I>true or false</I>&lt;/passThrough&gt;
69   *   &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
70   *   &lt;to&gt;<I><B>unaltered or postmaster</B></I>&lt;/to&gt;
71   *   &lt;recipients&gt;<B>postmaster</B>&lt;/recipients&gt;
72   *   &lt;inline&gt;see {@link Resend}&lt;/inline&gt;
73   *   &lt;attachment&gt;see {@link Resend}&lt;/attachment&gt;
74   *   &lt;isReply&gt;true&lt;/isReply&gt;
75   *   &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
76   * &lt;/mailet&gt;
77   * </CODE></PRE>
78   * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
79   * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
80   *
81   * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
82   */
83  public class NotifyPostmaster extends AbstractNotify {
84  
85      /***
86       * Return a string describing this mailet.
87       *
88       * @return a string describing this mailet
89       */
90      public String getMailetInfo() {
91          return "NotifyPostmaster Mailet";
92      }
93  
94      /*** Gets the expected init parameters. */
95      protected  String[] getAllowedInitParameters() {
96          String[] allowedArray = {
97  //            "static",
98              "debug",
99              "passThrough",
100             "fakeDomainCheck",
101             "inline",
102             "attachment",
103             "message",
104             "notice",
105             "sender",
106             "sendingAddress",
107             "prefix",
108             "attachError",
109             "attachStackTrace",
110             "to"
111         };
112         return allowedArray;
113     }
114     
115     /* ******************************************************************** */
116     /* ****************** Begin of getX and setX methods ****************** */
117     /* ******************************************************************** */
118 
119     /***
120      * @return the postmaster address
121      */
122     protected Collection getRecipients() {
123         Collection newRecipients = new HashSet();
124         newRecipients.add(getMailetContext().getPostmaster());
125         return newRecipients;
126     }
127 
128     /***
129      * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
130      */
131     protected InternetAddress[] getTo() throws MessagingException {
132         String addressList = getInitParameter("to");
133         InternetAddress[] iaarray = new InternetAddress[1];
134         iaarray[0] = getMailetContext().getPostmaster().toInternetAddress();
135         if (addressList != null) {
136             MailAddress specialAddress = getSpecialAddress(addressList,
137                                             new String[] {"postmaster", "unaltered"});
138             if (specialAddress != null) {
139                 iaarray[0] = specialAddress.toInternetAddress();
140             } else {
141                 log("\"to\" parameter ignored, set to postmaster");
142             }
143         }
144         return iaarray;
145     }
146 
147     /***
148      * @return the <CODE>attachStackTrace</CODE> init parameter, 
149      * or the <CODE>attachError</CODE> init parameter if missing,
150      * or false if missing 
151      */
152     protected boolean attachError() throws MessagingException {
153         String parameter = getInitParameter("attachStackTrace");
154         if (parameter == null) {
155             return super.attachError();
156         }        
157         return new Boolean(parameter).booleanValue();
158     }
159 
160     /* ******************************************************************** */
161     /* ******************* End of getX and setX methods ******************* */
162     /* ******************************************************************** */
163 
164 }
165