1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.mailet.MailAddress;
23
24 import javax.mail.MessagingException;
25 import javax.mail.internet.InternetAddress;
26 import java.util.HashSet;
27 import java.util.Collection;
28
29 /***
30 * <P>Sends a notification message to the Postmaster.</P>
31 * <P>A sender of the notification message can optionally be specified.
32 * If one is not specified, the postmaster's address will be used.<BR>
33 * The "To:" header of the notification message can be set to "unaltered";
34 * if missing will be set to the postmaster.<BR>
35 * A notice text can be specified, and in such case will be inserted into the
36 * notification inline text.<BR>
37 * If the notified message has an "error message" set, it will be inserted into the
38 * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
39 * is set to true, such error message will be attached to the notification message.<BR>
40 * The notified messages are attached in their entirety (headers and
41 * content) and the resulting MIME part type is "message/rfc822".</P>
42 * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
43 *
44 * <P>Sample configuration:</P>
45 * <PRE><CODE>
46 * <mailet match="All" class="NotifyPostmaster">
47 * <sender><I>an address or postmaster or sender or unaltered, default=postmaster</I></sender>
48 * <attachError><I>true or false, default=false</I></attachError>
49 * <message><I>notice attached to the original message text (optional)</I></message>
50 * <prefix><I>optional subject prefix prepended to the original message, default="Re:"</I></prefix>
51 * <inline><I>see {@link Resend}, default=none</I></inline>
52 * <attachment><I>see {@link Resend}, default=message</I></attachment>
53 * <passThrough><I>true or false, default=true</I></passThrough>
54 * <fakeDomainCheck><I>true or false, default=true</I></fakeDomainCheck>
55 * <to><I>unaltered (optional, defaults to postmaster)</I></to>
56 * <debug><I>true or false, default=false</I></debug>
57 * </mailet>
58 * </CODE></PRE>
59 *
60 * <P>The behaviour of this mailet is equivalent to using Resend with the following
61 * configuration:</P>
62 * <PRE><CODE>
63 * <mailet match="All" class="Resend">
64 * <sender><I>an address or postmaster or sender or unaltered</I></sender>
65 * <attachError><I>true or false</I></attachError>
66 * <message><I><B>dynamically built</B></I></message>
67 * <prefix><I>a string</I></prefix>
68 * <passThrough><I>true or false</I></passThrough>
69 * <fakeDomainCheck><I>true or false</I></fakeDomainCheck>
70 * <to><I><B>unaltered or postmaster</B></I></to>
71 * <recipients><B>postmaster</B></recipients>
72 * <inline>see {@link Resend}</inline>
73 * <attachment>see {@link Resend}</attachment>
74 * <isReply>true</isReply>
75 * <debug><I>true or false</I></debug>
76 * </mailet>
77 * </CODE></PRE>
78 * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
79 * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
80 *
81 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
82 */
83 public class NotifyPostmaster extends AbstractNotify {
84
85 /***
86 * Return a string describing this mailet.
87 *
88 * @return a string describing this mailet
89 */
90 public String getMailetInfo() {
91 return "NotifyPostmaster Mailet";
92 }
93
94 /*** Gets the expected init parameters. */
95 protected String[] getAllowedInitParameters() {
96 String[] allowedArray = {
97
98 "debug",
99 "passThrough",
100 "fakeDomainCheck",
101 "inline",
102 "attachment",
103 "message",
104 "notice",
105 "sender",
106 "sendingAddress",
107 "prefix",
108 "attachError",
109 "attachStackTrace",
110 "to"
111 };
112 return allowedArray;
113 }
114
115
116
117
118
119 /***
120 * @return the postmaster address
121 */
122 protected Collection getRecipients() {
123 Collection newRecipients = new HashSet();
124 newRecipients.add(getMailetContext().getPostmaster());
125 return newRecipients;
126 }
127
128 /***
129 * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
130 */
131 protected InternetAddress[] getTo() throws MessagingException {
132 String addressList = getInitParameter("to");
133 InternetAddress[] iaarray = new InternetAddress[1];
134 iaarray[0] = getMailetContext().getPostmaster().toInternetAddress();
135 if (addressList != null) {
136 MailAddress specialAddress = getSpecialAddress(addressList,
137 new String[] {"postmaster", "unaltered"});
138 if (specialAddress != null) {
139 iaarray[0] = specialAddress.toInternetAddress();
140 } else {
141 log("\"to\" parameter ignored, set to postmaster");
142 }
143 }
144 return iaarray;
145 }
146
147 /***
148 * @return the <CODE>attachStackTrace</CODE> init parameter,
149 * or the <CODE>attachError</CODE> init parameter if missing,
150 * or false if missing
151 */
152 protected boolean attachError() throws MessagingException {
153 String parameter = getInitParameter("attachStackTrace");
154 if (parameter == null) {
155 return super.attachError();
156 }
157 return new Boolean(parameter).booleanValue();
158 }
159
160
161
162
163
164 }
165