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