FileDocCategorySizeDatePackage
AbstractNotify.javaAPI DocApache James 2.3.111081Fri Jan 12 12:56:28 GMT 2007org.apache.james.transport.mailets

AbstractNotify

public abstract class AbstractNotify extends AbstractRedirect

Abstract mailet providing configurable notification services.
This mailet can be subclassed to make authoring notification mailets simple.

Provides the following functionalities to all notification subclasses:

  • A common notification message layout.
  • A sender of the notification message can optionally be specified. If one is not specified, the postmaster's address will be used.
  • A notice text can be specified, and in such case will be inserted into the notification inline text.
  • If the notified message has an "error message" set, it will be inserted into the notification inline text. If the attachStackTrace init parameter is set to true, such error message will be attached to the notification message.
  • The notified messages are attached in their entirety (headers and content) and the resulting MIME part type is "message/rfc822".
  • Supports by default the passThrough init parameter (true if missing).

Sample configuration common to all notification mailet subclasses:


<mailet match="All" class="a notification mailet">
<sender>an address or postmaster or sender or unaltered, default=postmaster</sender>
<attachError>true or false, default=false</attachError>
<message>notice attached to the original message text (optional)</message>
<prefix>optional subject prefix prepended to the original message</prefix>
<inline>see {@link Redirect}, default=none</inline>
<attachment>see {@link Redirect}, default=message</attachment>
<passThrough>true or false, default=true</passThrough>
<fakeDomainCheck>true or false, default=true</fakeDomainCheck>
<debug>true or false, default=false</debug>
</mailet>

notice and senderAddress can be used instead of message and sender; such names are kept for backward compatibility.

version
CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
since
2.2.0

Fields Summary
Constructors Summary
Methods Summary
protected intgetAttachmentType()

return
the attachment init parameter, or MESSAGE if missing

        return getTypeCode(getInitParameter("attachment","message"));
    
protected intgetInLineType()

return
the inline init parameter, or NONE if missing

        return getTypeCode(getInitParameter("inline","none"));
    
protected java.lang.StringgetMessage()

return
the notice init parameter, or the message init parameter if missing, or a default string if both are missing

        return getInitParameter("notice",
                getInitParameter("message",
                "We were unable to deliver the attached message because of an error in the mail server."));
    
protected java.lang.StringgetMessage(org.apache.mailet.Mail originalMail)

return
the full message to append, built from the Mail object

        MimeMessage message = originalMail.getMessage();
        StringWriter sout = new StringWriter();
        PrintWriter out = new PrintWriter(sout, true);

        // First add the "local" notice
        // (either from conf or generic error message)
        out.println(getMessage());
        // And then the message from other mailets
        if (originalMail.getErrorMessage() != null) {
            out.println();
            out.println("Error message below:");
            out.println(originalMail.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: " + originalMail.getSender());
        Iterator rcptTo = originalMail.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());
        }

        return sout.toString();
    
protected booleangetPassThrough()

return
the passThrough init parameter, or true if missing

        return new Boolean(getInitParameter("passThrough","true")).booleanValue();
    
protected abstract java.util.CollectiongetRecipients()

protected org.apache.mailet.MailAddressgetReplyTo()

return
SpecialAddress.NULL, that will remove the "ReplyTo:" header

        return SpecialAddress.NULL;
    
protected org.apache.mailet.MailAddressgetReversePath(org.apache.mailet.Mail originalMail)

return
{@link AbstractRedirect#getSender(Mail)}, meaning the new requested sender if any

        return getSender(originalMail);
    
protected org.apache.mailet.MailAddressgetSender()

return
the value of the sendingAddress init parameter, or the value of the sender init parameter if missing, or the postmaster address if both are missing
return
the sendingAddress init parameter or the sender init parameter or the postmaster address if both are missing; possible special addresses returned are SpecialAddress.SENDER and SpecialAddress.UNALTERED

        String addressString = getInitParameter("sendingAddress",getInitParameter("sender"));
        
        if (addressString == null) {
            return getMailetContext().getPostmaster();
        }
        
        MailAddress specialAddress = getSpecialAddress(addressString,
                                        new String[] {"postmaster", "sender", "unaltered"});
        if (specialAddress != null) {
            return specialAddress;
        }

        try {
            return new MailAddress(addressString);
        } catch(Exception e) {
            throw new MessagingException("Exception thrown in getSender() parsing: " + addressString, e);
        }
    
protected java.lang.StringgetSubject()

return
null

        return null;
    
protected java.lang.StringgetSubjectPrefix()

return
the prefix init parameter or "Re:" if missing

        return getInitParameter("prefix","Re:");
    
protected javax.mail.internet.InternetAddress[]getTo()

return
null

        return null;
    
protected booleanisReply()

return
true

        return true;
    
protected voidsetSubjectPrefix(org.apache.mailet.Mail newMail, java.lang.String subjectPrefix, org.apache.mailet.Mail originalMail)
Builds the subject of newMail appending the subject of originalMail to subjectPrefix, but avoiding a duplicate.

        String subject = originalMail.getMessage().getSubject();
        if (subject == null) {
            subject = "";
        }
        if (subjectPrefix==null || subject.indexOf(subjectPrefix) == 0) {
            newMail.getMessage().setSubject(subject);
        } else {
            newMail.getMessage().setSubject(subjectPrefix + subject);
        }