1 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 ; 25 import javax.mail.internet.MimeBodyPart ; 26 import javax.mail.internet.MimeMessage ; 27 import javax.mail.internet.MimeMultipart ; 28 import javax.mail.internet.MimePart ; 29 import java.io.IOException ; 30 import java.util.StringTokenizer ; 31 32 36 public class AddFooter extends GenericMailet { 37 38 41 String text = ""; 42 43 46 public void init() throws MessagingException { 47 text = getInitParameter("text"); 48 } 49 50 59 public void service(Mail mail) throws MessagingException { 60 try { 61 MimeMessage message = mail.getMessage(); 62 if (attachFooter(message)) { 64 message.saveChanges(); 65 93 } else { 94 log("Unable to add footer to mail " + ((MailImpl)mail).getName()); 95 } 96 } catch (IOException ioe) { 97 throw new MessagingException ("Could not read message", ioe); 98 } 99 } 100 101 107 public String getFooterText() { 108 return text; 109 } 110 111 118 public String getFooterHTML() { 119 String text = getFooterText(); 120 StringBuffer sb = new StringBuffer (); 121 StringTokenizer st = new StringTokenizer (text, "\r\n", true); 122 while (st.hasMoreTokens()) { 123 String 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 141 public String getMailetInfo() { 142 return "AddFooter Mailet"; 143 } 144 145 153 protected void addToText(MimePart part) throws MessagingException , IOException { 154 String content = part.getContent().toString(); 156 if (!content.endsWith("\n")) { 157 content += "\r\n"; 158 } 159 content += getFooterText(); 160 part.setText(content); 161 } 163 164 172 protected void addToHTML(MimePart part) throws MessagingException , IOException { 173 String content = part.getContent().toString(); 175 176 179 int index = content.lastIndexOf("</body>"); 180 if (index == -1) index = content.lastIndexOf("</BODY>"); 181 String 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 } 187 188 197 protected boolean attachFooter(MimePart part) throws MessagingException , IOException { 198 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 MimeMultipart multipart = (MimeMultipart )part.getContent(); 208 MimeBodyPart firstPart = (MimeBodyPart )multipart.getBodyPart(0); 209 boolean isFooterAttached = attachFooter(firstPart); 210 part.setContent(multipart); 212 return isFooterAttached; 213 } else if (part.isMimeType("multipart/alternative")) { 214 MimeMultipart multipart = (MimeMultipart )part.getContent(); 215 int count = multipart.getCount(); 216 boolean isFooterAttached = false; 218 for (int index = 0; index < count; index++) { 219 MimeBodyPart mimeBodyPart = (MimeBodyPart )multipart.getBodyPart(index); 221 isFooterAttached |= attachFooter(mimeBodyPart); 222 } 223 part.setContent(multipart); 225 return isFooterAttached; 226 } else { 227 return false; 229 } 230 } 231 } 232 | Popular Tags |