KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > AddFooter


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.james.core.MailImpl;
21 import org.apache.mailet.GenericMailet;
22 import org.apache.mailet.Mail;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeBodyPart JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import javax.mail.internet.MimeMultipart JavaDoc;
28 import javax.mail.internet.MimePart JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 /**
33  * This mailet will attach text to the end of the message (like a footer). Right
34  * now it only supports simple messages without multiple parts.
35  */

36 public class AddFooter extends GenericMailet {
37
38     /**
39      * This is the plain text version of the footer we are going to add
40      */

41     String JavaDoc text = "";
42
43     /**
44      * Initialize the mailet
45      */

46     public void init() throws MessagingException JavaDoc {
47         text = getInitParameter("text");
48     }
49
50     /**
51      * Takes the message and attaches a footer message to it. Right now, it only
52      * supports simple messages. Needs to have additions to make it support
53      * messages with alternate content types or with attachments.
54      *
55      * @param mail the mail being processed
56      *
57      * @throws MessagingException if an error arises during message processing
58      */

59     public void service(Mail mail) throws MessagingException JavaDoc {
60         try {
61             MimeMessage JavaDoc message = mail.getMessage();
62 // log("Trying to add footer to mail " + ((MailImpl)mail).getName());
63
if (attachFooter(message)) {
64                 message.saveChanges();
65 // log("Message after saving: " + message.getContent().toString());
66
/*
67                 java.io.ByteArrayOutputStream bodyOs = new java.io.ByteArrayOutputStream(512);
68                 java.io.OutputStream bos;
69                 java.io.InputStream bis;
70                 try {
71                     bis = message.getRawInputStream();
72                     bos = bodyOs;
73                     log("Using getRawInputStream()");
74                 } catch(javax.mail.MessagingException me) {
75                     bos = javax.mail.internet.MimeUtility.encode(bodyOs, message.getEncoding());
76                     bis = message.getInputStream();
77                     log("Using getInputStream()");
78                 }
79
80                 try {
81                     byte[] block = new byte[1024];
82                     int read = 0;
83                     while ((read = bis.read(block)) > -1) {
84                         bos.write(block, 0, read);
85                     }
86                     bos.flush();
87                 }
88                 finally {
89                     org.apache.avalon.excalibur.io.IOUtil.shutdownStream(bis);
90                 }
91                 log("Message from stream: " + bodyOs.toString());
92                 */

93             } else {
94                 log("Unable to add footer to mail " + ((MailImpl)mail).getName());
95             }
96         } catch (IOException JavaDoc ioe) {
97             throw new MessagingException JavaDoc("Could not read message", ioe);
98         }
99     }
100
101     /**
102      * This is exposed as a method for easy subclassing to provide alternate ways
103      * to get the footer text.
104      *
105      * @return the footer text
106      */

107     public String JavaDoc getFooterText() {
108         return text;
109     }
110
111     /**
112      * This is exposed as a method for easy subclassing to provide alternate ways
113      * to get the footer text. By default, this will take the footer text,
114      * converting the linefeeds to <br> tags.
115      *
116      * @return the HTML version of the footer text
117      */

118     public String JavaDoc getFooterHTML() {
119         String JavaDoc text = getFooterText();
120         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
121         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(text, "\r\n", true);
122         while (st.hasMoreTokens()) {
123             String JavaDoc token = st.nextToken();
124             if (token.equals("\r")) {
125                 continue;
126             }
127             if (token.equals("\n")) {
128                 sb.append("<br />\n");
129             } else {
130                 sb.append(token);
131             }
132         }
133         return sb.toString();
134     }
135
136     /**
137      * Return a string describing this mailet.
138      *
139      * @return a string describing this mailet
140      */

141     public String JavaDoc getMailetInfo() {
142         return "AddFooter Mailet";
143     }
144
145     /**
146      * Prepends the content of the MimePart as text to the existing footer
147      *
148      * @param part the MimePart to attach
149      *
150      * @throws MessagingException
151      * @throws IOException
152      */

153     protected void addToText(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
154 // log("Trying to add footer to " + part.getContent().toString());
155
String JavaDoc content = part.getContent().toString();
156         if (!content.endsWith("\n")) {
157             content += "\r\n";
158         }
159         content += getFooterText();
160         part.setText(content);
161 // log("After adding footer: " + part.getContent().toString());
162
}
163
164     /**
165      * Prepends the content of the MimePart as HTML to the existing footer
166      *
167      * @param part the MimePart to attach
168      *
169      * @throws MessagingException
170      * @throws IOException
171      */

172     protected void addToHTML(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
173 // log("Trying to add footer to " + part.getContent().toString());
174
String JavaDoc content = part.getContent().toString();
175
176         /* This HTML part may have a closing <BODY> tag. If so, we
177          * want to insert out footer immediately prior to that tag.
178          */

179         int index = content.lastIndexOf("</body>");
180         if (index == -1) index = content.lastIndexOf("</BODY>");
181         String JavaDoc insert = "<br>" + getFooterHTML();
182         content = index == -1 ? content + insert : content.substring(0, index) + insert + content.substring(index);
183    
184         part.setContent(content, part.getContentType());
185 // log("After adding footer: " + part.getContent().toString());
186
}
187
188     /**
189      * Attach a footer a MimePart
190      *
191      * @param part the MimePart to which the footer is to be attached
192      *
193      * @return whether a footer was successfully attached
194      * @throws MessagingException
195      * @throws IOException
196      */

197     protected boolean attachFooter(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
198 // log("Content type is " + part.getContentType());
199
if (part.isMimeType("text/plain")) {
200             addToText(part);
201             return true;
202         } else if (part.isMimeType("text/html")) {
203             addToHTML(part);
204             return true;
205         } else if (part.isMimeType("multipart/mixed")) {
206             //Find the first body part, and determine what to do then.
207
MimeMultipart JavaDoc multipart = (MimeMultipart JavaDoc)part.getContent();
208             MimeBodyPart JavaDoc firstPart = (MimeBodyPart JavaDoc)multipart.getBodyPart(0);
209             boolean isFooterAttached = attachFooter(firstPart);
210             //We have to do this because of a bug in JavaMail (ref id 4404733)
211
part.setContent(multipart);
212             return isFooterAttached;
213         } else if (part.isMimeType("multipart/alternative")) {
214             MimeMultipart JavaDoc multipart = (MimeMultipart JavaDoc)part.getContent();
215             int count = multipart.getCount();
216 // log("number of alternatives = " + count);
217
boolean isFooterAttached = false;
218             for (int index = 0; index < count; index++) {
219 // log("processing alternative #" + index);
220
MimeBodyPart JavaDoc mimeBodyPart = (MimeBodyPart JavaDoc)multipart.getBodyPart(index);
221                 isFooterAttached |= attachFooter(mimeBodyPart);
222             }
223             //We have to do this because of a bug in JavaMail (ref id 4404733)
224
part.setContent(multipart);
225             return isFooterAttached;
226         } else {
227             //Give up... we won't attach the footer to this MimePart
228
return false;
229         }
230     }
231 }
232
Popular Tags