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 sender of a message.</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 sender of the notified message.<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="NotifySender">
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;prefix&gt;<I>optional subject prefix prepended to the original message</I>&lt;/prefix&gt;
48   *   &lt;inline&gt;<I>see {@link Resend}, default=none</I>&lt;/inline&gt;
49   *   &lt;attachment&gt;<I>see {@link Resend}, default=message</I>&lt;/attachment&gt;
50   *   &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
51   *   &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
52   *   &lt;to&gt;<I>unaltered or sender or from(optional, defaults to sender)</I>&lt;/to&gt;
53   *   &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
54   * &lt;/mailet&gt;
55   * </CODE></PRE>
56   *
57   * <P>The behaviour of this mailet is equivalent to using Resend with the following
58   * configuration:</P>
59   * <PRE><CODE>
60   * &lt;mailet match="All" class="Resend">
61   *   &lt;sender&gt;<I>an address or postmaster or sender or unaltered</I>&lt;/sender&gt;
62   *   &lt;attachError&gt;<I>true or false</I>&lt;/attachError&gt;
63   *   &lt;message&gt;<I><B>dynamically built</B></I>&lt;/message&gt;
64   *   &lt;prefix&gt;<I>a string</I>&lt;/prefix&gt;
65   *   &lt;passThrough&gt;true&lt;/passThrough&gt;
66   *   &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
67   *   &lt;to&gt;<I>unaltered or sender or from&lt</I>;/to&gt;
68   *   &lt;recipients&gt;<B>sender</B>&lt;/recipients&gt;
69   *   &lt;inline&gt;none&lt;/inline&gt;
70   *   &lt;attachment&gt;message&lt;/attachment&gt;
71   *   &lt;isReply&gt;true&lt;/isReply&gt;
72   *   &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
73   * &lt;/mailet&gt;
74   * </CODE></PRE>
75   * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
76   * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
77   *
78   * @version CVS $Revision: 382444 $ $Date: 2006-03-02 16:56:32 +0000 (gio, 02 mar 2006) $
79   */
80  public class NotifySender extends AbstractNotify {
81  
82      /***
83       * Return a string describing this mailet.
84       *
85       * @return a string describing this mailet
86       */
87      public String getMailetInfo() {
88          return "NotifySender Mailet";
89      }
90  
91      /*** Gets the expected init parameters. */
92      protected  String[] getAllowedInitParameters() {
93          String[] allowedArray = {
94  //            "static",
95              "debug",
96              "passThrough",
97              "fakeDomainCheck",
98              "inline",
99              "attachment",
100             "message",
101             "notice",
102             "sender",
103             "sendingAddress",
104             "prefix",
105             "attachError",
106             "attachStackTrace",
107             "to"
108         };
109         return allowedArray;
110     }
111     
112     /* ******************************************************************** */
113     /* ****************** Begin of getX and setX methods ****************** */
114     /* ******************************************************************** */
115 
116     /***
117      * @return <CODE>SpecialAddress.SENDER</CODE>, indicating the sender of the current mail
118      */
119     protected Collection getRecipients() {
120         Collection newRecipients = new HashSet();
121         newRecipients.add(SpecialAddress.SENDER);
122         return newRecipients;
123     }
124 
125     /***
126      * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or <CODE>SpecialAddress.SENDER</CODE> if missing
127      */
128     protected InternetAddress[] getTo() throws MessagingException {
129         String addressList = getInitParameter("to");
130         InternetAddress[] iaarray = new InternetAddress[1];
131         iaarray[0] = SpecialAddress.SENDER.toInternetAddress();
132         if (addressList != null) {
133             MailAddress specialAddress = getSpecialAddress(addressList,
134                                             new String[] {"sender", "unaltered", "from"});
135             if (specialAddress != null) {
136                 iaarray[0] = specialAddress.toInternetAddress();
137             } else {
138                 log("\"to\" parameter ignored, set to sender");
139             }
140         }
141         return iaarray;
142     }
143 
144     /***
145      * @return the <CODE>attachStackTrace</CODE> init parameter, 
146      * or the <CODE>attachError</CODE> init parameter if missing,
147      * or false if missing 
148      */
149     protected boolean attachError() throws MessagingException {
150         String parameter = getInitParameter("attachStackTrace");
151         if (parameter == null) {
152             return super.attachError();
153         }        
154         return new Boolean(parameter).booleanValue();
155     }
156 
157     /* ******************************************************************** */
158     /* ******************* End of getX and setX methods ******************* */
159     /* ******************************************************************** */
160     
161 }
162