001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.mailet.MailAddress;
021:
022: import javax.mail.MessagingException;
023: import javax.mail.internet.InternetAddress;
024: import java.util.HashSet;
025: import java.util.Collection;
026:
027: /**
028: * <P>Sends a notification message to the Postmaster.</P>
029: * <P>A sender of the notification message can optionally be specified.
030: * If one is not specified, the postmaster's address will be used.<BR>
031: * The "To:" header of the notification message can be set to "unaltered";
032: * if missing will be set to the postmaster.<BR>
033: * A notice text can be specified, and in such case will be inserted into the
034: * notification inline text.<BR>
035: * If the notified message has an "error message" set, it will be inserted into the
036: * notification inline text. If the <CODE>attachStackTrace</CODE> init parameter
037: * is set to true, such error message will be attached to the notification message.<BR>
038: * The notified messages are attached in their entirety (headers and
039: * content) and the resulting MIME part type is "message/rfc822".</P>
040: * <P>Supports the <CODE>passThrough</CODE> init parameter (true if missing).</P>
041: *
042: * <P>Sample configuration:</P>
043: * <PRE><CODE>
044: * <mailet match="All" class="NotifyPostmaster">
045: * <sender><I>an address or postmaster or sender or unaltered, default=postmaster</I></sender>
046: * <attachError><I>true or false, default=false</I></attachError>
047: * <message><I>notice attached to the original message text (optional)</I></message>
048: * <prefix><I>optional subject prefix prepended to the original message, default="Re:"</I></prefix>
049: * <inline><I>see {@link Resend}, default=none</I></inline>
050: * <attachment><I>see {@link Resend}, default=message</I></attachment>
051: * <passThrough><I>true or false, default=true</I></passThrough>
052: * <fakeDomainCheck><I>true or false, default=true</I></fakeDomainCheck>
053: * <to><I>unaltered (optional, defaults to postmaster)</I></to>
054: * <debug><I>true or false, default=false</I></debug>
055: * </mailet>
056: * </CODE></PRE>
057: *
058: * <P>The behaviour of this mailet is equivalent to using Resend with the following
059: * configuration:</P>
060: * <PRE><CODE>
061: * <mailet match="All" class="Resend">
062: * <sender><I>an address or postmaster or sender or unaltered</I></sender>
063: * <attachError><I>true or false</I></attachError>
064: * <message><I><B>dynamically built</B></I></message>
065: * <prefix><I>a string</I></prefix>
066: * <passThrough><I>true or false</I></passThrough>
067: * <fakeDomainCheck><I>true or false</I></fakeDomainCheck>
068: * <to><I><B>unaltered or postmaster</B></I></to>
069: * <recipients><B>postmaster</B></recipients>
070: * <inline>see {@link Resend}</inline>
071: * <attachment>see {@link Resend}</attachment>
072: * <isReply>true</isReply>
073: * <debug><I>true or false</I></debug>
074: * </mailet>
075: * </CODE></PRE>
076: * <P><I>notice</I>, <I>sendingAddress</I> and <I>attachStackTrace</I> can be used instead of
077: * <I>message</I>, <I>sender</I> and <I>attachError</I>; such names are kept for backward compatibility.</P>
078: *
079: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
080: */
081: public class NotifyPostmaster extends AbstractNotify {
082:
083: /**
084: * Return a string describing this mailet.
085: *
086: * @return a string describing this mailet
087: */
088: public String getMailetInfo() {
089: return "NotifyPostmaster Mailet";
090: }
091:
092: /** Gets the expected init parameters. */
093: protected String[] getAllowedInitParameters() {
094: String[] allowedArray = {
095: // "static",
096: "debug", "passThrough", "fakeDomainCheck", "inline",
097: "attachment", "message", "notice", "sender",
098: "sendingAddress", "prefix", "attachError",
099: "attachStackTrace", "to" };
100: return allowedArray;
101: }
102:
103: /* ******************************************************************** */
104: /* ****************** Begin of getX and setX methods ****************** */
105: /* ******************************************************************** */
106:
107: /**
108: * @return the postmaster address
109: */
110: protected Collection getRecipients() {
111: Collection newRecipients = new HashSet();
112: newRecipients.add(getMailetContext().getPostmaster());
113: return newRecipients;
114: }
115:
116: /**
117: * @return <CODE>SpecialAddress.UNALTERED</CODE> if specified or postmaster if missing
118: */
119: protected InternetAddress[] getTo() throws MessagingException {
120: String addressList = getInitParameter("to");
121: InternetAddress[] iaarray = new InternetAddress[1];
122: iaarray[0] = getMailetContext().getPostmaster()
123: .toInternetAddress();
124: if (addressList != null) {
125: MailAddress specialAddress = getSpecialAddress(addressList,
126: new String[] { "postmaster", "unaltered" });
127: if (specialAddress != null) {
128: iaarray[0] = specialAddress.toInternetAddress();
129: } else {
130: log("\"to\" parameter ignored, set to postmaster");
131: }
132: }
133: return iaarray;
134: }
135:
136: /**
137: * @return the <CODE>attachStackTrace</CODE> init parameter,
138: * or the <CODE>attachError</CODE> init parameter if missing,
139: * or false if missing
140: */
141: protected boolean attachError() throws MessagingException {
142: String parameter = getInitParameter("attachStackTrace");
143: if (parameter == null) {
144: return super .attachError();
145: }
146: return new Boolean(parameter).booleanValue();
147: }
148:
149: /* ******************************************************************** */
150: /* ******************* End of getX and setX methods ******************* */
151: /* ******************************************************************** */
152:
153: }
|