KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.configuration.ConfigurationException;
21 import org.apache.james.util.XMLResources;
22 import org.apache.mailet.GenericMailet;
23 import org.apache.mailet.Mail;
24 import org.apache.oro.text.regex.*;
25
26 import javax.mail.MessagingException JavaDoc;
27 import javax.mail.internet.MimeBodyPart JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29 import javax.mail.internet.MimeMultipart JavaDoc;
30 import javax.mail.internet.MimePart JavaDoc;
31 import java.io.IOException JavaDoc;
32
33
34 /**
35  * CommandListservFooter is based on the AddFooter mailet.
36  *
37  * It is used by the {@link CommandListservProcessor} to inject a footer into mailing list.
38  * <br />
39  * <br />
40  *
41  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:19 $
42  * @since 2.2.0
43  * @see XMLResources
44  */

45 public class CommandListservFooter extends GenericMailet {
46
47     protected String JavaDoc footerText;
48     protected String JavaDoc footerHtml;
49
50     /**
51      * The list serv manager
52      */

53     protected ICommandListservManager commandListservManager;
54
55     /**
56      * For matching
57      */

58     protected Perl5Compiler perl5Compiler = new Perl5Compiler();
59     protected Pattern insertPattern;
60     protected Pattern newlinePattern;
61
62     //For resources
63
protected XMLResources[] xmlResources = new XMLResources[2];
64
65     protected static final int TEXT_PLAIN = 0;
66     protected static final int TEXT_HTML = 1;
67
68     public CommandListservFooter(ICommandListservManager commandListservManager) {
69         this.commandListservManager = commandListservManager;
70         try {
71             insertPattern = perl5Compiler.compile("</body>\\s*</html>", Perl5Compiler.CASE_INSENSITIVE_MASK);
72             newlinePattern = perl5Compiler.compile("\r\n|\n", Perl5Compiler.CASE_INSENSITIVE_MASK);
73         } catch (MalformedPatternException e) {
74             throw new IllegalStateException JavaDoc("Unable to parse regexps: " + e.getMessage());
75         }
76     }
77
78     /**
79      * Initialize the mailet
80      */

81     public void init() throws MessagingException JavaDoc {
82         try {
83             xmlResources = commandListservManager.initXMLResources(new String JavaDoc[]{"footer", "footer_html"});
84         } catch (ConfigurationException e) {
85             throw new MessagingException JavaDoc(e.getMessage(), e);
86         }
87     }
88
89     /**
90      * Return a string describing this mailet.
91      *
92      * @return a string describing this mailet
93      */

94     public String JavaDoc getMailetInfo() {
95         return "CommandListservFooter Mailet";
96     }
97
98
99     /**
100      * Identify what type of mimeMessage it is, and attach the footer
101      * @param mail
102      * @throws MessagingException
103      */

104     public void service(Mail mail) throws MessagingException JavaDoc {
105         try {
106             MimeMessage JavaDoc message = mail.getMessage();
107
108             //I want to modify the right message body
109
if (message.isMimeType("text/plain")) {
110                 //This is a straight text message... just append the single part normally
111
addToText(message);
112             } else if (message.isMimeType("multipart/mixed")) {
113                 //Find the first body part, and determine what to do then.
114
MimeMultipart JavaDoc multipart = (MimeMultipart JavaDoc) message.getContent();
115                 MimeBodyPart JavaDoc part = (MimeBodyPart JavaDoc) multipart.getBodyPart(0);
116                 attachFooter(part);
117                 //We have to do this because of a bug in JavaMail (ref id 4404733)
118
message.setContent(multipart);
119             } else {
120                 //Find the HTML and text message types and add to each
121
MimeMultipart JavaDoc multipart = (MimeMultipart JavaDoc) message.getContent();
122                 int count = multipart.getCount();
123                 for (int index = 0; index < count; index++) {
124                     MimeBodyPart JavaDoc part = (MimeBodyPart JavaDoc) multipart.getBodyPart(index);
125                     attachFooter(part);
126                 }
127                 //We have to do this because of a bug in JavaMail (ref id 4404733)
128
message.setContent(multipart);
129             }
130         } catch (IOException JavaDoc ioe) {
131             throw new MessagingException JavaDoc("Could not read message", ioe);
132         }
133     }
134
135     /**
136      * Get and cache the footer text
137      *
138      * @return the footer text
139      * @see XMLResources
140      */

141     protected String JavaDoc getFooterText() {
142         if (footerText == null) {
143             footerText = getFormattedText(TEXT_PLAIN);
144         }
145         return footerText;
146     }
147
148     /**
149      * Get and cache the footer html text
150      *
151      * @return the footer text
152      * @see XMLResources
153      */

154     protected String JavaDoc getFooterHTML() {
155         if (footerHtml == null) {
156             String JavaDoc footerText = getFormattedText(TEXT_HTML);
157             footerHtml = Util.substitute(new Perl5Matcher(),
158                     newlinePattern,
159                     new StringSubstitution(" <br />"),
160                     footerText,
161                     Util.SUBSTITUTE_ALL);
162         }
163         return footerHtml;
164     }
165
166     /**
167      * Prepends the content of the MimePart as HTML to the existing footer.
168      * We use the regular expression to inject the footer inside of the body tag appropriately.
169      *
170      * @param part the MimePart to attach
171      *
172      * @throws MessagingException
173      * @throws java.io.IOException
174      */

175     protected void addToHTML(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
176         String JavaDoc content = part.getContent().toString();
177         StringSubstitution stringSubstitution = new StringSubstitution("<br />" + getFooterHTML() + "</body</html>");
178         String JavaDoc result = Util.substitute(new Perl5Matcher(), insertPattern, stringSubstitution, content, 1);
179         part.setContent(result, part.getContentType());
180     }
181
182     /**
183      * Prepends the content of the MimePart as text to the existing footer
184      *
185      * @param part the MimePart to attach
186      *
187      * @throws MessagingException
188      * @throws IOException
189      */

190     protected void addToText(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
191         String JavaDoc content = part.getContent().toString();
192         if (!content.endsWith("\n")) {
193             content += "\r\n";
194         }
195         content += getFooterText();
196         part.setText(content);
197     }
198
199     /**
200      * Attaches a MimePart as an appropriate footer
201      *
202      * @param part the MimePart to attach
203      *
204      * @throws MessagingException
205      * @throws IOException
206      */

207     protected void attachFooter(MimePart JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc {
208         if (part.isMimeType("text/plain")) {
209             addToText(part);
210         } else if (part.isMimeType("text/html")) {
211             addToHTML(part);
212         } else if (part.getContent() instanceof MimeMultipart JavaDoc) {
213             MimeMultipart JavaDoc multipart = (MimeMultipart JavaDoc) part.getContent();
214             int count = multipart.getCount();
215             for (int index = 0; index < count; index++) {
216                 MimeBodyPart JavaDoc mimeBodyPart = (MimeBodyPart JavaDoc) multipart.getBodyPart(index);
217                 attachFooter(mimeBodyPart);
218             }
219             part.setContent(multipart);
220         } else {
221             //System.err.println(part.getContentType());
222
}
223     }
224
225     /**
226      * @see XMLResources#getString
227      * @param index either {@link #TEXT_PLAIN} or {@link #TEXT_HTML}
228      * @return a formatted text with the proper list and domain
229      */

230     protected String JavaDoc getFormattedText(int index) {
231         return xmlResources[index].getString("text");
232     }
233 }
234
Popular Tags