KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > Forward


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailAddress;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.InternetAddress JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashSet JavaDoc;
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: 1.6.4.14 $ $Date: 2004/03/15 03:54:19 $
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 JavaDoc getMailetInfo() {
67         return "Forward Mailet";
68     }
69
70     /** Gets the expected init parameters. */
71     protected String JavaDoc[] getAllowedInitParameters() {
72         String JavaDoc[] 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 JavaDoc {
91         return UNALTERED;
92     }
93
94     /**
95      * @return NONE
96      */

97     protected int getAttachmentType() throws MessagingException JavaDoc {
98         return NONE;
99     }
100
101     /**
102      * @return ""
103      */

104     protected String JavaDoc getMessage() throws MessagingException JavaDoc {
105         return "";
106     }
107
108     /**
109      * @return the <CODE>recipients</CODE> init parameter or null if missing
110      */

111     protected Collection JavaDoc getRecipients() throws MessagingException JavaDoc {
112         Collection JavaDoc newRecipients = new HashSet JavaDoc();
113         boolean error = false;
114         String JavaDoc addressList = getInitParameter("forwardto");
115         if (addressList == null) {
116             addressList = getInitParameter("forwardTo");
117         }
118         
119         // if nothing was specified, throw an exception
120
if (addressList == null) {
121             throw new MessagingException JavaDoc("Failed to initialize \"recipients\" list: no <forwardTo> or <forwardto> init parameter found");
122         }
123
124         try {
125             InternetAddress JavaDoc[] iaarray = InternetAddress.parse(addressList, false);
126             for (int i = 0; i < iaarray.length; i++) {
127                 String JavaDoc addressString = iaarray[i].getAddress();
128                 MailAddress specialAddress = getSpecialAddress(addressString,
129                 new String JavaDoc[] {"postmaster", "sender", "from", "replyTo", "reversePath", "unaltered", "recipients", "to", "null"});
130                 if (specialAddress != null) {
131                     newRecipients.add(specialAddress);
132                 } else {
133                     newRecipients.add(new MailAddress(iaarray[i]));
134                 }
135             }
136         } catch (Exception JavaDoc e) {
137             throw new MessagingException JavaDoc("Exception thrown in getRecipients() parsing: " + addressList, e);
138         }
139         if (newRecipients.size() == 0) {
140             throw new MessagingException JavaDoc("Failed to initialize \"recipients\" list; empty <recipients> init parameter found.");
141         }
142
143         return newRecipients;
144     }
145
146     /**
147      * @return null
148      */

149     protected InternetAddress JavaDoc[] getTo() throws MessagingException JavaDoc {
150         return null;
151     }
152
153     /**
154      * @return null
155      */

156     protected MailAddress getReplyTo() throws MessagingException JavaDoc {
157         return null;
158     }
159
160     /**
161      * @return null
162      */

163     protected MailAddress getReversePath() throws MessagingException JavaDoc {
164         return null;
165     }
166
167     /**
168      * @return null
169      */

170     protected MailAddress getSender() throws MessagingException JavaDoc {
171         return null;
172     }
173
174     /**
175      * @return null
176      */

177     protected String JavaDoc getSubject() throws MessagingException JavaDoc {
178         return null;
179     }
180
181     /**
182      * @return ""
183      */

184     protected String JavaDoc getSubjectPrefix() throws MessagingException JavaDoc {
185         return null;
186     }
187
188     /**
189      * @return false
190      */

191     protected boolean attachError() {
192         return false;
193     }
194
195     /**
196      * @return false
197      */

198     protected boolean isReply() throws MessagingException JavaDoc {
199         return false;
200     }
201
202     /* ******************************************************************** */
203     /* ******************* End of getX and setX methods ******************* */
204     /* ******************************************************************** */
205
206 }
207
208
Popular Tags