Return-Path: Delivered-To: apmail-jakarta-james-dev-archive@apache.org Received: (qmail 74616 invoked from network); 2 Jun 2003 05:48:44 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 2 Jun 2003 05:48:44 -0000 Received: (qmail 7953 invoked by uid 97); 2 Jun 2003 05:51:06 -0000 Delivered-To: qmlist-jakarta-archive-james-dev@nagoya.betaversion.org Received: (qmail 7946 invoked from network); 2 Jun 2003 05:51:06 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 2 Jun 2003 05:51:06 -0000 Received: (qmail 74368 invoked by uid 500); 2 Jun 2003 05:48:40 -0000 Mailing-List: contact james-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "James Developers List" Reply-To: "James Developers List" Delivered-To: mailing list james-dev@jakarta.apache.org Received: (qmail 74357 invoked by uid 500); 2 Jun 2003 05:48:40 -0000 Received: (qmail 74354 invoked from network); 2 Jun 2003 05:48:40 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 2 Jun 2003 05:48:40 -0000 Received: (qmail 5885 invoked by uid 1589); 2 Jun 2003 05:48:51 -0000 Date: 2 Jun 2003 05:48:51 -0000 Message-ID: <20030602054851.5884.qmail@icarus.apache.org> From: noel@apache.org To: jakarta-james-cvs@apache.org Subject: cvs commit: jakarta-james/src/java/org/apache/james/transport/mailets Redirect.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N noel 2003/06/01 22:48:51 Modified: src/java/org/apache/james/transport/mailets Tag: branch_2_1_fcs Redirect.java Log: Reverted Redirect to previous version. Accidentally committed new version that isn't quite finished yet. Revision Changes Path No revision No revision 1.18.4.5 +213 -766 jakarta-james/src/java/org/apache/james/transport/mailets/Redirect.java Index: Redirect.java =================================================================== RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/Redirect.java,v retrieving revision 1.18.4.4 retrieving revision 1.18.4.5 diff -u -r1.18.4.4 -r1.18.4.5 --- Redirect.java 2 Jun 2003 05:40:28 -0000 1.18.4.4 +++ Redirect.java 2 Jun 2003 05:48:51 -0000 1.18.4.5 @@ -68,7 +68,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.StringTokenizer; -import java.util.ArrayList; +import java.util.Vector; import javax.mail.Message; @@ -81,7 +81,6 @@ import org.apache.james.util.RFC2822Headers; import org.apache.james.util.RFC822DateFormat; -import org.apache.james.core.MailImpl; import org.apache.mailet.GenericMailet; import org.apache.mailet.Mail; @@ -105,11 +104,9 @@ *
  • getMessage(), The text of the message itself
  • *
  • getRecipients(), the recipients the mail is sent to
  • *
  • getReplyTo(), where replys to this message will be sent
  • -*
  • getReturnPath(), what to set the Return-Path to
  • *
  • getSender(), who the mail is from
  • *
  • getSubjectPrefix(), a prefix to be added to the message subject
  • *
  • getTo(), a list of people to whom the mail is *apparently* sent
  • -*
  • isReply(), should this mailet set the IN_REPLY_TO header to the id of the current message
  • *
  • getPassThrough(), should this mailet allow the original message to continue processing or GHOST it.
  • *
  • isStatic(), should this mailet run the get methods for every mail, or just *once.
  • @@ -121,16 +118,14 @@ *A comma delimited list of email addresses for recipients of *this message, it will use the "to" list if not specified. These *addresses will only appear in the To: header if no "to" list is -*supplied.
    -*It can include constants "sender" and "postmaster" +*supplied. * * *<to> *A comma delimited list of addresses to appear in the To: header, *the email will only be delivered to these addresses if they are in the recipients *list.
    -*The recipients list will be used if this is not supplied.
    -*It can include constants "sender", "postmaster" and "unaltered" +*The recipients list will be used if this is not supplied. * * *<sender> @@ -191,15 +186,9 @@ * * *<replyto> -*A single email address to appear in the Reply-To: header, can +*A single email address to appear in the Rely-To: header, can *also be "sender" or "postmaster", this header is not -*set if this is omitted. -* -* -*<returnPath> -*A single email address to appear in the Return-Path: header, can -*also be "sender" or "postmaster" or "null"; this header is not -*set if this parameter is omitted. +*set if this is omited. * * *<prefix> @@ -208,11 +197,6 @@ *Undeliverable mail: * * -*<isReply> -*TRUE or FALSE, if true the IN_REPLY_TO header will be set to the -*id of the current message. -* -* *<static> * *

    TRUE or FALSE. If this is TRUE it tells the mailet that it can @@ -220,6 +204,8 @@ *their values. This will boost performance where a redirect task *doesn't contain any dynamic values. If this is FALSE, it tells the *mailet to recalculate the values for each e-mail processed.

    +*

    Note: If you use "magic words" such as "sender" in the <sender> +*tag, you must NOT use set static to TRUE.

    *

    This defaults to false.
    * * @@ -254,127 +240,69 @@ *<static>TRUE</static>
    *</mailet>

    * + * @author Danny Angus * */ - public class Redirect extends GenericMailet { - + /** * Controls certain log messages */ - protected boolean isDebug = false; - - private static class AddressMarker { - public static MailAddress SENDER; - public static MailAddress TO; - public static MailAddress RECIPIENTS; - public static MailAddress DELETE; - public static MailAddress UNALTERED; - public static MailAddress NULL; - - static { - try { - MailAddress SENDER = new MailAddress("sender","Address.Marker"); - MailAddress TO = new MailAddress("to","Address.Marker"); - MailAddress RECIPIENTS = new MailAddress("recipients","Address.Marker"); - MailAddress DELETE = new MailAddress("delete","Address.Marker"); - MailAddress UNALTERED = new MailAddress("unaltered","Address.Marker"); - MailAddress NULL = new MailAddress("null","Address.Marker"); - - } catch (Exception _) {} - } - } - - protected static class SpecialAddress { - public static final MailAddress SENDER = AddressMarker.SENDER; - public static final MailAddress TO = AddressMarker.TO; - public static final MailAddress RECIPIENTS = AddressMarker.RECIPIENTS; - public static final MailAddress DELETE = AddressMarker.DELETE; - public static final MailAddress UNALTERED = AddressMarker.UNALTERED; - public static final MailAddress NULL = AddressMarker.NULL; - } + private boolean isDebug = false; // The values that indicate how to attach the original mail - // to the new mail. + // to the redirected mail. - protected static final int UNALTERED = 0; + private static final int UNALTERED = 0; - protected static final int HEADS = 1; + private static final int HEADS = 1; - protected static final int BODY = 2; + private static final int BODY = 2; - protected static final int ALL = 3; + private static final int ALL = 3; - protected static final int NONE = 4; + private static final int NONE = 4; - protected static final int MESSAGE = 5; + private static final int MESSAGE = 5; - private boolean isStatic = false; - - private int attachmentType = NONE; - private int inLineType = BODY; + private InternetAddress[] apparentlyTo; private String messageText; private Collection recipients; private MailAddress replyTo; - private MailAddress returnPath; private MailAddress sender; - private String subjectPrefix; - private InternetAddress[] apparentlyTo; - private boolean attachError = false; - private boolean isReply = false; private RFC822DateFormat rfc822DateFormat = new RFC822DateFormat(); /** - * Returns a string describing this mailet. - * - * @return a string describing this mailet - */ - public String getMailetInfo() { - return "Resend Mailet"; - } - - /** - * Gets the passThrough init parameter. + * returns one of these values to indicate how to attach the original message + *
      + *
    • BODY : original message body is attached as plain text to the new message
    • + *
    • HEADS : original message headers are attached as plain text to the new message
    • + *
    • ALL : original is attached as plain text with all headers
    • + *
    • MESSAGE : original message is attached as type message/rfc822, a complete mail message.
    • + *
    • NONE : original is not attached
    • + *
    * - * @return true to allow the original message to continue through the processor, false to GHOST it */ - protected boolean getPassThrough() { - if(getInitParameter("passThrough") == null) { - return false; + public int getAttachmentType() { + if(getInitParameter("attachment") == null) { + return NONE; } else { - return new Boolean(getInitParameter("passThrough")).booleanValue(); + return getTypeCode(getInitParameter("attachment")); } } /** - * Gets the static init parameter. - * return true to reduce calls to getTo, getSender, getRecipients, getReplyTo, getReturnPath amd getMessage - * where these values don't change (eg hard coded, or got at startup from the mailet config)
    - * return false where any of these methods generate their results dynamically eg in response to the message being processed, - * or by reference to a repository of users - * Is a "getX()" method. - */ - protected boolean isStatic() { - return isStatic; - } - - /** - * Gets the inline init parameter. - * May return one of the following values to indicate how to append the original message - * to build the new message: - *
      + * returns one of these values to indicate how to append the original message + *
        *
      • UNALTERED : original message is the new message body
      • *
      • BODY : original message body is appended to the new message
      • *
      • HEADS : original message headers are appended to the new message
      • *
      • ALL : original is appended with all headers
      • *
      • NONE : original is not appended
      • - *
      - * Is a "getX()" method. - * - * @return the inline type value code + *
    */ - protected int getInLineType() throws MessagingException { + public int getInLineType() { if(getInitParameter("inline") == null) { return BODY; } else { @@ -383,61 +311,18 @@ } /** - * Gets the inline init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getInlineType()} - */ - protected int getInLineType(Mail originalMail) throws MessagingException { - int inLineType = (isStatic()) ? this.inLineType : getInLineType(); - return inLineType; - } - - /** - * Gets the attachment init parameter. - * May return one of the following values to indicate how to attach the original message - * to the new message: - *
      - *
    • BODY : original message body is attached as plain text to the new message
    • - *
    • HEADS : original message headers are attached as plain text to the new message
    • - *
    • ALL : original is attached as plain text with all headers
    • - *
    • MESSAGE : original message is attached as type message/rfc822, a complete mail message.
    • - *
    • NONE : original is not attached
    • - *
    - * Is a "getX()" method. - * - * @return the attachmentType value code - */ - protected int getAttachmentType() throws MessagingException { - if(getInitParameter("attachment") == null) { - return NONE; - } else { - return getTypeCode(getInitParameter("attachment")); - } - } - - /** - * Gets the attachment init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. + * Return a string describing this mailet. * - * @return {@link #getAttachmentType()} + * @return a string describing this mailet */ - protected int getAttachmentType(Mail originalMail) throws MessagingException { - int attachmentType = (isStatic()) ? this.attachmentType : getAttachmentType(); - return attachmentType; + public String getMailetInfo() { + return "Resend Mailet"; } /** - * Gets the message init parameter. - * Returns a message to which the original message can be attached/appended - * to build the new message. - * Is a "getX()" method. - * - * @return the message or an empty string if parameter is missing + * must return either an empty string, or a message to which the redirect can be attached/appended */ - protected String getMessage() throws MessagingException { + public String getMessage() { if(getInitParameter("message") == null) { return ""; } else { @@ -446,44 +331,25 @@ } /** - * Gets the message init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getMessage()} + * return true to allow thie original message to continue through the processor, false to GHOST it */ - protected String getMessage(Mail originalMail) throws MessagingException { - String messageText = (isStatic()) ? this.messageText : getMessage(); - return messageText; + public boolean getPassThrough() { + if(getInitParameter("passThrough") == null) { + return false; + } else { + return new Boolean(getInitParameter("passThrough")).booleanValue(); + } } - + /** - * Gets the recipients init parameter. - * Returns the collection of recipients of the new message. - * If the recipients init parameter is missing, - * returns the to init parameter. - * Is a "getX()" method. - * - * @return the addresses or SENDER or null if missing + * must return a Collection of recipient MailAddresses */ - protected Collection getRecipients() throws MessagingException { - Collection newRecipients = new HashSet(); - String addressList = (getInitParameter("recipients") == null) - ? getInitParameter("to") - : getInitParameter("recipients"); - // if nothing was specified, return null meaning no change - if (addressList == null) { - return null; - } - if(addressList.compareTo("postmaster") == 0) { - newRecipients.add(getMailetContext().getPostmaster()); - return newRecipients; - } - if(addressList.compareTo("sender") == 0) { - newRecipients.add(SpecialAddress.SENDER); - return newRecipients; - } - StringTokenizer st = new StringTokenizer(addressList, ",", false); + public Collection getRecipients() { + Collection newRecipients = new HashSet(); + String addressList = (getInitParameter("recipients") == null) + ? getInitParameter("to") + : getInitParameter("recipients"); + StringTokenizer st = new StringTokenizer(addressList, ",", false); while(st.hasMoreTokens()) { try { newRecipients.add(new MailAddress(st.nextToken())); @@ -495,123 +361,11 @@ } /** - * Gets the recipients init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getRecipients()}, replacing SENDER if applicable - */ - protected Collection getRecipients(Mail originalMail) throws MessagingException { - // TODO: implement MailAddress.RETURN_PATH - Collection recipients = (isStatic()) ? this.recipients : getRecipients(); - if (recipients != null && recipients.size() == 1) { - if (recipients.contains(SpecialAddress.SENDER)) { - recipients = new ArrayList(); - recipients.add(originalMail.getSender()); - } - } - return recipients; - } - - /** - * Sets the recipients of newMail to recipients. - */ - protected void setRecipients(Mail newMail, Collection recipients, Mail originalMail) throws MessagingException { - if (recipients != null) { - ((MailImpl) newMail).setRecipients(recipients); - if (isDebug) { - log("recipients set to: " + arrayToString(recipients.toArray())); - } - } - } - - /** - * Gets the to init parameter. - * Returns the "To:" recipients of the new message. - * If the to init parameter is missing, - * returns the recipients init parameter. - * Is a "getX()" method. - * - * @return the addresses or SENDER or UNALTERED or null meaning no change - */ - protected InternetAddress[] getTo() throws MessagingException { - String addressList = (getInitParameter("to") == null) - ? getInitParameter("recipients") - : getInitParameter("to"); - // if nothing was specified, return null meaning no change - if (addressList == null) { - return null; - } - if(addressList.compareTo("postmaster") == 0) { - InternetAddress[] iaarray = new InternetAddress[1]; - iaarray[0] = getMailetContext().getPostmaster().toInternetAddress(); - return iaarray; - } - if(addressList.compareTo("sender") == 0) { - InternetAddress[] iaarray = new InternetAddress[1]; - iaarray[0] = SpecialAddress.SENDER.toInternetAddress(); - return iaarray; - } - if(addressList.compareTo("unaltered") == 0) { - InternetAddress[] iaarray = new InternetAddress[1]; - iaarray[0] = SpecialAddress.UNALTERED.toInternetAddress(); - return iaarray; - } - StringTokenizer rec = new StringTokenizer(addressList, ","); - int tokensn = rec.countTokens(); - InternetAddress[] iaarray = new InternetAddress[tokensn]; - String tokenx = ""; - for(int i = 0; i < tokensn; ++i) { - try { - tokenx = rec.nextToken(); - iaarray[i] = new InternetAddress(tokenx); - } catch(Exception e) { - log("Internet address exception in getTo()"); - } - } - return iaarray; - } - - /** - * Gets the to init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getTo()}, replacing SENDER and UNALTERED if applicable - */ - protected InternetAddress[] getTo(Mail originalMail) throws MessagingException { - InternetAddress[] apparentlyTo = (isStatic()) ? this.apparentlyTo : getTo(); - if (apparentlyTo != null && apparentlyTo.length == 1) { - if (apparentlyTo[0].equals(SpecialAddress.SENDER.toInternetAddress())) { - apparentlyTo = new InternetAddress[1]; - apparentlyTo[0] = originalMail.getSender().toInternetAddress(); - } else if (apparentlyTo[0].equals(SpecialAddress.UNALTERED.toInternetAddress())) { - apparentlyTo = (InternetAddress[]) originalMail.getMessage().getRecipients(Message.RecipientType.TO); - } - } - return apparentlyTo; - } - - /** - * Sets the "To:" header of newMail to to. - */ - protected void setTo(Mail newMail, InternetAddress[] to, Mail originalMail) throws MessagingException { - if (to != null) { - newMail.getMessage().setRecipients(Message.RecipientType.TO, to); - if (isDebug) { - log("apparentlyTo set to: " + arrayToString(to)); - } - } - } - - /** - * Gets the replyto init parameter. - * Returns the Reply-To address of the new message. - * Is a "getX()" method. + * Returns the reply to address as a string. * - * @return an address or null if parameter is missing or == "sender" (null means "use original") + * @return the replyto address for the mail as a string */ - protected MailAddress getReplyTo() throws MessagingException { + public MailAddress getReplyTo() { String sr = getInitParameter("replyto"); if(sr != null) { MailAddress rv; @@ -620,7 +374,6 @@ return rv; } if(sr.compareTo("sender") == 0) { - // means no change return null; } try { @@ -634,104 +387,9 @@ } /** - * Gets the replyTo init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getReplyTo()} - */ - protected MailAddress getReplyTo(Mail originalMail) throws MessagingException { - MailAddress replyTo = (isStatic()) ? this.replyTo : getReplyTo(); - return replyTo; - } - - /** - * Sets the "Reply-To:" header of newMail to replyTo. - */ - protected void setReplyTo(Mail newMail, MailAddress replyTo, Mail originalMail) throws MessagingException { - if(replyTo != null) { - InternetAddress[] iart = new InternetAddress[1]; - iart[0] = replyTo.toInternetAddress(); - newMail.getMessage().setReplyTo(iart); - if (isDebug) { - log("replyTo set to: " + replyTo); - } - } - } - - /** - * Gets the returnPath init parameter. - * Returns the Return-Path of the new message. - * Is a "getX()" method. - * - * @return an address or NULL or SENDER or null if parameter is missing (null means "use original") - */ - protected MailAddress getReturnPath() throws MessagingException { - String sr = getInitParameter("returnPath"); - if(sr != null) { - MailAddress rv; - if(sr.compareTo("postmaster") == 0) { - return getMailetContext().getPostmaster(); - } - if(sr.compareTo("NULL") == 0) { - return SpecialAddress.NULL; - } - if(sr.compareTo("sender") == 0) { - return SpecialAddress.SENDER; - } - try { - rv = new MailAddress(sr); - return rv; - } catch(Exception e) { - log("Parse error in getReturnPath " + sr); - } - } - return null; - } - - /** - * Gets the returnPath init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getReturnPath()}, replacing SENDER if applicable, but not replacing NULL - */ - protected MailAddress getReturnPath(Mail originalMail) throws MessagingException { - MailAddress returnPath = (isStatic()) ? this.returnPath : getReturnPath(); - if (returnPath != null) { - if (returnPath == SpecialAddress.SENDER) { - returnPath = originalMail.getSender(); - } - } - return returnPath; - } - - /** - * Sets the "Return-Path:" header of newMail to returnPath. - */ - protected void setReturnPath(Mail newMail, MailAddress returnPath, Mail originalMail) throws MessagingException { - if(returnPath != null) { - String returnPathString; - if (returnPath == SpecialAddress.NULL) { - returnPathString = ""; - } else { - returnPathString = returnPath.toString(); - } - newMail.getMessage().setHeader(RFC2822Headers.RETURN_PATH, "<" + returnPathString + ">"); - if (isDebug) { - log("returnPath set to: " + returnPath); - } - } - } - - /** - * Gets the sender init parameter. - * Returns the new sender as a MailAddress. - * Is a "getX()" method. - * - * @return an address or null if parameter is missing or == "sender", meaning "use original" + * returns the senders address, as a MailAddress */ - protected MailAddress getSender() throws MessagingException { + public MailAddress getSender() { String sr = getInitParameter("sender"); if(sr != null) { MailAddress rv; @@ -740,7 +398,6 @@ return rv; } if(sr.compareTo("sender") == 0) { - // means no change: use FROM header; kept as is for compatibility return null; } try { @@ -754,43 +411,22 @@ } /** - * Gets the sender init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getSender()} - */ - protected MailAddress getSender(Mail originalMail) throws MessagingException { - MailAddress sender = (isStatic()) ? this.sender : getSender(); - return sender; - } - - /** - * Sets the sender and the "From:" header of newMail to sender. - * If sender is null will set such values to the ones in originalMail. + * return true to reduce calls to getTo, getSender, getRecipients, getReplyTo amd getMessage + * where these values don't change (eg hard coded, or got at startup from the mailet config)
    + * return false where any of these methods generate their results dynamically eg in response to the message being processed, + * or by refrence to a repository of users */ - protected void setSender(Mail newMail, MailAddress sender, Mail originalMail) throws MessagingException { - if (sender == null) { - MailAddress originalSender = new MailAddress(((InternetAddress) originalMail.getMessage().getFrom()[0]).getAddress()); - newMail.getMessage().setHeader(RFC2822Headers.FROM, originalMail.getMessage().getHeader(RFC2822Headers.FROM, ",")); - ((MailImpl) newMail).setSender(originalSender); - } else { - newMail.getMessage().setFrom(sender.toInternetAddress()); - ((MailImpl) newMail).setSender(sender); - if (isDebug) { - log("sender set to: " + sender); - } + public boolean isStatic() { + if(getInitParameter("static") == null) { + return false; } + return new Boolean(getInitParameter("static")).booleanValue(); } - + /** - * Gets the prefix init parameter. - * Returns a prefix for the new message subject. - * Is a "getX()" method. - * - * @return the prefix or an empty string if parameter is missing + * return a prefix for the message subject */ - protected String getSubjectPrefix() throws MessagingException { + public String getSubjectPrefix() { if(getInitParameter("prefix") == null) { return ""; } else { @@ -799,41 +435,31 @@ } /** - * Gets the subjectPrefix init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #getSubjectPrefix()} - */ - protected String getSubjectPrefix(Mail originalMail) throws MessagingException { - String subjectPrefix = (isStatic()) ? this.subjectPrefix : getSubjectPrefix(); - return subjectPrefix; - } - - /** - * Builds the subject of newMail appending the subject - * of originalMail to subjectPrefix. + * returns an array of InternetAddress 'es for the To: header */ - protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail) throws MessagingException { - String subject = originalMail.getMessage().getSubject(); - if (subject == null) { - subject = ""; - } - newMail.getMessage().setSubject(subjectPrefix + subject); - if (isDebug) { - log("subjectPrefix set to: " + subjectPrefix); + public InternetAddress[] getTo() { + String addressList = (getInitParameter("to") == null) + ? getInitParameter("recipients") : getInitParameter("to"); + StringTokenizer rec = new StringTokenizer(addressList, ","); + int tokensn = rec.countTokens(); + InternetAddress[] iaarray = new InternetAddress[tokensn]; + String tokenx = ""; + for(int i = 0; i < tokensn; ++i) { + try { + tokenx = rec.nextToken(); + iaarray[i] = new InternetAddress(tokenx); + } catch(Exception e) { + log("Internet address exception in getTo()"); + } } + return iaarray; } - + /** - * Gets the attachError init parameter. - * Returns a boolean indicating whether to append a description of any error to the main body part - * of the new message, if getInlineType does not return "UNALTERED". - * Is a "getX()" method. - * - * @return true or false; false if init parameter missing + * return true to append a description of any error to the main body part + * if getInlineType does not return "UNALTERED" */ - protected boolean attachError() throws MessagingException { + public boolean attachError() { if(getInitParameter("attachError") == null) { return false; } else { @@ -842,101 +468,31 @@ } /** - * Gets the attachError init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #attachError()} - */ - protected boolean attachError(Mail originalMail) throws MessagingException { - boolean attachError = (isStatic()) ? this.attachError : attachError(); - return attachError; - } - - /** - * Gets the isReply init parameter. - * Returns a boolean indicating whether the new message must be considered - * a reply to the original message, setting the IN_REPLY_TO header of the new - * message to the id of the original message. - * Is a "getX()" method. - * - * @return true or false; false if init parameter missing - */ - protected boolean isReply() throws MessagingException { - if(getInitParameter("isReply") == null) { - return false; - } - return new Boolean(getInitParameter("isReply")).booleanValue(); - } - - /** - * Gets the isReply init parameter, - * built dynamically using the original Mail object. - * Is a "getX(Mail)" method. - * - * @return {@link #isReply()} - */ - protected boolean isReply(Mail originalMail) throws MessagingException { - boolean isReply = (isStatic()) ? this.isReply : isReply(); - return isReply; - } - - /** - * Sets the "In-Reply-To:" header of newMail to the "Message-Id:" of - * originalMail, if isReply is true. - */ - protected void setIsReply(Mail newMail, boolean isReply, Mail originalMail) throws MessagingException { - if (isReply) { - String messageId = originalMail.getMessage().getMessageID(); - if (messageId != null) { - newMail.getMessage().setHeader(RFC2822Headers.IN_REPLY_TO, messageId); - if (isDebug) { - log("IN_REPLY_TO set to: " + messageId); - } - } - } - } - - /** - * Mailet initialization routine. - * Will setup static values for each "x" initialization parameter in config.xml, - * using getX(), if isStatic() returns true. - * - */ + * init will setup static values for sender, recipients, message text, and reply to + *
    if isStatic() returns true + * it calls getSender(), getReplyTo(), getMessage(), and getRecipients() and getTo() + * + */ public void init() throws MessagingException { if (isDebug) { log("Redirect init"); } isDebug = (getInitParameter("debug") == null) ? false : new Boolean(getInitParameter("debug")).booleanValue(); - - isStatic = (getInitParameter("static") == null) ? false : new Boolean(getInitParameter("static")).booleanValue(); - if(isStatic()) { - attachmentType = getAttachmentType(); - inLineType = getInLineType(); - messageText = getMessage(); - recipients = getRecipients(); - replyTo = getReplyTo(); - returnPath = getReturnPath(); - sender = getSender(); - subjectPrefix = getSubjectPrefix(); - apparentlyTo = getTo(); - attachError = attachError(); - isReply = isReply(); + sender = getSender(); + replyTo = getReplyTo(); + messageText = getMessage(); + recipients = getRecipients(); + apparentlyTo = getTo(); if (isDebug) { StringBuffer logBuffer = new StringBuffer(1024) - .append("static, sender=").append(sender) - .append(", replyTo=").append(replyTo) - .append(", returnPath=").append(returnPath) - .append(", message=").append(messageText) - .append(", recipients=").append(arrayToString(recipients.toArray())) - .append(", subjectPrefix=").append(subjectPrefix) - .append(", apparentlyTo=").append(arrayToString(apparentlyTo)) - .append(", attachError=").append(attachError) - .append(", isReply=").append(isReply) - .append(", attachmentType=").append(attachmentType) - .append(", inLineType=").append(inLineType) + .append("static, sender=") + .append(sender) + .append(", replyTo=") + .append(replyTo) + .append(", message=") + .append(messageText) .append(" "); log(logBuffer.toString()); } @@ -944,103 +500,133 @@ } /** - * Service does the hard work,and redirects the originalMail in the form specified. + * Service does the hard work,and redirects the mail in the form specified * - * @param originalMail the mail to process and redirect - * @throws MessagingException if a problem arises formulating the redirected mail + * @param mail the mail to process and redirect + * @throws MessagingException if a problem arising formulating the redirected mail */ - public void service(Mail originalMail) throws MessagingException { - - boolean keepMessageId = false; - - // duplicates the Mail object, to be able to modify the new mail keeping the original untouched - Mail newMail = ((MailImpl) originalMail).duplicate(newName((MailImpl)originalMail)); - - if (isDebug) { - MailImpl newMailImpl = (MailImpl) newMail; - log("New mail - sender: " + newMailImpl.getSender() - + ", recipients: " + arrayToString(newMailImpl.getRecipients().toArray()) - + ", name: " + newMailImpl.getName() - + ", remoteHost: " + newMailImpl.getRemoteHost() - + ", remoteAddr: " + newMailImpl.getRemoteAddr() - + ", state: " + newMailImpl.getState() - + ", lastUpdated: " + newMailImpl.getLastUpdated() - + ", errorMessage: " + newMailImpl.getErrorMessage()); + public void service(Mail mail) throws MessagingException { + if(!isStatic()) { + sender = getSender(); + replyTo = getReplyTo(); + messageText = getMessage(); + recipients = getRecipients(); + apparentlyTo = getTo(); } - + + MimeMessage message = mail.getMessage(); + MimeMessage reply = null; //Create the message - if(getInLineType(originalMail) != UNALTERED) { + if(getInLineType() != UNALTERED) { if (isDebug) { - log("Alter message inline=:" + getInLineType(originalMail)); + log("Alter message inline=:" + getInLineType()); } - newMail.setMessage(new MimeMessage(Session.getDefaultInstance(System.getProperties(), - null))); - - // handle the new message if altered - buildAlteredMessage(newMail, originalMail); - - setTo(newMail, getTo(originalMail), originalMail); - + reply = new MimeMessage(Session.getDefaultInstance(System.getProperties(), + null)); + StringWriter sout = new StringWriter(); + PrintWriter out = new PrintWriter(sout, true); + Enumeration heads = message.getAllHeaderLines(); + String head = ""; + StringBuffer headBuffer = new StringBuffer(1024); + while(heads.hasMoreElements()) { + headBuffer.append(heads.nextElement().toString()).append("\n"); + } + head = headBuffer.toString(); + boolean all = false; + if(messageText != null) { + out.println(messageText); + } + switch(getInLineType()) { + case ALL: //ALL: + all = true; + case HEADS: //HEADS: + out.println("Message Headers:"); + out.println(head); + if(!all) { + break; + } + case BODY: //BODY: + out.println("Message:"); + try { + out.println(message.getContent().toString()); + } catch(Exception e) { + out.println("body unavailable"); + } + break; + default: + case NONE: //NONE: + break; + } + MimeMultipart multipart = new MimeMultipart(); + //Add message as the first mime body part + MimeBodyPart part = new MimeBodyPart(); + part.setText(sout.toString()); + part.setDisposition("inline"); + multipart.addBodyPart(part); + if(getAttachmentType() != NONE) { + part = new MimeBodyPart(); + switch(getAttachmentType()) { + case HEADS: //HEADS: + part.setText(head); + break; + case BODY: //BODY: + try { + part.setText(message.getContent().toString()); + } catch(Exception e) { + part.setText("body unavailable"); + } + break; + case ALL: //ALL: + StringBuffer textBuffer = + new StringBuffer(1024) + .append(head) + .append("\n\n") + .append(message.toString()); + part.setText(textBuffer.toString()); + break; + case MESSAGE: //MESSAGE: + part.setContent(message, "message/rfc822"); + break; + } + part.setDisposition("Attachment"); + multipart.addBodyPart(part); + } + reply.setContent(multipart); + reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); + reply.setRecipients(Message.RecipientType.TO, apparentlyTo); } else { // if we need the original, create a copy of this message to redirect - if (getPassThrough()) { - newMail.setMessage(new MimeMessage(originalMail.getMessage())); - } + reply = getPassThrough() ? new MimeMessage(message) : message; if (isDebug) { log("Message resent unaltered."); } - keepMessageId = true; } - //Set additional headers - - setRecipients(newMail, getRecipients(originalMail), originalMail); - - setSubjectPrefix(newMail, getSubjectPrefix(originalMail), originalMail); - - if(newMail.getMessage().getHeader(RFC2822Headers.DATE) == null) { - newMail.getMessage().setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date())); + reply.setSubject(getSubjectPrefix() + message.getSubject()); + if(reply.getHeader(RFC2822Headers.DATE) == null) { + reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date())); } - setReplyTo(newMail, getReplyTo(originalMail), originalMail); - - setReturnPath(newMail, getReturnPath(originalMail), originalMail); - - setSender(newMail, getSender(originalMail), originalMail); - - setIsReply(newMail, isReply(originalMail), originalMail); - - newMail.getMessage().saveChanges(); - - if (keepMessageId) { - setMessageId(newMail, originalMail); + // + + if(replyTo != null) { + InternetAddress[] iart = new InternetAddress[1]; + iart[0] = replyTo.toInternetAddress(); + reply.setReplyTo(iart); + } + if(sender == null) { + reply.setHeader(RFC2822Headers.FROM, message.getHeader(RFC2822Headers.FROM, ",")); + sender = new MailAddress(((InternetAddress)message.getFrom()[0]).getAddress()); + } else { + reply.setFrom(sender.toInternetAddress()); } - //Send it off... - getMailetContext().sendMail(newMail); - + getMailetContext().sendMail(sender, recipients, reply); if(!getPassThrough()) { - originalMail.setState(Mail.GHOST); + mail.setState(Mail.GHOST); } } - private static final java.util.Random random = new java.util.Random(); // Used to generate new mail names - /** - * Create a unique new primary key name. - * - * @param mail the mail to use as the basis for the new mail name - * - * @return a new name - */ - private String newName(MailImpl mail) { - StringBuffer nameBuffer = - new StringBuffer(64) - .append(mail.getName()) - .append("-!") - .append(random.nextInt(1048576)); - return nameBuffer.toString(); - } - /** * A private method to convert types from string to int. * @@ -1070,144 +656,5 @@ return MESSAGE; } return NONE; - } - - /** - * Utility method for obtaining a string representation of an array of Objects. - */ - private String arrayToString(Object[] array) { - StringBuffer sb = new StringBuffer(1024); - sb.append("["); - for (int i = 0; i < array.length; i++) { - sb.append(array[i]); - } - sb.append("]"); - return sb.toString(); - } - - /** - * Builds the message of the newMail in case it has to be altered. - * - * @param originalMail the original Mail object - * @param newMail the Mail object to build - */ - protected void buildAlteredMessage(Mail newMail, Mail originalMail) throws MessagingException { - - MimeMessage message = originalMail.getMessage(); - - StringWriter sout = new StringWriter(); - PrintWriter out = new PrintWriter(sout, true); - Enumeration heads = message.getAllHeaderLines(); - String head = ""; - StringBuffer headBuffer = new StringBuffer(1024); - while(heads.hasMoreElements()) { - headBuffer.append(heads.nextElement().toString()).append("\n"); - } - head = headBuffer.toString(); - boolean all = false; - - String messageText = getMessage(originalMail); - if(messageText != null) { - out.println(messageText); - } - - switch(getInLineType(originalMail)) { - case ALL: //ALL: - all = true; - case HEADS: //HEADS: - out.println("Message Headers:"); - out.println(head); - if(!all) { - break; - } - case BODY: //BODY: - out.println("Message:"); - try { - out.println(message.getContent().toString()); - } catch(Exception e) { - out.println("body unavailable"); - } - break; - default: - case NONE: //NONE: - break; - } - - try { - //Create the message body - MimeMultipart multipart = new MimeMultipart("mixed"); - - // Create the message - MimeMultipart mpContent = new MimeMultipart("alternative"); - MimeBodyPart contentPartRoot = new MimeBodyPart(); - contentPartRoot.setContent(mpContent); - - multipart.addBodyPart(contentPartRoot); - - MimeBodyPart part = new MimeBodyPart(); - part.setText(sout.toString()); - part.setDisposition("inline"); - mpContent.addBodyPart(part); - if(getAttachmentType() != NONE) { - part = new MimeBodyPart(); - switch(getAttachmentType()) { - case HEADS: //HEADS: - part.setText(head); - break; - case BODY: //BODY: - try { - part.setText(message.getContent().toString()); - } catch(Exception e) { - part.setText("body unavailable"); - } - break; - case ALL: //ALL: - StringBuffer textBuffer = - new StringBuffer(1024) - .append(head) - .append("\n\n") - .append(message.toString()); - part.setText(textBuffer.toString()); - break; - case MESSAGE: //MESSAGE: - part.setContent(message, "message/rfc822"); - break; - } - if ((message.getSubject() != null) && (message.getSubject().trim().length() > 0)) { - part.setFileName(message.getSubject().trim()); - } else { - part.setFileName("No Subject"); - } - part.setDisposition("Attachment"); - multipart.addBodyPart(part); - } - //if set, attach the full stack trace - if (attachError(originalMail) && originalMail.getErrorMessage() != null) { - part = new MimeBodyPart(); - part.setContent(originalMail.getErrorMessage(), "text/plain"); - part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); - part.setFileName("Reasons"); - part.setDisposition(javax.mail.Part.ATTACHMENT); - multipart.addBodyPart(part); - } - newMail.getMessage().setContent(multipart); - newMail.getMessage().setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); - - } catch (Exception ioe) { - throw new MessagingException("Unable to create multipart body", ioe); - } - } - - /** - * Sets the message id of originalMail into newMail. - */ - private void setMessageId(Mail newMail, Mail originalMail) throws MessagingException { - String messageId = originalMail.getMessage().getMessageID(); - if (messageId != null) { - newMail.getMessage().setHeader(RFC2822Headers.MESSAGE_ID, messageId); - if (isDebug) { - log("MESSAGE_ID restored to: " + messageId); - } - } } } --------------------------------------------------------------------- To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: james-dev-help@jakarta.apache.org