View Javadoc

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