KickJava   Java API By Example, From Geeks To Geeks.

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


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 sender of a message.</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 sender of the notified message.<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="NotifySender">
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;prefix&gt;<I>optional subject prefix prepended to the original message</I>&lt;/prefix&gt;
63  * &lt;inline&gt;<I>see {@link Resend}, default=none</I>&lt;/inline&gt;
64  * &lt;attachment&gt;<I>see {@link Resend}, default=message</I>&lt;/attachment&gt;
65  * &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
66  * &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
67  * &lt;to&gt;<I>unaltered or sender or from(optional, defaults to sender)</I>&lt;/to&gt;
68  * &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
69  * &lt;/mailet&gt;
70  * </CODE></PRE>
71  *
72  * <P>The behaviour of this mailet is equivalent to using Resend with the following
73  * configuration:</P>
74  * <PRE><CODE>
75  * &lt;mailet match="All" class="Resend">
76  * &lt;sender&gt;<I>an address or postmaster or sender or unaltered</I>&lt;/sender&gt;
77  * &lt;attachError&gt;<I>true or false</I>&lt;/attachError&gt;
78  * &lt;message&gt;<I><B>dynamically built</B></I>&lt;/message&gt;
79  * &lt;prefix&gt;<I>a string</I>&lt;/prefix&gt;
80  * &lt;passThrough&gt;true&lt;/passThrough&gt;
81  * &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
82  * &lt;to&gt;<I>unaltered or sender or from&lt</I>;/to&gt;
83  * &lt;recipients&gt;<B>sender</B>&lt;/recipients&gt;
84  * &lt;inline&gt;none&lt;/inline&gt;
85  * &lt;attachment&gt;message&lt;/attachment&gt;
86  * &lt;isReply&gt;true&lt;/isReply&gt;
87  * &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
88  * &lt;/mailet&gt;
89  * </CODE></PRE>
90  * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
91  * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
92  *
93  * @version CVS $Revision: 1.10.4.14 $ $Date: 2004/03/15 03:54:19 $
94  */

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

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

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

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

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