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
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.mailet.MailAddress;
25
26 import javax.mail.MessagingException;
27 import javax.mail.internet.InternetAddress;
28 import java.util.HashSet;
29 import java.util.Collection;
30
31 /**
32 * <P>Sends a notification message to the sender of a message.</P>
33 * <P>A sender of the notification message can optionally be specified.
34 * If one is not specified, the postmaster's address will be used.<BR>
35 * The "To:" header of the notification message can be set to "unaltered";
36 * if missing will be set to the sender of the notified message.<BR>
37 * A notice text can be specified, and in such case will be inserted into the
38 * notification inline text.<BR>
39 * If the notified message has an "error message" set, it will be inserted into the
40 * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
41 * is set to true, such error message will be attached to the notification message.<BR>
42 * The notified messages are attached in their entirety (headers and
43 * content) and the resulting MIME part type is "message/rfc822".</P>
44 * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
45 *
46 * <P>Sample configuration:</P>
47 * <PRE><CODE>
48 * <mailet match="All" class="NotifySender">
49 * <sender><I>an address or postmaster or sender or unaltered, default=postmaster</I></sender>
50 * <attachError><I>true or false, default=false</I></attachError>
51 * <prefix><I>optional subject prefix prepended to the original message</I></prefix>
52 * <inline><I>see {@link Resend}, default=none</I></inline>
53 * <attachment><I>see {@link Resend}, default=message</I></attachment>
54 * <passThrough><I>true or false, default=true</I></passThrough>
55 * <fakeDomainCheck><I>true or false, default=true</I></fakeDomainCheck>
56 * <to><I>unaltered or sender or from(optional, defaults to sender)</I></to>
57 * <debug><I>true or false, default=false</I></debug>
58 * </mailet>
59 * </CODE></PRE>
60 *
61 * <P>The behaviour of this mailet is equivalent to using Resend with the following
62 * configuration:</P>
63 * <PRE><CODE>
64 * <mailet match="All" class="Resend">
65 * <sender><I>an address or postmaster or sender or unaltered</I></sender>
66 * <attachError><I>true or false</I></attachError>
67 * <message><I><B>dynamically built</B></I></message>
68 * <prefix><I>a string</I></prefix>
69 * <passThrough>true</passThrough>
70 * <fakeDomainCheck><I>true or false</I></fakeDomainCheck>
71 * <to><I>unaltered or sender or from<</I>;/to>
72 * <recipients><B>sender</B></recipients>
73 * <inline>none</inline>
74 * <attachment>message</attachment>
75 * <isReply>true</isReply>
76 * <debug><I>true or false</I></debug>
77 * </mailet>
78 * </CODE></PRE>
79 * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
80 * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
81 *
82 * @version CVS $Revision: 426007 $ $Date: 2006-07-27 09:52:06 +0100 (Thu, 27 Jul 2006) $
83 */
84 public class NotifySender extends AbstractNotify {
85
86 /**
87 * Return a string describing this mailet.
88 *
89 * @return a string describing this mailet
90 */
91 public String getMailetInfo() {
92 return "NotifySender Mailet";
93 }
94
95 /** Gets the expected init parameters. */
96 protected String[] getAllowedInitParameters() {
97 String[] allowedArray = {
98 // "static",
99 "debug",
100 "passThrough",
101 "fakeDomainCheck",
102 "inline",
103 "attachment",
104 "message",
105 "notice",
106 "sender",
107 "sendingAddress",
108 "prefix",
109 "attachError",
110 "attachStackTrace",
111 "to"
112 };
113 return allowedArray;
114 }
115
116 /* ******************************************************************** */
117 /* ****************** Begin of getX and setX methods ****************** */
118 /* ******************************************************************** */
119
120 /**
121 * @return <CODE>SpecialAddress.SENDER</CODE>, indicating the sender of the current mail
122 */
123 protected Collection getRecipients() {
124 Collection newRecipients = new HashSet();
125 newRecipients.add(SpecialAddress.SENDER);
126 return newRecipients;
127 }
128
129 /**
130 * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or <CODE>SpecialAddress.SENDER</CODE> if missing
131 */
132 protected InternetAddress[] getTo() throws MessagingException {
133 String addressList = getInitParameter("to");
134 InternetAddress[] iaarray = new InternetAddress[1];
135 iaarray[0] = SpecialAddress.SENDER.toInternetAddress();
136 if (addressList != null) {
137 MailAddress specialAddress = getSpecialAddress(addressList,
138 new String[] {"sender", "unaltered", "from"});
139 if (specialAddress != null) {
140 iaarray[0] = specialAddress.toInternetAddress();
141 } else {
142 log("\"to\" parameter ignored, set to sender");
143 }
144 }
145 return iaarray;
146 }
147
148 /**
149 * @return the <CODE>attachStackTrace</CODE> init parameter,
150 * or the <CODE>attachError</CODE> init parameter if missing,
151 * or false if missing
152 */
153 protected boolean attachError() throws MessagingException {
154 String parameter = getInitParameter("attachStackTrace");
155 if (parameter == null) {
156 return super.attachError();
157 }
158 return new Boolean(parameter).booleanValue();
159 }
160
161 /* ******************************************************************** */
162 /* ******************* End of getX and setX methods ******************* */
163 /* ******************************************************************** */
164
165 }
166