1 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 ; 27 import javax.mail.internet.MimeBodyPart ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.internet.MimeMultipart ; 30 import javax.mail.internet.MimePart ; 31 import java.io.IOException ; 32 33 34 45 public class CommandListservFooter extends GenericMailet { 46 47 protected String footerText; 48 protected String footerHtml; 49 50 53 protected ICommandListservManager commandListservManager; 54 55 58 protected Perl5Compiler perl5Compiler = new Perl5Compiler(); 59 protected Pattern insertPattern; 60 protected Pattern newlinePattern; 61 62 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 ("Unable to parse regexps: " + e.getMessage()); 75 } 76 } 77 78 81 public void init() throws MessagingException { 82 try { 83 xmlResources = commandListservManager.initXMLResources(new String []{"footer", "footer_html"}); 84 } catch (ConfigurationException e) { 85 throw new MessagingException (e.getMessage(), e); 86 } 87 } 88 89 94 public String getMailetInfo() { 95 return "CommandListservFooter Mailet"; 96 } 97 98 99 104 public void service(Mail mail) throws MessagingException { 105 try { 106 MimeMessage message = mail.getMessage(); 107 108 if (message.isMimeType("text/plain")) { 110 addToText(message); 112 } else if (message.isMimeType("multipart/mixed")) { 113 MimeMultipart multipart = (MimeMultipart ) message.getContent(); 115 MimeBodyPart part = (MimeBodyPart ) multipart.getBodyPart(0); 116 attachFooter(part); 117 message.setContent(multipart); 119 } else { 120 MimeMultipart multipart = (MimeMultipart ) message.getContent(); 122 int count = multipart.getCount(); 123 for (int index = 0; index < count; index++) { 124 MimeBodyPart part = (MimeBodyPart ) multipart.getBodyPart(index); 125 attachFooter(part); 126 } 127 message.setContent(multipart); 129 } 130 } catch (IOException ioe) { 131 throw new MessagingException ("Could not read message", ioe); 132 } 133 } 134 135 141 protected String getFooterText() { 142 if (footerText == null) { 143 footerText = getFormattedText(TEXT_PLAIN); 144 } 145 return footerText; 146 } 147 148 154 protected String getFooterHTML() { 155 if (footerHtml == null) { 156 String 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 175 protected void addToHTML(MimePart part) throws MessagingException , IOException { 176 String content = part.getContent().toString(); 177 StringSubstitution stringSubstitution = new StringSubstitution("<br />" + getFooterHTML() + "</body</html>"); 178 String result = Util.substitute(new Perl5Matcher(), insertPattern, stringSubstitution, content, 1); 179 part.setContent(result, part.getContentType()); 180 } 181 182 190 protected void addToText(MimePart part) throws MessagingException , IOException { 191 String content = part.getContent().toString(); 192 if (!content.endsWith("\n")) { 193 content += "\r\n"; 194 } 195 content += getFooterText(); 196 part.setText(content); 197 } 198 199 207 protected void attachFooter(MimePart part) throws MessagingException , IOException { 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 ) { 213 MimeMultipart multipart = (MimeMultipart ) part.getContent(); 214 int count = multipart.getCount(); 215 for (int index = 0; index < count; index++) { 216 MimeBodyPart mimeBodyPart = (MimeBodyPart ) multipart.getBodyPart(index); 217 attachFooter(mimeBodyPart); 218 } 219 part.setContent(multipart); 220 } else { 221 } 223 } 224 225 230 protected String getFormattedText(int index) { 231 return xmlResources[index].getString("text"); 232 } 233 } 234 | Popular Tags |