1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.mailet.base.RFC2822Headers;
25 import org.apache.mailet.Mail;
26 import org.apache.mailet.MailAddress;
27
28 import javax.mail.MessagingException;
29 import javax.mail.internet.InternetAddress;
30 import javax.mail.internet.MimeMessage;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.util.Collection;
34 import java.util.Iterator;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public abstract class AbstractNotify extends AbstractRedirect {
75
76
77
78
79
80
81
82
83 protected boolean getPassThrough() throws MessagingException {
84 return new Boolean(getInitParameter("passThrough","true")).booleanValue();
85 }
86
87
88
89
90 protected int getInLineType() throws MessagingException {
91 return getTypeCode(getInitParameter("inline","none"));
92 }
93
94
95
96
97 protected int getAttachmentType() throws MessagingException {
98 return getTypeCode(getInitParameter("attachment","message"));
99 }
100
101
102
103
104
105
106 protected String getMessage() {
107 return getInitParameter("notice",
108 getInitParameter("message",
109 "We were unable to deliver the attached message because of an error in the mail server."));
110 }
111
112
113
114
115 protected String getMessage(Mail originalMail) throws MessagingException {
116 MimeMessage message = originalMail.getMessage();
117 StringWriter sout = new StringWriter();
118 PrintWriter out = new PrintWriter(sout, true);
119
120
121
122 out.println(getMessage());
123
124 if (originalMail.getErrorMessage() != null) {
125 out.println();
126 out.println("Error message below:");
127 out.println(originalMail.getErrorMessage());
128 }
129 out.println();
130 out.println("Message details:");
131
132 if (message.getSubject() != null) {
133 out.println(" Subject: " + message.getSubject());
134 }
135 if (message.getSentDate() != null) {
136 out.println(" Sent date: " + message.getSentDate());
137 }
138 out.println(" MAIL FROM: " + originalMail.getSender());
139 Iterator rcptTo = originalMail.getRecipients().iterator();
140 out.println(" RCPT TO: " + rcptTo.next());
141 while (rcptTo.hasNext()) {
142 out.println(" " + rcptTo.next());
143 }
144 String[] addresses = null;
145 addresses = message.getHeader(RFC2822Headers.FROM);
146 if (addresses != null) {
147 out.print(" From: ");
148 for (int i = 0; i < addresses.length; i++) {
149 out.print(addresses[i] + " ");
150 }
151 out.println();
152 }
153 addresses = message.getHeader(RFC2822Headers.TO);
154 if (addresses != null) {
155 out.print(" To: ");
156 for (int i = 0; i < addresses.length; i++) {
157 out.print(addresses[i] + " ");
158 }
159 out.println();
160 }
161 addresses = message.getHeader(RFC2822Headers.CC);
162 if (addresses != null) {
163 out.print(" CC: ");
164 for (int i = 0; i < addresses.length; i++) {
165 out.print(addresses[i] + " ");
166 }
167 out.println();
168 }
169 out.println(" Size (in bytes): " + message.getSize());
170 if (message.getLineCount() >= 0) {
171 out.println(" Number of lines: " + message.getLineCount());
172 }
173
174 return sout.toString();
175 }
176
177
178 abstract protected Collection getRecipients() throws MessagingException;
179
180
181
182
183 protected InternetAddress[] getTo() throws MessagingException {
184 return null;
185 }
186
187
188
189
190 protected MailAddress getReplyTo() throws MessagingException {
191 return SpecialAddress.NULL;
192 }
193
194
195
196
197 protected MailAddress getReversePath(Mail originalMail) throws MessagingException {
198 return getSender(originalMail);
199 }
200
201
202
203
204
205
206
207
208
209 protected MailAddress getSender() throws MessagingException {
210 String addressString = getInitParameter("sendingAddress",getInitParameter("sender"));
211
212 if (addressString == null) {
213 return getMailetContext().getPostmaster();
214 }
215
216 MailAddress specialAddress = getSpecialAddress(addressString,
217 new String[] {"postmaster", "sender", "unaltered"});
218 if (specialAddress != null) {
219 return specialAddress;
220 }
221
222 try {
223 return new MailAddress(addressString);
224 } catch(Exception e) {
225 throw new MessagingException("Exception thrown in getSender() parsing: " + addressString, e);
226 }
227 }
228
229
230
231
232 protected String getSubject() throws MessagingException {
233 return null;
234 }
235
236
237
238
239 protected String getSubjectPrefix() {
240 return getInitParameter("prefix","Re:");
241 }
242
243
244
245
246
247 protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail) throws MessagingException {
248 String subject = originalMail.getMessage().getSubject();
249 if (subject == null) {
250 subject = "";
251 }
252 if (subjectPrefix==null || subject.indexOf(subjectPrefix) == 0) {
253 newMail.getMessage().setSubject(subject);
254 } else {
255 newMail.getMessage().setSubject(subjectPrefix + subject);
256 }
257 }
258
259
260
261
262 protected boolean isReply() {
263 return true;
264 }
265
266
267
268
269
270 }