KickJava   Java API By Example, From Geeks To Geeks.

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


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.james.util.RFC2822Headers;
21 import org.apache.mailet.GenericMailet;
22 import org.apache.mailet.Mail;
23 import org.apache.mailet.MailAddress;
24 import org.apache.mailet.MailetException;
25
26 import javax.mail.Address JavaDoc;
27 import javax.mail.Message JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Session JavaDoc;
30 import javax.mail.internet.InternetAddress JavaDoc;
31 import javax.mail.internet.MimeBodyPart JavaDoc;
32 import javax.mail.internet.MimeMessage JavaDoc;
33 import javax.mail.internet.MimeMultipart JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.PrintWriter JavaDoc;
36 import java.io.StringWriter JavaDoc;
37 import java.util.Date JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42
43 /**
44  * <P>Abstract mailet providing configurable notification services.<BR>
45  * This mailet can be subclassed to make authoring notification mailets simple.<BR>
46  * <P>Provides the following functionalities to all notification subclasses:</P>
47  * <UL>
48  * <LI>A common notification message layout.</LI>
49  * <LI>A sender of the notification message can optionally be specified.
50  * If one is not specified, the postmaster's address will be used.</LI>
51  * <LI>A notice text can be specified, and in such case will be inserted into the
52  * notification inline text.</LI>
53  * <LI>If the notified message has an "error message" set, it will be inserted into the
54  * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
55  * is set to true, such error message will be attached to the notification message.</LI>
56  * <LI>The notified messages are attached in their entirety (headers and
57  * content) and the resulting MIME part type is "message/rfc822".</LI>
58  * <LI>Supports by default the <CODE>passThrough</CODE> init parameter (true if missing).</LI>
59  * </UL>
60  *
61  * <P>Sample configuration common to all notification mailet subclasses:</P>
62  * <PRE><CODE>
63  * &lt;mailet match="All" class="<I>a notification mailet</I>">
64  * &lt;sender&gt;<I>an address or postmaster or sender or unaltered, default=postmaster</I>&lt;/sender&gt;
65  * &lt;attachError&gt;<I>true or false, default=false</I>&lt;/attachError&gt;
66  * &lt;message&gt;<I>notice attached to the original message text (optional)</I>&lt;/message&gt;
67  * &lt;prefix&gt;<I>optional subject prefix prepended to the original message</I>&lt;/prefix&gt;
68  * &lt;inline&gt;<I>see {@link Redirect}, default=none</I>&lt;/inline&gt;
69  * &lt;attachment&gt;<I>see {@link Redirect}, default=message</I>&lt;/attachment&gt;
70  * &lt;passThrough&gt;<I>true or false, default=true</I>&lt;/passThrough&gt;
71  * &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
72  * &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
73  * &lt;/mailet&gt;
74  * </CODE></PRE>
75  * <P><I>notice</I> and <I>senderAddress</I> can be used instead of
76  * <I>message</I> and <I>sender</I>; such names are kept for backward compatibility.</P>
77  *
78  * @version CVS $Revision: 1.1.2.12 $ $Date: 2004/05/05 10:34:53 $
79  * @since 2.2.0
80  */

