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