FileDocCategorySizeDatePackage
NotifyPostmaster.javaAPI DocApache James 2.3.17361Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.mailets

NotifyPostmaster.java

/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

package org.apache.james.transport.mailets;

import org.apache.mailet.MailAddress;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import java.util.HashSet;
import java.util.Collection;

/**
 * <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>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
 *
 * <P>Sample configuration:</P>
 * <PRE><CODE>
 * <mailet match="All" class="NotifyPostmaster">
 *   <sender><I>an address or postmaster or sender or unaltered, default=postmaster</I></sender>
 *   <attachError><I>true or false, default=false</I></attachError>
 *   <message><I>notice attached to the original message text (optional)</I></message>
 *   <prefix><I>optional subject prefix prepended to the original message, default="Re:"</I></prefix>
 *   <inline><I>see {@link Resend}, default=none</I></inline>
 *   <attachment><I>see {@link Resend}, default=message</I></attachment>
 *   <passThrough><I>true or false, default=true</I></passThrough>
 *   <fakeDomainCheck><I>true or false, default=true</I></fakeDomainCheck>
 *   <to><I>unaltered (optional, defaults to postmaster)</I></to>
 *   <debug><I>true or false, default=false</I></debug>
 * </mailet>
 * </CODE></PRE>
 *
 * <P>The behaviour of this mailet is equivalent to using Resend with the following
 * configuration:</P>
 * <PRE><CODE>
 * <mailet match="All" class="Resend">
 *   <sender><I>an address or postmaster or sender or unaltered</I></sender>
 *   <attachError><I>true or false</I></attachError>
 *   <message><I><B>dynamically built</B></I></message>
 *   <prefix><I>a string</I></prefix>
 *   <passThrough><I>true or false</I></passThrough>
 *   <fakeDomainCheck><I>true or false</I></fakeDomainCheck>
 *   <to><I><B>unaltered or postmaster</B></I></to>
 *   <recipients><B>postmaster</B></recipients>
 *   <inline>see {@link Resend}</inline>
 *   <attachment>see {@link Resend}</attachment>
 *   <isReply>true</isReply>
 *   <debug><I>true or false</I></debug>
 * </mailet>
 * </CODE></PRE>
 * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
 * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
 *
 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
 */
public class NotifyPostmaster extends AbstractNotify {

    /**
     * Return a string describing this mailet.
     *
     * @return a string describing this mailet
     */
    public String getMailetInfo() {
        return "NotifyPostmaster Mailet";
    }

    /** Gets the expected init parameters. */
    protected  String[] getAllowedInitParameters() {
        String[] allowedArray = {
//            "static",
            "debug",
            "passThrough",
            "fakeDomainCheck",
            "inline",
            "attachment",
            "message",
            "notice",
            "sender",
            "sendingAddress",
            "prefix",
            "attachError",
            "attachStackTrace",
            "to"
        };
        return allowedArray;
    }
    
    /* ******************************************************************** */
    /* ****************** Begin of getX and setX methods ****************** */
    /* ******************************************************************** */

    /**
     * @return the postmaster address
     */
    protected Collection getRecipients() {
        Collection newRecipients = new HashSet();
        newRecipients.add(getMailetContext().getPostmaster());
        return newRecipients;
    }

    /**
     * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
     */
    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;
    }

    /**
     * @return the <CODE>attachStackTrace</CODE> init parameter, 
     * or the <CODE>attachError</CODE> init parameter if missing,
     * or false if missing 
     */
    protected boolean attachError() throws MessagingException {
        String parameter = getInitParameter("attachStackTrace");
        if (parameter == null) {
            return super.attachError();
        }        
        return new Boolean(parameter).booleanValue();
    }

    /* ******************************************************************** */
    /* ******************* End of getX and setX methods ******************* */
    /* ******************************************************************** */

}