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