You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2003/06/03 00:37:10 UTC
cvs commit: jakarta-james/src/java/org/apache/james/transport/mailets AbstractNotify.java AbstractRedirect.java Bounce.java NotifyPostmaster.java NotifySender.java Redirect.java Forward.java
noel 2003/06/02 15:37:10
Modified: src/java/org/apache/james/transport/mailets Tag:
branch_2_1_fcs NotifyPostmaster.java
NotifySender.java Redirect.java Forward.java
Added: src/java/org/apache/james/transport/mailets Tag:
branch_2_1_fcs AbstractNotify.java
AbstractRedirect.java Bounce.java
Log:
New and improved Redirect hierarchy. AbstractRedirect is the new base. Redirect, Forward, and AbstractNotify extend AbstractRedirect. Bounce, NotifySender and NotifyPostmaster extend AbstractNotify.
Revision Changes Path
No revision
No revision
1.9.4.3 +95 -183 jakarta-james/src/java/org/apache/james/transport/mailets/NotifyPostmaster.java
Index: NotifyPostmaster.java
===================================================================
RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/NotifyPostmaster.java,v
retrieving revision 1.9.4.2
retrieving revision 1.9.4.3
diff -u -r1.9.4.2 -r1.9.4.3
--- NotifyPostmaster.java 8 Mar 2003 21:54:08 -0000 1.9.4.2
+++ NotifyPostmaster.java 2 Jun 2003 22:37:07 -0000 1.9.4.3
@@ -78,209 +78,121 @@
import java.io.StringWriter;
import java.util.Date;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
+import java.util.Collection;
+import java.util.Iterator;
/**
- * Sends an error message to the sender of a message (that's typically landed in
- * the error mail repository). You can optionally specify a sender of the error
- * message. If you do not specify one, it will use the postmaster's address
+ * <P>Sends a notification message to the Postmaster.</P>
+ * <P>A sender of the notification message can optionally be specified.
+ * If one is not specified, the postmaster's address will be used.<BR>
+ * The "To:" header of the notification message can be set to "unaltered";
+ * if missing will be set to the postmaster.<BR>
+ * A notice text can be specified, and in such case will be inserted into the
+ * notification inline text.<BR>
+ * If the notified message has an "error message" set, it will be inserted into the
+ * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
+ * is set to true, such error message will be attached to the notification message.<BR>
+ * The notified messages are attached in their entirety (headers and
+ * content) and the resulting MIME part type is "message/rfc822".</P>
+ * <P>passThrough is <B>true</B>.</P>
+ *
+ * <P>Sample configuration:</P>
+ * <PRE><CODE>
+ * <mailet match="All" class="NotifyPostmaster">
+ * <sendingAddress><I>an address or postmaster or sender, default=postmaster</I></sendingAddress>
+ * <attachStackTrace><I>true or false, default=false</I></attachStackTrace>
+ * <notice><I>notice attached to the message (optional)</I></notice>
+ * <to><I>unaltered (optional, defaults to postmaster)</I></to>
+ * </mailet>
+ * </CODE></PRE>
*
- * Sample configuration:
- * <mailet match="All" class="NotifyPostmaster">
- * <sendingAddress>nobounce@localhost</sendingAddress>
- * <attachStackTrace>true</attachStackTrace>
- * <notice>Notice attached to the message (optional)</notice>
- * </mailet>
+ * <P>The behaviour of this mailet is equivalent to using Redirect with the following
+ * configuration:</P>
+ * <PRE><CODE>
+ * <mailet match="All" class="Redirect">
+ * <sender><I>an address or postmaster or sender, default=postmaster</I></sender>
+ * <attachError><I>true or false, default=false</I></attachError>
+ * <message><I><B>dynamically built</B></I></message>
+ * <passThrough>true</passThrough>
+ * <to><I>unaltered or postmaster<</I>;/to>
+ * <recipients><B>postmaster</B></recipients>
+ * <inline>none</inline>
+ * <attachment>message</attachment>
+ * <isReply>true</isReply>
+ * <static>true</static>
+ * </mailet>
+ * </CODE></PRE>
*
- * @author Serge Knystautas <se...@lokitech.com>
- * @author Ivan Seskar <is...@upsideweb.com>
- * @author Danny Angus <da...@thought.co.uk>
*/
-public class NotifyPostmaster extends GenericMailet {
-
+public class NotifyPostmaster extends AbstractNotify {
+
/**
- * The sender address for the reply message
- */
- MailAddress notifier = null;
-
- /**
- * Whether exception stack traces should be attached to the error
- * messages
- */
- boolean attachStackTrace = false;
-
- /**
- * The text of the reply notice
+ * Return a string describing this mailet.
+ *
+ * @return a string describing this mailet
*/
- String noticeText = null;
-
+ public String getMailetInfo() {
+ return "NotifyPostmaster Mailet";
+ }
+ /* ******************************************************************** */
+ /* ****************** Begin of getX and setX methods ****************** */
+ /* ******************************************************************** */
+
+ /**
+ * @return the postmaster address
+ */
+ protected Collection getRecipients() {
+ Collection newRecipients = new HashSet();
+ newRecipients.add(getMailetContext().getPostmaster());
+ return newRecipients;
+ }
+
/**
- * The date format object used to generate RFC 822 compliant date headers
+ * @return UNALTERED if specified or postmaster if missing
*/
- private RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();
-
+ protected InternetAddress[] getTo() throws MessagingException {
+ String addressList = getInitParameter("to");
+ InternetAddress[] iaarray = new InternetAddress[1];
+ iaarray[0] = getMailetContext().getPostmaster().toInternetAddress();
+ if (addressList != null) {
+ MailAddress specialAddress = getSpecialAddress(addressList,
+ new String[] {"postmaster", "unaltered"});
+ if (specialAddress != null) {
+ iaarray[0] = specialAddress.toInternetAddress();
+ } else {
+ log("\"to\" parameter ignored, set to postmaster");
+ }
+ }
+ return iaarray;
+ }
+
/**
- * Initialize the mailet, loading all configuration parameters.
- *
- * @throws MessagingException
+ * @return "Re:"
*/
- public void init() throws MessagingException {
- if (getInitParameter("sendingAddress") == null) {
- notifier = getMailetContext().getPostmaster();
- } else {
- notifier = new MailAddress(getInitParameter("sendingAddress"));
- }
- if (getInitParameter("notice") == null) {
- noticeText = "We were unable to deliver the attached message because of an error in the mail server.";
- } else {
- noticeText = getInitParameter("notice");
- }
- try {
- attachStackTrace = new Boolean(getInitParameter("attachStackTrace")).booleanValue();
- } catch (Exception e) {
- // Ignore exception, default to false
- }
+ protected String getSubjectPrefix() {
+ return "Re:";
}
-
+
/**
- * Sends a message to the postmaster with the original message as to why it failed.
- *
- * @param mail the mail being processed
- *
- * @throws MessagingException if an error occurs while formulating the message to the sender
+ * Builds the subject of <I>newMail</I> appending the subject
+ * of <I>originalMail</I> to <I>subjectPrefix</I>, but avoiding a duplicate.
*/
- public void service(Mail mail) throws MessagingException {
- MimeMessage message = mail.getMessage();
- //Create the reply message
- MimeMessage reply = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null));
-
- //Create the list of recipients in the Address[] format
- InternetAddress[] rcptAddr = new InternetAddress[1];
- rcptAddr[0] = getMailetContext().getPostmaster().toInternetAddress();
- reply.setRecipients(Message.RecipientType.TO, rcptAddr);
-
- //Set the sender...
- reply.setFrom(notifier.toInternetAddress());
-
- //Create the message
- StringWriter sout = new StringWriter();
- PrintWriter out = new PrintWriter(sout, true);
-
- // First add the "local" notice
- // (either from conf or generic error message)
- out.println(noticeText);
- // And then the message from other mailets
- if (mail.getErrorMessage() != null) {
- out.println();
- out.println("Error message below:");
- out.println(mail.getErrorMessage());
- }
- out.println();
- out.println("Message details:");
-
- if (message.getSubject() != null) {
- out.println(" Subject: " + message.getSubject());
- }
- if (message.getSentDate() != null) {
- out.println(" Sent date: " + message.getSentDate());
- }
- out.println(" MAIL FROM: " + mail.getSender());
- Iterator rcptTo = mail.getRecipients().iterator();
- out.println(" RCPT TO: " + rcptTo.next());
- while (rcptTo.hasNext()) {
- out.println(" " + rcptTo.next());
- }
- String[] addresses = null;
- addresses = message.getHeader(RFC2822Headers.FROM);
- if (addresses != null) {
- out.print(" From: ");
- for (int i = 0; i < addresses.length; i++) {
- out.print(addresses[i] + " ");
- }
- out.println();
- }
- addresses = message.getHeader(RFC2822Headers.TO);
- if (addresses != null) {
- out.print(" To: ");
- for (int i = 0; i < addresses.length; i++) {
- out.print(addresses[i] + " ");
- }
- out.println();
- }
- addresses = message.getHeader(RFC2822Headers.CC);
- if (addresses != null) {
- out.print(" CC: ");
- for (int i = 0; i < addresses.length; i++) {
- out.print(addresses[i] + " ");
- }
- out.println();
- }
- out.println(" Size (in bytes): " + message.getSize());
- if (message.getLineCount() >= 0) {
- out.println(" Number of lines: " + message.getLineCount());
- }
-
-
- try {
- //Create the message body
- MimeMultipart multipart = new MimeMultipart();
- //Add message as the first mime body part
- MimeBodyPart part = new MimeBodyPart();
- part.setContent(sout.toString(), "text/plain");
- part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
- multipart.addBodyPart(part);
-
- //Add the original message as the second mime body part
- part = new MimeBodyPart();
- part.setContent(message.getContent(), message.getContentType());
- part.setHeader(RFC2822Headers.CONTENT_TYPE, message.getContentType());
- multipart.addBodyPart(part);
-
- //if set, attach the full stack trace
- if (attachStackTrace && mail.getErrorMessage() != null) {
- part = new MimeBodyPart();
- part.setContent(mail.getErrorMessage(), "text/plain");
- part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
- multipart.addBodyPart(part);
- }
-
- reply.setContent(multipart);
- reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType());
- } catch (IOException ioe) {
- throw new MailetException("Unable to create multipart body");
- }
-
- //Create the list of recipients in our MailAddress format
- Set recipients = new HashSet();
- recipients.add(getMailetContext().getPostmaster());
-
- //Set additional headers
- if (reply.getHeader(RFC2822Headers.DATE)==null) {
- reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));
- }
- String subject = message.getSubject();
+ protected void setSubjectPrefix(Mail newMail, String subjectPrefix, Mail originalMail) throws MessagingException {
+ String subject = originalMail.getMessage().getSubject();
if (subject == null) {
subject = "";
}
- if (subject.indexOf("Re:") == 0) {
- reply.setSubject(subject);
+ if (subject.indexOf(subjectPrefix) == 0) {
+ newMail.getMessage().setSubject(subject);
} else {
- reply.setSubject("Re:" + subject);
+ newMail.getMessage().setSubject(subjectPrefix + subject);
}
- reply.setHeader(RFC2822Headers.IN_REPLY_TO, message.getMessageID());
-
- //Send it off...
- getMailetContext().sendMail(notifier, recipients, reply);
- }
-
- /**
- * Return a string describing this mailet.
- *
- * @return a string describing this mailet
- */
- public String getMailetInfo() {
- return "NotifyPostmaster Mailet";
}
+
+ /* ******************************************************************** */
+ /* ******************* End of getX and setX methods ******************* */
+ /* ******************************************************************** */
+
}
1.10.4.4 +76 -180 jakarta-james/src/java/org/apache/james/transport/mailets/NotifySender.java
Index: NotifySender.java
===================================================================
RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/NotifySender.java,v
retrieving revision 1.10.4.3
retrieving revision 1.10.4.4
diff -u -r1.10.4.3 -r1.10.4.4
--- NotifySender.java 9 May 2003 02:12:20 -0000 1.10.4.3
+++ NotifySender.java 2 Jun 2003 22:37:07 -0000 1.10.4.4
@@ -79,202 +79,98 @@
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
+import java.util.Collection;
+import java.util.Iterator;
/**
- * Sends an error message to the sender of a message (that's typically landed in
- * the error mail repository). You can optionally specify a sender of the error
- * message. If you do not specify one, it will use the postmaster's address
+ * <P>Sends a notification message to the sender of a message.</P>
+ * <P>A sender of the notification message can optionally be specified.
+ * If one is not specified, the postmaster's address will be used.<BR>
+ * The "To:" header of the notification message can be set to "unaltered";
+ * if missing will be set to the sender of the notified message.<BR>
+ * A notice text can be specified, and in such case will be inserted into the
+ * notification inline text.<BR>
+ * If the notified message has an "error message" set, it will be inserted into the
+ * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
+ * is set to true, such error message will be attached to the notification message.<BR>
+ * The notified messages are attached in their entirety (headers and
+ * content) and the resulting MIME part type is "message/rfc822".</P>
+ * <P>passThrough is <B>true</B>.</P>
*
- * Sample configuration:
+ * <P>Sample configuration:</P>
+ * <PRE><CODE>
* <mailet match="All" class="NotifySender">
- * <sendingAddress>nobounce@localhost</sendingAddress>
- * <attachStackTrace>true</attachStackTrace>
- * <notice>Notice attached to the message (optional)</notice>
+ * <sendingAddress><I>an address or postmaster</I></sendingAddress>
+ * <attachStackTrace><I>true or false, default=false</I></attachStackTrace>
+ * <notice><I>notice attached to the message (optional)</I></notice>
+ * <to><I>unaltered (optional, defaults to sender)</I></to>
* </mailet>
+ * </CODE></PRE>
+ *
+ * <P>The behaviour of this mailet is equivalent to using Redirect with the following
+ * configuration:</P>
+ * <PRE><CODE>
+ * <mailet match="All" class="Redirect">
+ * <sender><I>an address or postmaster</I></sender>
+ * <attachError><I>true or false, default=false</I></attachError>
+ * <message><I><B>dynamically built</B></I></message>
+ * <passThrough>true</passThrough>
+ * <to><I>unaltered or sender<</I>;/to>
+ * <recipients><B>sender</B></recipients>
+ * <inline>none</inline>
+ * <attachment>message</attachment>
+ * <isReply>true</isReply>
+ * <static>true</static>
+ * </mailet>
+ * </CODE></PRE>
*
*/
-public class NotifySender extends GenericMailet {
-
- /**
- * The sender address for the reply message
- */
- MailAddress notifier = null;
-
- /**
- * Whether exception stack traces should be attached to the error
- * messages
- */
- boolean attachStackTrace = false;
-
- /**
- * The text of the reply notice
- */
- String noticeText = null;
-
+public class NotifySender extends AbstractNotify {
+
/**
- * The date format object used to generate RFC 822 compliant date headers
- */
- private RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();
-
- /**
- * Initialize the mailet, loading all configuration parameters.
+ * Return a string describing this mailet.
*
- * @throws MessagingException
+ * @return a string describing this mailet
*/
- public void init() throws MessagingException {
- if (getInitParameter("sendingAddress") == null) {
- notifier = getMailetContext().getPostmaster();
- } else {
- notifier = new MailAddress(getInitParameter("sendingAddress"));
- }
- if (getInitParameter("notice") == null) {
- noticeText = "We were unable to deliver the attached message because of an error in the mail server.";
- } else {
- noticeText = getInitParameter("notice");
- }
- try {
- attachStackTrace = new Boolean(getInitParameter("attachStackTrace")).booleanValue();
- } catch (Exception e) {
- // Ignore exception, default to false
- }
+ public String getMailetInfo() {
+ return "NotifySender Mailet";
}
-
+
+ /* ******************************************************************** */
+ /* ****************** Begin of getX and setX methods ****************** */
+ /* ******************************************************************** */
+
+ /**
+ * @return SENDER, indicating the sender of the current mail
+ */
+ protected Collection getRecipients() {
+ Collection newRecipients = new HashSet();
+ newRecipients.add(SpecialAddress.SENDER);
+ return newRecipients;
+ }
+
/**
- * Sends a message back to the sender with the message as to why it failed.
- *
- * @param mail the mail being processed
- *
- * @throws MessagingException if an error occurs while formulating the message to the sender
+ * @return UNALTERED if specified or SENDER if missing
*/
- public void service(Mail mail) throws MessagingException {
- MimeMessage message = mail.getMessage();
- //Create the reply message
- MimeMessage reply = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null));
-
- //Create the list of recipients in the Address[] format
- InternetAddress[] rcptAddr = new InternetAddress[1];
- rcptAddr[0] = mail.getSender().toInternetAddress();
- reply.setRecipients(Message.RecipientType.TO, rcptAddr);
-
- //Set the sender...
- reply.setFrom(notifier.toInternetAddress());
-
- //Create the message
- StringWriter sout = new StringWriter();
- PrintWriter out = new PrintWriter(sout, true);
-
- // First add the "local" notice
- // (either from conf or generic error message)
- out.println(noticeText);
- // And then the message from other mailets
- if (mail.getErrorMessage() != null) {
- out.println();
- out.println("Error message below:");
- out.println(mail.getErrorMessage());
- }
- out.println();
- out.println("Message details:");
-
- if (message.getSubject() != null) {
- out.println(" Subject: " + message.getSubject());
- }
- if (message.getSentDate() != null) {
- out.println(" Sent date: " + message.getSentDate());
- }
- String[] rcpts = null;
- rcpts = message.getHeader(RFC2822Headers.TO);
- if (rcpts != null) {
- out.print(" To: ");
- for (int i = 0; i < rcpts.length; i++) {
- out.print(rcpts[i] + " ");
- }
- out.println();
- }
- rcpts = message.getHeader(RFC2822Headers.CC);
- if (rcpts != null) {
- out.print(" CC: ");
- for (int i = 0; i < rcpts.length; i++) {
- out.print(rcpts[i] + " ");
- }
- out.println();
- }
- out.println(" Size (in bytes): " + message.getSize());
- if (message.getLineCount() >= 0) {
- out.println(" Number of lines: " + message.getLineCount());
- }
-
- 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());
- mpContent.addBodyPart(part);
-
- //Add the original message as the second mime body part
- part = new MimeBodyPart();
- part.setContent(message, "message/rfc822");
- if ((message.getSubject() != null) && (message.getSubject().trim().length() > 0)) {
- part.setFileName(message.getSubject().trim());
+ protected InternetAddress[] getTo() throws MessagingException {
+ String addressList = getInitParameter("to");
+ InternetAddress[] iaarray = new InternetAddress[1];
+ iaarray[0] = SpecialAddress.SENDER.toInternetAddress();
+ if (addressList != null) {
+ MailAddress specialAddress = getSpecialAddress(addressList,
+ new String[] {"sender", "unaltered"});
+ if (specialAddress != null) {
+ iaarray[0] = specialAddress.toInternetAddress();
} else {
- part.setFileName("No Subject");
+ log("\"to\" parameter ignored, set to sender");
}
- part.setDisposition(javax.mail.Part.ATTACHMENT);
- multipart.addBodyPart(part);
-
- //if set, attach the full stack trace
- if (attachStackTrace && mail.getErrorMessage() != null) {
- part = new MimeBodyPart();
- part.setContent(mail.getErrorMessage(), "text/plain");
- part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain");
- part.setFileName("Reasons");
- part.setDisposition(javax.mail.Part.ATTACHMENT);
- multipart.addBodyPart(part);
- }
-
- reply.setContent(multipart);
- } catch (Exception ioe) {
- throw new MessagingException("Unable to create multipart body", ioe);
- }
-
- //Create the list of recipients in our MailAddress format
- Set recipients = new HashSet();
- recipients.add(mail.getSender());
-
- //Set additional headers
- if (reply.getHeader(RFC2822Headers.DATE)==null){
- reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));
- }
- String subject = message.getSubject();
- if (subject == null) {
- subject = "";
- }
- reply.setSubject(subject);
-
- if (message.getMessageID() != null) {
- reply.setHeader(RFC2822Headers.IN_REPLY_TO, message.getMessageID());
}
-
- reply.saveChanges();
-
- //Send it off...
- getMailetContext().sendMail(notifier, recipients, reply);
- }
-
- /**
- * Return a string describing this mailet.
- *
- * @return a string describing this mailet
- */
- public String getMailetInfo() {
- return "NotifySender Mailet";
+ return iaarray;
}
+
+ /* ******************************************************************** */
+ /* ******************* End of getX and setX methods ******************* */
+ /* ******************************************************************** */
+
}
1.18.4.6 +163 -350 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.5
retrieving revision 1.18.4.6
diff -u -r1.18.4.5 -r1.18.4.6
--- Redirect.java 2 Jun 2003 05:48:51 -0000 1.18.4.5
+++ Redirect.java 2 Jun 2003 22:37:07 -0000 1.18.4.6
@@ -68,7 +68,7 @@
import java.util.Iterator;
import java.util.Locale;
import java.util.StringTokenizer;
-import java.util.Vector;
+import java.util.ArrayList;
import javax.mail.Message;
@@ -81,6 +81,7 @@
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;
@@ -92,25 +93,6 @@
*This mailet can produce listserver, forward and notify behaviour, with the original
*message intact, attached, appended or left out altogether.<BR>
*This built in functionality is controlled by the configuration as laid out below.</P>
-*<P>However it is also designed to be easily subclassed to make authoring redirection
-*mailets simple. <BR>
-*By extending it and overriding one or more of these methods new behaviour can
-*be quickly created without the author having to address any other issue than
-*the relevant one:</P>
-*<UL>
-*<LI>attachError() , should error messages be appended to the message</LI>
-*<LI>getAttachmentType(), what should be attached to the message</LI>
-*<LI>getInLineType(), what should be included in the message</LI>
-*<LI>getMessage(), The text of the message itself</LI>
-*<LI>getRecipients(), the recipients the mail is sent to</LI>
-*<LI>getReplyTo(), where replys to this message will be sent</LI>
-*<LI>getSender(), who the mail is from</LI>
-*<LI>getSubjectPrefix(), a prefix to be added to the message subject</LI>
-*<LI>getTo(), a list of people to whom the mail is *apparently* sent</LI>
-*<LI>getPassThrough(), should this mailet allow the original message to continue processing or GHOST it.</LI>
-*<LI>isStatic(), should this mailet run the get methods for every mail, or just
-*once. </LI>
-*</UL>
*<P>The configuration parameters are:</P>
*<TABLE width="75%" border="0" cellspacing="2" cellpadding="2">
*<TR>
@@ -118,14 +100,16 @@
*<TD width="80%">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.</TD>
+*supplied.<BR>
+*It can include constants "sender", "postmaster" and "returnPath"</TD>
*</TR>
*<TR>
*<TD width="20%"><to></TD>
*<TD width="80%">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.<BR>
-*The recipients list will be used if this is not supplied.</TD>
+*The recipients list will be used if this is not supplied.<BR>
+*It can include constants "sender", "postmaster", "returnPath" and "unaltered"</TD>
*</TR>
*<TR>
*<TD width="20%"><sender></TD>
@@ -186,9 +170,15 @@
*</TR>
*<TR>
*<TD width="20%"><replyto></TD>
-*<TD width="80%">A single email address to appear in the Rely-To: header, can
+*<TD width="80%">A single email address to appear in the Reply-To: header, can
*also be "sender" or "postmaster", this header is not
-*set if this is omited.</TD>
+*set if this is omitted.</TD>
+*</TR>
+*<TR>
+*<TD width="20%"><returnPath></TD>
+*<TD width="80%">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.</TD>
*</TR>
*<TR>
*<TD width="20%"><prefix></TD>
@@ -197,6 +187,11 @@
*Undeliverable mail: </TD>
*</TR>
*<TR>
+*<TD width="20%"><isReply></TD>
+*<TD width="80%">TRUE or FALSE, if true the IN_REPLY_TO header will be set to the
+*id of the current message.</TD>
+*</TR>
+*<TR>
*<TD width="20%"><static></TD>
*<TD width="80%">
*<P>TRUE or FALSE. If this is TRUE it tells the mailet that it can
@@ -204,8 +199,6 @@
*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.</P>
-*<P>Note: If you use "magic words" such as "sender" in the <sender>
-*tag, you must NOT use set static to TRUE.</P>
*<P>This defaults to false.<BR>
*</TD>
*</TR>
@@ -240,69 +233,35 @@
*<static>TRUE</static><BR>
*</mailet></P>
*
- * @author Danny Angus <da...@thought.co.uk>
*
*/
-public class Redirect extends GenericMailet {
+public class Redirect extends AbstractRedirect {
+
/**
- * Controls certain log messages
+ * Returns a string describing this mailet.
+ *
+ * @return a string describing this mailet
*/
- private boolean isDebug = false;
-
- // The values that indicate how to attach the original mail
- // to the redirected mail.
-
- private static final int UNALTERED = 0;
-
- private static final int HEADS = 1;
-
- private static final int BODY = 2;
-
- private static final int ALL = 3;
-
- private static final int NONE = 4;
-
- private static final int MESSAGE = 5;
-
- private InternetAddress[] apparentlyTo;
- private String messageText;
- private Collection recipients;
- private MailAddress replyTo;
- private MailAddress sender;
-
- private RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();
+ public String getMailetInfo() {
+ return "Resend Mailet";
+ }
+ /* ******************************************************************** */
+ /* ****************** Begin of getX and setX methods ****************** */
+ /* ******************************************************************** */
+
/**
- * returns one of these values to indicate how to attach the original message
- *<ul>
- * <li>BODY : original message body is attached as plain text to the new message</li>
- * <li>HEADS : original message headers are attached as plain text to the new message</li>
- * <li>ALL : original is attached as plain text with all headers</li>
- * <li>MESSAGE : original message is attached as type message/rfc822, a complete mail message.</li>
- * <li>NONE : original is not attached</li>
- *</ul>
- *
- */
- public int getAttachmentType() {
- if(getInitParameter("attachment") == null) {
- return NONE;
- } else {
- return getTypeCode(getInitParameter("attachment"));
- }
+ * @return the <CODE>static</CODE> init parameter
+ */
+ protected boolean isStatic() {
+ return isStatic;
}
/**
- * returns one of these values to indicate how to append the original message
- *<ul>
- * <li>UNALTERED : original message is the new message body</li>
- * <li>BODY : original message body is appended to the new message</li>
- * <li>HEADS : original message headers are appended to the new message</li>
- * <li>ALL : original is appended with all headers</li>
- * <li>NONE : original is not appended</li>
- *</ul>
+ * @return the <CODE>inline</CODE> init parameter
*/
- public int getInLineType() {
+ protected int getInLineType() throws MessagingException {
if(getInitParameter("inline") == null) {
return BODY;
} else {
@@ -311,18 +270,20 @@
}
/**
- * Return a string describing this mailet.
- *
- * @return a string describing this mailet
+ * @return the <CODE>attachment</CODE> init parameter
*/
- public String getMailetInfo() {
- return "Resend Mailet";
+ protected int getAttachmentType() throws MessagingException {
+ if(getInitParameter("attachment") == null) {
+ return NONE;
+ } else {
+ return getTypeCode(getInitParameter("attachment"));
+ }
}
/**
- * must return either an empty string, or a message to which the redirect can be attached/appended
+ * @return the <CODE>message</CODE> init parameter or an empty string if missing
*/
- public String getMessage() {
+ protected String getMessage() throws MessagingException {
if(getInitParameter("message") == null) {
return "";
} else {
@@ -331,25 +292,27 @@
}
/**
- * return true to allow thie original message to continue through the processor, false to GHOST it
+ * @return the <CODE>recipients</CODE> init parameter or SpecialAddress.SENDER
+ * or SpecialAddress.RETURN_PATH or null if missing
*/
- public boolean getPassThrough() {
- if(getInitParameter("passThrough") == null) {
- return false;
- } else {
- return new Boolean(getInitParameter("passThrough")).booleanValue();
+ 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;
+ }
+
+ MailAddress specialAddress = getSpecialAddress(addressList,
+ new String[] {"postmaster", "sender", "returnPath"});
+ if (specialAddress != null) {
+ newRecipients.add(specialAddress);
+ return newRecipients;
}
- }
- /**
- * must return a Collection of recipient MailAddresses
- */
- public Collection getRecipients() {
- Collection newRecipients = new HashSet();
- String addressList = (getInitParameter("recipients") == null)
- ? getInitParameter("to")
- : getInitParameter("recipients");
- StringTokenizer st = new StringTokenizer(addressList, ",", false);
+ StringTokenizer st = new StringTokenizer(addressList, ",", false);
while(st.hasMoreTokens()) {
try {
newRecipients.add(new MailAddress(st.nextToken()));
@@ -361,72 +324,120 @@
}
/**
- * Returns the reply to address as a string.
- *
- * @return the replyto address for the mail as a string
+ * @return the <CODE>to</CODE> init parameter or SpecialAddress.SENDER
+ * or SpecialAddress.RETURN_PATH or SpecialAddress.UNALTERED or null if missing
*/
- public MailAddress getReplyTo() {
- String sr = getInitParameter("replyto");
- if(sr != null) {
- MailAddress rv;
- if(sr.compareTo("postmaster") == 0) {
- rv = getMailetContext().getPostmaster();
- return rv;
+ 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;
+ }
+
+ MailAddress specialAddress = getSpecialAddress(addressList,
+ new String[] {"postmaster", "sender", "returnPath", "unaltered"});
+ if (specialAddress != null) {
+ InternetAddress[] iaarray = new InternetAddress[1];
+ iaarray[0] = specialAddress.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()");
}
- if(sr.compareTo("sender") == 0) {
- return null;
+ }
+ return iaarray;
+ }
+
+ /**
+ * @return the <CODE>replyto</CODE> init parameter or null if missing or == "sender"
+ */
+ protected MailAddress getReplyTo() throws MessagingException {
+ String addressString = getInitParameter("replyto");
+ if(addressString != null) {
+ MailAddress specialAddress = getSpecialAddress(addressString,
+ new String[] {"postmaster", "sender"});
+ if (specialAddress != null) {
+ if (specialAddress == SpecialAddress.SENDER) {
+ // means no change
+ return null;
+ }
+ return specialAddress;
}
+
try {
- rv = new MailAddress(sr);
- return rv;
+ return new MailAddress(addressString);
} catch(Exception e) {
- log("Parse error in getReplyTo " + sr);
+ log("Parse error in getReplyTo: " + addressString);
}
}
+
return null;
}
/**
- * returns the senders address, as a MailAddress
+ * @return the <CODE>returnPath</CODE> init parameter or SpecialAddress.NULL
+ * or SpecialAddress.SENDER or null if missing
*/
- public MailAddress getSender() {
- String sr = getInitParameter("sender");
- if(sr != null) {
- MailAddress rv;
- if(sr.compareTo("postmaster") == 0) {
- rv = getMailetContext().getPostmaster();
- return rv;
- }
- if(sr.compareTo("sender") == 0) {
- return null;
+ protected MailAddress getReturnPath() throws MessagingException {
+ String addressString = getInitParameter("returnPath");
+ if(addressString != null) {
+ MailAddress specialAddress = getSpecialAddress(addressString,
+ new String[] {"postmaster", "sender", "null"});
+ if (specialAddress != null) {
+ return specialAddress;
}
+
try {
- rv = new MailAddress(sr);
- return rv;
+ return new MailAddress(addressString);
} catch(Exception e) {
- log("Parse error in getSender " + sr);
+ log("Parse error in getReturnPath: " + addressString);
}
}
+
return null;
}
/**
- * 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)<br>
- * 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
+ * @return the <CODE>sender</CODE> init parameter or null if missing or == "sender"
*/
- public boolean isStatic() {
- if(getInitParameter("static") == null) {
- return false;
+ protected MailAddress getSender() throws MessagingException {
+ String addressString = getInitParameter("sender");
+ if(addressString != null) {
+ MailAddress specialAddress = getSpecialAddress(addressString,
+ new String[] {"postmaster", "sender"});
+ if (specialAddress != null) {
+ if (specialAddress == SpecialAddress.SENDER) {
+ // means no change: use FROM header; kept as is for compatibility
+ return null;
+ }
+ return specialAddress;
+ }
+
+ try {
+ return new MailAddress(addressString);
+ } catch(Exception e) {
+ log("Parse error in getSender: " + addressString);
+ }
}
- return new Boolean(getInitParameter("static")).booleanValue();
+
+ return null;
}
/**
- * return a prefix for the message subject
+ * @return the <CODE>prefix</CODE> init parameter or an empty string if missing
*/
- public String getSubjectPrefix() {
+ protected String getSubjectPrefix() throws MessagingException {
if(getInitParameter("prefix") == null) {
return "";
} else {
@@ -435,31 +446,9 @@
}
/**
- * returns an array of InternetAddress 'es for the To: header
+ * @return the <CODE>attachError</CODE> init parameter; false if missing
*/
- 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;
- }
-
- /**
- * return true to append a description of any error to the main body part
- * if getInlineType does not return "UNALTERED"
- */
- public boolean attachError() {
+ protected boolean attachError() throws MessagingException {
if(getInitParameter("attachError") == null) {
return false;
} else {
@@ -468,193 +457,17 @@
}
/**
- * init will setup static values for sender, recipients, message text, and reply to
- * <br> 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();
- if(isStatic()) {
- 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(", message=")
- .append(messageText)
- .append(" ");
- log(logBuffer.toString());
- }
- }
- }
-
- /**
- * Service does the hard work,and redirects the mail in the form specified
- *
- * @param mail the mail to process and redirect
- * @throws MessagingException if a problem arising formulating the redirected mail
+ * @return the <CODE>isReply</CODE> init parameter; false if missing
*/
- 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() != UNALTERED) {
- if (isDebug) {
- log("Alter message inline=:" + getInLineType());
- }
- 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
- reply = getPassThrough() ? new MimeMessage(message) : message;
- if (isDebug) {
- log("Message resent unaltered.");
- }
- }
- //Set additional headers
- reply.setSubject(getSubjectPrefix() + message.getSubject());
- if(reply.getHeader(RFC2822Headers.DATE) == null) {
- reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date()));
- }
-
- //
-
- 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(sender, recipients, reply);
- if(!getPassThrough()) {
- mail.setState(Mail.GHOST);
- }
- }
-
- /**
- * A private method to convert types from string to int.
- *
- * @param param the string type
- *
- * @return the corresponding int enumeration
- */
- private int getTypeCode(String param) {
- int code;
- param = param.toLowerCase(Locale.US);
- if(param.compareTo("unaltered") == 0) {
- return UNALTERED;
- }
- if(param.compareTo("heads") == 0) {
- return HEADS;
- }
- if(param.compareTo("body") == 0) {
- return BODY;
- }
- if(param.compareTo("all") == 0) {
- return ALL;
- }
- if(param.compareTo("none") == 0) {
- return NONE;
- }
- if(param.compareTo("message") == 0) {
- return MESSAGE;
+ protected boolean isReply() throws MessagingException {
+ if(getInitParameter("isReply") == null) {
+ return false;
}
- return NONE;
+ return new Boolean(getInitParameter("isReply")).booleanValue();
}
+
+ /* ******************************************************************** */
+ /* ******************* End of getX and setX methods ******************* */
+ /* ******************************************************************** */
+
}
1.6.4.3 +54 -21 jakarta-james/src/java/org/apache/james/transport/mailets/Forward.java
Index: Forward.java
===================================================================
RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/Forward.java,v
retrieving revision 1.6.4.2
retrieving revision 1.6.4.3
diff -u -r1.6.4.2 -r1.6.4.3
--- Forward.java 8 Mar 2003 21:54:08 -0000 1.6.4.2
+++ Forward.java 2 Jun 2003 22:37:08 -0000 1.6.4.3
@@ -68,26 +68,67 @@
import java.util.StringTokenizer;
/**
- * Replaces incoming recipients with those specified.
+ * Replaces incoming recipients with those specified, and resends the message unaltered.
+ *
+ * <P>Sample configuration:</P>
+ * <PRE><CODE>
+ * <mailet match="All" class="Forward">
+ * <forwardto><I>comma delimited list of email addresses</I></forwardto>
+ * <passThrough><I>true or false, default=false</I></passThrough>
+ * </mailet>
+ * </CODE></PRE>
+ *
+ * <P>The behaviour of this mailet is equivalent to using Redirect with the following
+ * configuration:</P>
+ * <PRE><CODE>
+ * <mailet match="All" class="Redirect">
+ * <passThrough>true or false</passThrough>
+ * <recipients>comma delimited list of email addresses</recipients>
+ * <inline>unaltered</inline>
+ * </mailet>
+ * </CODE></PRE>
*
- * @author Federico Barbieri <sc...@pop.systemy.it>
- * @author Serge Knystautas <se...@lokitech.com>
*/
-public class Forward extends GenericMailet {
-
- private Collection newRecipients;
+public class Forward extends AbstractRedirect {
/**
- * Initialize the mailet
+ * Return a string describing this mailet.
+ *
+ * @return a string describing this mailet
+ */
+ public String getMailetInfo() {
+ return "Forward Mailet";
+ }
+
+ /* ******************************************************************** */
+ /* ****************** Begin of getX and setX methods ****************** */
+ /* ******************************************************************** */
+
+ /**
+ * @return the <CODE>recipients</CODE> init parameter or null if missing
*/
- public void init() throws MessagingException {
- newRecipients = new HashSet();
- StringTokenizer st = new StringTokenizer(getMailetConfig().getInitParameter("forwardto"), ",", false);
- while (st.hasMoreTokens()) {
- newRecipients.add(new MailAddress(st.nextToken()));
+ protected Collection getRecipients() throws MessagingException {
+ Collection newRecipients = new HashSet();
+ String addressList = getInitParameter("forwardto");
+ // if nothing was specified, return null meaning no change
+ if (addressList == null) {
+ return null;
+ }
+ StringTokenizer st = new StringTokenizer(addressList, ",", false);
+ while(st.hasMoreTokens()) {
+ try {
+ newRecipients.add(new MailAddress(st.nextToken()));
+ } catch(Exception e) {
+ log("add recipient failed in getRecipients");
+ }
}
+ return newRecipients;
}
+ /* ******************************************************************** */
+ /* ******************* End of getX and setX methods ******************* */
+ /* ******************************************************************** */
+
/**
* Forwards a mail to a particular recipient.
*
@@ -104,7 +145,7 @@
// Although this can be viewed as a configuration error, the
// consequences of such a mis-configuration are severe enough
// to warrant protecting against the infinite loop.
- getMailetContext().sendMail(mail.getSender(), newRecipients, mail.getMessage());
+ super.service(mail);
}
else {
StringBuffer logBuffer = new StringBuffer(256)
@@ -120,13 +161,5 @@
}
}
- /**
- * Return a string describing this mailet.
- *
- * @return a string describing this mailet
- */
- public String getMailetInfo() {
- return "Forward Mailet";
- }
}
No revision
No revision
1.1.2.1 +253 -0 jakarta-james/src/java/org/apache/james/transport/mailets/Attic/AbstractNotify.java
1.1.2.1 +1131 -0 jakarta-james/src/java/org/apache/james/transport/mailets/Attic/AbstractRedirect.java
1.1.2.1 +199 -0 jakarta-james/src/java/org/apache/james/transport/mailets/Attic/Bounce.java
---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org