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.GenericMailet;
25 import org.apache.mailet.Mail;
26 import org.apache.mailet.base.RFC2822Headers;
27
28 import javax.mail.MessagingException;
29 import javax.mail.internet.MimeBodyPart;
30 import javax.mail.internet.MimeMessage;
31 import javax.mail.internet.MimeMultipart;
32 import javax.mail.internet.MimePart;
33
34 import java.io.IOException;
35 import java.io.UnsupportedEncodingException;
36
37
38
39
40 public abstract class AbstractAddFooter extends GenericMailet {
41
42
43
44
45
46
47
48
49
50
51 public void service(Mail mail) throws MessagingException {
52 try {
53 MimeMessage message = mail.getMessage();
54
55 if (attachFooter(message)) {
56 message.saveChanges();
57 } else {
58 log("Unable to add footer to mail " + mail.getName());
59 }
60 } catch (UnsupportedEncodingException e) {
61 log("UnsupportedEncoding Unable to add footer to mail "
62 + mail.getName());
63 } catch (IOException ioe) {
64 throw new MessagingException("Could not read message", ioe);
65 }
66 }
67
68
69
70
71
72
73
74
75
76 protected void addToText(MimePart part) throws MessagingException,
77 IOException {
78
79 String contentType = part.getContentType();
80 String content = (String) part.getContent();
81
82 if (!content.endsWith("\n")) {
83 content += "\r\n";
84 }
85 content += getFooterText();
86
87 part.setContent(content, contentType);
88 part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
89
90 }
91
92
93
94
95
96
97
98
99
100 protected void addToHTML(MimePart part) throws MessagingException,
101 IOException {
102
103 String contentType = part.getContentType();
104 String content = (String) part.getContent();
105
106
107
108
109 int index = content.lastIndexOf("</body>");
110 if (index == -1)
111 index = content.lastIndexOf("</BODY>");
112 String insert = "<br>" + getFooterHTML();
113 content = index == -1 ? content + insert : content.substring(0, index)
114 + insert + content.substring(index);
115
116 part.setContent(content, contentType);
117 part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
118
119 }
120
121
122
123
124
125
126
127
128
129
130 protected boolean attachFooter(MimePart part) throws MessagingException,
131 IOException {
132
133 if (part.isMimeType("text/plain")
134 && part.getContent() instanceof String) {
135 addToText(part);
136 return true;
137 } else if (part.isMimeType("text/html")
138 && part.getContent() instanceof String) {
139 addToHTML(part);
140 return true;
141 } else if (part.isMimeType("multipart/mixed")
142 || part.isMimeType("multipart/related")) {
143
144 MimeMultipart multipart = (MimeMultipart) part.getContent();
145 MimeBodyPart firstPart = (MimeBodyPart) multipart.getBodyPart(0);
146 boolean isFooterAttached = attachFooter(firstPart);
147 if (isFooterAttached) {
148
149
150 part.setContent(multipart);
151 }
152 return isFooterAttached;
153 } else if (part.isMimeType("multipart/alternative")) {
154 MimeMultipart multipart = (MimeMultipart) part.getContent();
155 int count = multipart.getCount();
156
157 boolean isFooterAttached = false;
158 for (int index = 0; index < count; index++) {
159
160 MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart
161 .getBodyPart(index);
162 isFooterAttached |= attachFooter(mimeBodyPart);
163 }
164 if (isFooterAttached) {
165
166
167 part.setContent(multipart);
168 }
169 return isFooterAttached;
170 } else {
171
172 return false;
173 }
174 }
175
176
177
178
179
180
181
182 protected abstract String getFooterText();
183
184
185
186
187
188
189
190
191 protected abstract String getFooterHTML();
192
193 }