KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > NotifyPostmaster


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailAddress;
23 import org.apache.mailet.MailetException;
24
25 import javax.mail.Address JavaDoc;
26 import javax.mail.Message JavaDoc;
27 import javax.mail.MessagingException JavaDoc;
28 import javax.mail.Session JavaDoc;
29 import javax.mail.internet.InternetAddress JavaDoc;
30 import javax.mail.internet.MimeBodyPart JavaDoc;
31 import javax.mail.internet.MimeMessage JavaDoc;
32 import javax.mail.internet.MimeMultipart JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.HashSet JavaDoc;
38 import java.util.Set JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * <P>Sends a notification message to the Postmaster.</P>
44  * <P>A sender of the notification message can optionally be specified.
45  * If one is not specified, the postmaster's address will be used.<BR>
46  * The "To:" header of the notification message can be set to "unaltered";
47  * if missing will be set to the postmaster.<BR>
48  * A notice text can be specified, and in such case will be inserted into the
49  * notification inline text.<BR>
50  * If the notified message has an "error message" set, it will be inserted into the
51  * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
52  * is set to true, such error message will be attached to the notification message.<BR>
53  * The notified messages are attached in their entirety (headers and
54  * content) and the resulting MIME part type is "message/rfc822".</P>
55  * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
56  *
57  * <P>Sample configuration:</P>
58  * <PRE><CODE>
59  * &lt;mailet match="All" class="NotifyPostmaster">
60  * &lt;sender&gt;<I>an address or postmaster or sender or unaltered, default=postmaster</I>&lt;/sender&gt;
61  * &lt;attachError&gt;<I>true or false, default=false</I>&lt;/attachError&gt;
62  * &lt;message&gt;<I>notice attached to the original message text (optional)</I>&lt;/message&gt;
63  * &lt;prefix&gt;<I>optional subject prefix prepended to the original message, default="Re:"</I>&lt;/prefix&gt;
64  * &lt;inline&gt;<I>see {@link Resend}, default=none</I>&lt;/inline&gt;
65  * &lt;attachment&gt;<I>see {@link Resend}, default=message</I>&lt;/attachment&gt;
66  * &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
67  * &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
68  * &lt;to&gt;<I>unaltered (optional, defaults to postmaster)</I>&lt;/to&gt;
69  * &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
70  * &lt;/mailet&gt;
71  * </CODE></PRE>
72  *
73  * <P>The behaviour of this mailet is equivalent to using Resend with the following
74  * configuration:</P>
75  * <PRE><CODE>
76  * &lt;mailet match="All" class="Resend">
77  * &lt;sender&gt;<I>an address or postmaster or sender or unaltered</I>&lt;/sender&gt;
78  * &lt;attachError&gt;<I>true or false</I>&lt;/attachError&gt;
79  * &lt;message&gt;<I><B>dynamically built</B></I>&lt;/message&gt;
80  * &lt;prefix&gt;<I>a string</I>&lt;/prefix&gt;
81  * &lt;passThrough&gt;<I>true or false</I>&lt;/passThrough&gt;
82  * &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
83  * &lt;to&gt;<I><B>unaltered or postmaster</B></I>&lt;/to&gt;
84  * &lt;recipients&gt;<B>postmaster</B>&lt;/recipients&gt;
85  * &lt;inline&gt;see {@link Resend}&lt;/inline&gt;
86  * &lt;attachment&gt;see {@link Resend}&lt;/attachment&gt;
87  * &lt;isReply&gt;true&lt;/isReply&gt;
88  * &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
89  * &lt;/mailet&gt;
90  * </CODE></PRE>
91  * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
92  * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
93  *
94  * @version CVS $Revision: 1.9.4.12 $ $Date: 2004/03/15 03:54:19 $
95  */

96 public class NotifyPostmaster extends AbstractNotify {
97
98     /**
99      * Return a string describing this mailet.
100      *
101      * @return a string describing this mailet
102      */

103     public String JavaDoc getMailetInfo() {
104         return "NotifyPostmaster Mailet";
105     }
106
107     /** Gets the expected init parameters. */
108     protected String JavaDoc[] getAllowedInitParameters() {
109         String JavaDoc[] allowedArray = {
110 // "static",
111
"debug",
112             "passThrough",
113             "fakeDomainCheck",
114             "inline",
115             "attachment",
116             "message",
117             "notice",
118             "sender",
119             "sendingAddress",
120             "prefix",
121             "attachError",
122             "attachStackTrace",
123             "to"
124         };
125         return allowedArray;
126     }
127     
128     /* ******************************************************************** */
129     /* ****************** Begin of getX and setX methods ****************** */
130     /* ******************************************************************** */
131
132     /**
133      * @return the postmaster address
134      */

135     protected Collection JavaDoc getRecipients() {
136         Collection JavaDoc newRecipients = new HashSet JavaDoc();
137         newRecipients.add(getMailetContext().getPostmaster());
138         return newRecipients;
139     }
140
141     /**
142      * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
143      */

144     protected InternetAddress JavaDoc[] getTo() throws MessagingException JavaDoc {
145         String JavaDoc addressList = getInitParameter("to");
146         InternetAddress JavaDoc[] iaarray = new InternetAddress JavaDoc[1];
147         iaarray[0] = getMailetContext().getPostmaster().toInternetAddress();
148         if (addressList != null) {
149             MailAddress specialAddress = getSpecialAddress(addressList,
150                                             new String JavaDoc[] {"postmaster", "unaltered"});
151             if (specialAddress != null) {
152                 iaarray[0] = specialAddress.toInternetAddress();
153             } else {
154                 log("\"to\" parameter ignored, set to postmaster");
155             }
156         }
157         return iaarray;
158     }
159
160     /**
161      * @return the <CODE>attachStackTrace</CODE> init parameter,
162      * or the <CODE>attachError</CODE> init parameter if missing,
163      * or false if missing
164      */

165     protected boolean attachError() throws MessagingException JavaDoc {
166         String JavaDoc parameter = getInitParameter("attachStackTrace");
167         if (parameter == null) {
168             return super.attachError();
169         }
170         return new Boolean JavaDoc(parameter).booleanValue();
171     }
172
173     /* ******************************************************************** */
174     /* ******************* End of getX and setX methods ******************* */
175     /* ******************************************************************** */
176
177 }
178
179
Popular Tags