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  package org.apache.james.transport.mailets;
21  
22  import org.apache.mailet.MailAddress;
23  
24  import javax.mail.MessagingException;
25  import javax.mail.internet.InternetAddress;
26  import java.util.Collection;
27  import java.util.HashSet;
28  
29  /***
30   * <P>Replaces incoming recipients with those specified, and resends the message unaltered.</P>
31   * <P>Can be totally replaced by an equivalent usage of {@link Resend} (see below),
32   * simply replacing <I>&lt;forwardto&gt;</I> with <I>&lt;recipients&gt</I>.
33   *
34   * <P>Sample configuration:</P>
35   * <PRE><CODE>
36   * &lt;mailet match="All" class="Forward">
37   *   &lt;forwardTo&gt;<I>comma delimited list of email addresses</I>&lt;/forwardTo&gt;
38   *   &lt;passThrough&gt;<I>true or false, default=false</I>&lt;/passThrough&gt;
39   *   &lt;fakeDomainCheck&gt;<I>true or false, default=true</I>&lt;/fakeDomainCheck&gt;
40   *   &lt;debug&gt;<I>true or false, default=false</I>&lt;/debug&gt;
41   * &lt;/mailet&gt;
42   * </CODE></PRE>
43   *
44   * <P>The behaviour of this mailet is equivalent to using Resend with the following
45   * configuration:</P>
46   * <PRE><CODE>
47   * &lt;mailet match="All" class="Resend">
48   *   &lt;recipients&gt;comma delimited list of email addresses&lt;/recipients&gt;
49   *   &lt;passThrough&gt;true or false&lt;/passThrough&gt;
50   *   &lt;fakeDomainCheck&gt;<I>true or false</I>&lt;/fakeDomainCheck&gt;
51   *   &lt;debug&gt;<I>true or false</I>&lt;/debug&gt;
52   * &lt;/mailet&gt;
53   * </CODE></PRE>
54   * <P><I>forwardto</I> can be used instead of
55   * <I>forwardTo</I>; such name is kept for backward compatibility.</P>
56   *
57   * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
58   */
59  public class Forward extends AbstractRedirect {
60  
61      /***
62       * Return a string describing this mailet.
63       *
64       * @return a string describing this mailet
65       */
66      public String getMailetInfo() {
67          return "Forward Mailet";
68      }
69  
70      /*** Gets the expected init parameters. */
71      protected  String[] getAllowedInitParameters() {
72          String[] allowedArray = {
73  //            "static",
74              "debug",
75              "passThrough",
76              "fakeDomainCheck",
77              "forwardto",
78              "forwardTo"
79          };
80          return allowedArray;
81      }
82  
83      /* ******************************************************************** */
84      /* ****************** Begin of getX and setX methods ****************** */
85      /* ******************************************************************** */
86  
87      /***
88       * @return UNALTERED
89       */
90      protected int getInLineType() throws MessagingException {
91          return UNALTERED;
92      }
93  
94      /***
95       * @return NONE
96       */
97      protected int getAttachmentType() throws MessagingException {
98          return NONE;
99      }
100 
101     /***
102      * @return ""
103      */
104     protected String getMessage() throws MessagingException {
105         return "";
106     }
107 
108     /***
109      * @return the <CODE>recipients</CODE> init parameter or null if missing
110      */
111     protected Collection getRecipients() throws MessagingException {
112         Collection newRecipients = new HashSet();
113         String addressList = getInitParameter("forwardto",getInitParameter("forwardTo"));
114         
115         // if nothing was specified, throw an exception
116         if (addressList == null) {
117             throw new MessagingException("Failed to initialize \"recipients\" list: no <forwardTo> or <forwardto> init parameter found");
118         }
119 
120         try {
121             InternetAddress[] iaarray = InternetAddress.parse(addressList, false);
122             for (int i = 0; i < iaarray.length; i++) {
123                 String addressString = iaarray[i].getAddress();
124                 MailAddress specialAddress = getSpecialAddress(addressString,
125                 new String[] {"postmaster", "sender", "from", "replyTo", "reversePath", "unaltered", "recipients", "to", "null"});
126                 if (specialAddress != null) {
127                     newRecipients.add(specialAddress);
128                 } else {
129                     newRecipients.add(new MailAddress(iaarray[i]));
130                 }
131             }
132         } catch (Exception e) {
133             throw new MessagingException("Exception thrown in getRecipients() parsing: " + addressList, e);
134         }
135         if (newRecipients.size() == 0) {
136             throw new MessagingException("Failed to initialize \"recipients\" list; empty <recipients> init parameter found.");
137         }
138 
139         return newRecipients;
140     }
141 
142     /***
143      * @return null
144      */
145     protected InternetAddress[] getTo() throws MessagingException {
146         return null;
147     }
148 
149     /***
150      * @return null
151      */
152     protected MailAddress getReplyTo() throws MessagingException {
153         return null;
154     }
155 
156     /***
157      * @return null
158      */
159     protected MailAddress getReversePath() throws MessagingException {
160         return null;
161     }
162 
163     /***
164      * @return null
165      */
166     protected MailAddress getSender() throws MessagingException {
167         return null;
168     }
169 
170     /***
171      * @return null
172      */
173     protected String getSubject() throws MessagingException {
174         return null;
175     }
176 
177     /***
178      * @return ""
179      */
180     protected String getSubjectPrefix() throws MessagingException {
181         return null;
182     }
183 
184     /***
185      * @return false
186      */
187     protected boolean attachError() {
188         return false;
189     }
190 
191     /***
192      * @return false
193      */
194     protected boolean isReply() throws MessagingException {
195         return false;
196     }
197 
198     /* ******************************************************************** */
199     /* ******************* End of getX and setX methods ******************* */
200     /* ******************************************************************** */
201 
202 }
203