View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.transport.mailets;
23  
24  import org.apache.mailet.MailAddress;
25  
26  import javax.mail.MessagingException;
27  import javax.mail.internet.InternetAddress;
28  import java.util.Collection;
29  import java.util.HashSet;
30  
31  /**
32   * <P>Replaces incoming recipients with those specified, and resends the message unaltered.</P>
33   * <P>Can be totally replaced by an equivalent usage of {@link Resend} (see below),
34   * simply replacing <I>&lt;forwardto&gt;</I> with <I>&lt;recipients&gt</I>.
35   *
36   * <P>Sample configuration:</P>
37   * <PRE><CODE>
38   * &lt;mailet match="All" class="Forward">
39   *   &lt;forwardTo&gt;<I>comma delimited list of email addresses</I>&lt;/forwardTo&gt;
40   *   &lt;passThrough&gt;<I>true or false, default=false</I>&lt;/passThrough&gt;
41   *   &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
42   *   &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
43   * &lt;/mailet&gt;
44   * </CODE></PRE>
45   *
46   * <P>The behaviour of this mailet is equivalent to using Resend with the following
47   * configuration:</P>
48   * <PRE><CODE>
49   * &lt;mailet match="All" class="Resend">
50   *   &lt;recipients&gt;comma delimited list of email addresses&lt;/recipients&gt;
51   *   &lt;passThrough&gt;true or false&lt;/passThrough&gt;
52   *   &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
53   *   &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
54   * &lt;/mailet&gt;
55   * </CODE></PRE>
56   * <P><I>forwardto</I> can be used instead of
57   * <I>forwardTo</I>; such name is kept for backward compatibility.</P>
58   *
59   * @version CVS $Revision: 426007 $ $Date: 2006-07-27 09:52:06 +0100 (Thu, 27 Jul 2006) $
60   */
61  public class Forward extends AbstractRedirect {
62  
63      /**
64       * Return a string describing this mailet.
65       *
66       * @return a string describing this mailet
67       */
68      public String getMailetInfo() {
69          return "Forward Mailet";
70      }
71  
72      /** Gets the expected init parameters. */
73      protected  String[] getAllowedInitParameters() {
74          String[] allowedArray = {
75  //            "static",
76              "debug",
77              "passThrough",
78              "fakeDomainCheck",
79              "forwardto",
80              "forwardTo"
81          };
82          return allowedArray;
83      }
84  
85      /* ******************************************************************** */
86      /* ****************** Begin of getX and setX methods ****************** */
87      /* ******************************************************************** */
88  
89      /**
90       * @return UNALTERED
91       */
92      protected int getInLineType() throws MessagingException {
93          return UNALTERED;
94      }
95  
96      /**
97       * @return NONE
98       */
99      protected int getAttachmentType() throws MessagingException {
100         return NONE;
101     }
102 
103     /**
104      * @return ""
105      */
106     protected String getMessage() throws MessagingException {
107         return "";
108     }
109 
110     /**
111      * @return the <CODE>recipients</CODE> init parameter or null if missing
112      */
113     protected Collection getRecipients() throws MessagingException {
114         Collection newRecipients = new HashSet();
115         String addressList = getInitParameter("forwardto",getInitParameter("forwardTo"));
116         
117         // if nothing was specified, throw an exception
118         if (addressList == null) {
119             throw new MessagingException("Failed to initialize \"recipients\" list: no <forwardTo> or <forwardto> init parameter found");
120         }
121 
122         try {
123             InternetAddress[] iaarray = InternetAddress.parse(addressList, false);
124             for (int i = 0; i < iaarray.length; i++) {
125                 String addressString = iaarray[i].getAddress();
126                 MailAddress specialAddress = getSpecialAddress(addressString,
127                 new String[] {"postmaster", "sender", "from", "replyTo", "reversePath", "unaltered", "recipients", "to", "null"});
128                 if (specialAddress != null) {
129                     newRecipients.add(specialAddress);
130                 } else {
131                     newRecipients.add(new MailAddress(iaarray[i]));
132                 }
133             }
134         } catch (Exception e) {
135             throw new MessagingException("Exception thrown in getRecipients() parsing: " + addressList, e);
136         }
137         if (newRecipients.size() == 0) {
138             throw new MessagingException("Failed to initialize \"recipients\" list; empty <recipients> init parameter found.");
139         }
140 
141         return newRecipients;
142     }
143 
144     /**
145      * @return null
146      */
147     protected InternetAddress[] getTo() throws MessagingException {
148         return null;
149     }
150 
151     /**
152      * @return null
153      */
154     protected MailAddress getReplyTo() throws MessagingException {
155         return null;
156     }
157 
158     /**
159      * @return null
160      */
161     protected MailAddress getReversePath() throws MessagingException {
162         return null;
163     }
164 
165     /**
166      * @return null
167      */
168     protected MailAddress getSender() throws MessagingException {
169         return null;
170     }
171 
172     /**
173      * @return null
174      */
175     protected String getSubject() throws MessagingException {
176         return null;
177     }
178 
179     /**
180      * @return ""
181      */
182     protected String getSubjectPrefix() throws MessagingException {
183         return null;
184     }
185 
186     /**
187      * @return false
188      */
189     protected boolean attachError() {
190         return false;
191     }
192 
193     /**
194      * @return false
195      */
196     protected boolean isReply() throws MessagingException {
197         return false;
198     }
199 
200     /* ******************************************************************** */
201     /* ******************* End of getX and setX methods ******************* */
202     /* ******************************************************************** */
203 
204 }
205