81 public abstract class AbstractNotify extends AbstractRedirect {
82
83     /* ******************************************************************** */
84     /* ****************** Begin of getX and setX methods ****************** */
85     /* ******************************************************************** */
86
87     /**
88      * @return the <CODE>passThrough</CODE> init parameter, or true if missing
89      */

90     protected boolean getPassThrough() throws MessagingException JavaDoc {
91         if(getInitParameter("passThrough") == null) {
92             return true;
93         } else {
94             return new Boolean JavaDoc(getInitParameter("passThrough")).booleanValue();
95         }
96     }
97
98     /**
99      * @return the <CODE>inline</CODE> init parameter, or <CODE>NONE</CODE> if missing
100      */

101     protected int getInLineType() throws MessagingException JavaDoc {
102         if(getInitParameter("inline") == null) {
103             return NONE;
104         } else {
105             return getTypeCode(getInitParameter("inline"));
106         }
107     }
108
109     /**
110      * @return the <CODE>attachment</CODE> init parameter, or <CODE>MESSAGE</CODE> if missing
111      */

112     protected int getAttachmentType() throws MessagingException JavaDoc {
113         if(getInitParameter("attachment") == null) {
114             return MESSAGE;
115         } else {
116             return getTypeCode(getInitParameter("attachment"));
117         }
118     }
119
120     /**
121      * @return the <CODE>notice</CODE> init parameter,
122      * or the <CODE>message</CODE> init parameter if missing,
123      * or a default string if both are missing
124      */

125     protected String JavaDoc getMessage() {
126         if(getInitParameter("notice") == null) {
127             if(getInitParameter("message") == null) {
128                 return "We were unable to deliver the attached message because of an error in the mail server.";
129             } else {
130                 return getInitParameter("message");
131             }
132         } else {
133             return getInitParameter("notice");
134         }
135     }
136
137     /**
138      * @return the full message to append, built from the Mail object
139      */

140     protected String JavaDoc getMessage(Mail originalMail) throws MessagingException JavaDoc {
141         MimeMessage JavaDoc message = originalMail.getMessage();
142         StringWriter JavaDoc sout = new StringWriter JavaDoc();
143         PrintWriter JavaDoc out = new PrintWriter JavaDoc(sout, true);
144
145         // First add the "local" notice
146
// (either from conf or generic error message)
147
out.println(getMessage());
148         // And then the message from other mailets
149
if (originalMail.getErrorMessage() != null) {
150             out.println();
151             out.println("Error message below:");
152             out.println(originalMail.getErrorMessage());
153         }
154         out.println();
155         out.println("Message details:");
156
157         if (message.getSubject() != null) {
158             out.println(" Subject: " + message.getSubject());
159         }
160         if (message.getSentDate() != null) {
161             out.println(" Sent date: " + message.getSentDate());
162         }
163         out.println(" MAIL FROM: " + originalMail.getSender());
164         Iterator JavaDoc rcptTo = originalMail.getRecipients().iterator();
165         out.println(" RCPT TO: " + rcptTo.next());
166         while (rcptTo.hasNext()) {
167             out.println(" " + rcptTo.next());
168         }
169         String JavaDoc[] addresses = null;
170         addresses = message.getHeader(RFC2822Headers.FROM);
171         if (addresses != null) {
172             out.print(" From: ");
173             for (int i = 0; i < addresses.length; i++) {
174                 out.print(addresses[i] + " ");
175             }
176             out.println();
177         }
178         addresses = message.getHeader(RFC2822Headers.TO);
179         if (addresses != null) {
180             out.print(" To: ");
181             for (int i = 0; i < addresses.length; i++) {
182                 out.print(addresses[i] + " ");
183             }
184             out.println();
185         }
186         addresses = message.getHeader(RFC2822Headers.CC);
187         if (addresses != null) {
188             out.print(" CC: ");
189             for (int i = 0; i < addresses.length; i++) {
190                 out.print(addresses[i] + " ");
191             }
192             out.println();
193         }
194         out.println(" Size (in bytes): " + message.getSize());
195         if (message.getLineCount() >= 0) {
196             out.println(" Number of lines: " + message.getLineCount());
197         }
198
199         return sout.toString();
200     }
201
202     // All subclasses of AbstractNotify are expected to establish their own recipients
203
abstract protected Collection JavaDoc getRecipients() throws MessagingException JavaDoc;
204
205     /**
206      * @return null
207      */

208     protected InternetAddress JavaDoc[] getTo() throws MessagingException JavaDoc {
209         return null;
210     }
211
212     /**
213      * @return <CODE>SpecialAddress.NULL</CODE>, that will remove the "ReplyTo:" header
214      */

215     protected MailAddress getReplyTo() throws MessagingException JavaDoc {
216         return SpecialAddress.NULL;
217     }
218
219     /**
220      * @return {@link AbstractRedirect#getSender(Mail)}, meaning the new requested sender if any
221      */

222     protected MailAddress getReversePath(Mail originalMail) throws MessagingException JavaDoc {
223         return getSender(originalMail);
224     }
225
226     /**
227      * @return the value of the <CODE>sendingAddress</CODE> init parameter,
228      * or the value of the <CODE>sender</CODE> init parameter if missing,
229      * or the postmaster address if both are missing
230      * @return the <CODE>sendingAddress</CODE> init parameter
231      * or the <CODE>sender</CODE> init parameter
232      * or the postmaster address if both are missing;
233      * possible special addresses returned are
234      * <CODE>SpecialAddress.SENDER</CODE>
235      * and <CODE>SpecialAddress.UNALTERED</CODE>
236      */

237     protected MailAddress getSender() throws MessagingException JavaDoc {
238         String JavaDoc addressString = getInitParameter("sendingAddress");
239         
240         if (addressString == null) {
241             addressString = getInitParameter("sender");
242             if (addressString == null) {
243                 return getMailetContext().getPostmaster();
244             }
245         }
246         
247         MailAddress specialAddress = getSpecialAddress(addressString,
248                                         new String JavaDoc[] {"postmaster", "sender", "unaltered"});
249         if (specialAddress != null) {
250             return specialAddress;
251         }
252
253         try {
254             return new MailAddress(addressString);
255         } catch(Exception JavaDoc e) {
256             throw new MessagingException JavaDoc("Exception thrown in getSender() parsing: " + addressString, e);
257         }
258     }
259
260     /**
261      * @return null
262      */

263     protected String JavaDoc getSubject() throws MessagingException JavaDoc {
264         return null;
265     }
266
267     /**
268      * @return the <CODE>prefix</CODE> init parameter or "Re:" if missing
269      */

270     protected String JavaDoc getSubjectPrefix() {
271         if(getInitParameter("prefix") == null) {
272             return "Re:";
273         } else {
274             return getInitParameter("prefix");
275         }
276     }
277
278     /**
279      * Builds the subject of <I>newMail</I> appending the subject
280      * of <I>originalMail</I> to <I>subjectPrefix</I>, but avoiding a duplicate.
281      */

282     protected void setSubjectPrefix(Mail newMail, String JavaDoc subjectPrefix, Mail originalMail) throws MessagingException JavaDoc {
283         String JavaDoc subject = originalMail.getMessage().getSubject();
284         if (subject == null) {
285             subject = "";
286         }
287         if (subjectPrefix==null || subject.indexOf(subjectPrefix) == 0) {
288             newMail.getMessage().setSubject(subject);
289         } else {
290             newMail.getMessage().setSubject(subjectPrefix + subject);
291         }
292     }
293
294     /**
295      * @return true
296      */

297     protected boolean isReply() {
298         return true;
299     }
300
301     /* ******************************************************************** */
302     /* ******************* End of getX and setX methods ******************* */
303     /* ******************************************************************** */
304
305 }
306
Popular Tags