1 17 18 package org.apache.james.transport.mailets; 19 20 import org.apache.mailet.GenericMailet; 21 import org.apache.mailet.Mail; 22 import org.apache.mailet.MailAddress; 23 24 import javax.mail.MessagingException ; 25 import javax.mail.internet.InternetAddress ; 26 import javax.mail.internet.MimeMessage ; 27 import java.util.Collection ; 28 import java.util.StringTokenizer ; 29 import java.util.Vector ; 30 31 53 public class UseHeaderRecipients extends GenericMailet { 54 55 58 private boolean isDebug = false; 59 60 65 public void init() { 66 isDebug = (getInitParameter("debug") == null) ? false : new Boolean (getInitParameter("debug")).booleanValue(); 67 } 68 69 76 public void service(Mail mail) throws MessagingException { 77 MimeMessage message = mail.getMessage(); 78 79 Collection recipients = mail.getRecipients(); 84 recipients.clear(); 86 recipients.addAll(getHeaderMailAddresses(message, "Mail-For")); 87 if (recipients.isEmpty()) { 88 recipients.addAll(getHeaderMailAddresses(message, "To")); 89 recipients.addAll(getHeaderMailAddresses(message, "Cc")); 90 } 91 if (isDebug) { 92 log("All recipients = " + recipients.toString()); 93 log("Reprocessing mail using recipients in message headers"); 94 } 95 96 getMailetContext().sendMail(mail.getSender(), mail.getRecipients(), mail.getMessage()); 98 mail.setState(Mail.GHOST); 99 } 100 101 102 107 public String getMailetInfo() { 108 return "UseHeaderRecipients Mailet"; 109 } 110 111 119 private Collection getHeaderMailAddresses(MimeMessage message, String name) throws MessagingException { 120 121 if (isDebug) { 122 StringBuffer logBuffer = 123 new StringBuffer (64) 124 .append("Checking ") 125 .append(name) 126 .append(" headers"); 127 log(logBuffer.toString()); 128 } 129 Collection addresses = new Vector (); 130 String [] headers = message.getHeader(name); 131 String addressString; 132 InternetAddress iAddress; 133 if (headers != null) { 134 for(int i = 0; i < headers.length; i++) { 135 StringTokenizer st = new StringTokenizer (headers[i], ",", false); 136 while (st.hasMoreTokens()) { 137 addressString = st.nextToken(); 138 iAddress = new InternetAddress (addressString); 139 if (isDebug) { 140 log("Address = " + iAddress.toString()); 141 } 142 addresses.add(new MailAddress(iAddress)); 143 } 144 } 145 } 146 return addresses; 147 } 148 149 } 150 | Popular Tags |