KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.mail.internet.MimeMessage JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 /**
32  * <p>Mailet designed to process the recipients from the mail headers rather
33  * than the recipients specified in the SMTP message header. This can be
34  * useful if your mail is redirected on-route by a mail server that
35  * substitutes a fixed recipient address for the original.</p>
36  *
37  * <p>To use this, match against the redirection address using the
38  * <code>RecipientIs</code> matcher and set the mailet 'class' to
39  * <code>UseHeaderRecipients</code>. This will cause the email to be
40  * re-injected into the root process with the recipient substituted
41  * by all the recipients in the Mail-For, To and Cc headers
42  * of the message.</p>
43  *
44  * <p>e.g.</p>
45  * <pre>
46  * <mailet match="RecipientIs=forwarded@myhost"
47  * class="UseHeaderRecipients">
48  * </mailet>
49  * </pre>
50  *
51  * @version 1.0.0, 24/11/2000
52  */

53 public class UseHeaderRecipients extends GenericMailet {
54
55     /**
56      * Controls certain log messages
57      */

58     private boolean isDebug = false;
59
60     /**
61      * Initialize the mailet
62      *
63      * initializes the DEBUG flag
64      */

65     public void init() {
66         isDebug = (getInitParameter("debug") == null) ? false : new Boolean JavaDoc(getInitParameter("debug")).booleanValue();
67     }
68
69     /**
70      * Process an incoming email, removing the currently identified
71      * recipients and replacing them with the recipients indicated in
72      * the Mail-For, To and Cc headers of the actual email.
73      *
74      * @param mail incoming email
75      */

76     public void service(Mail mail) throws MessagingException JavaDoc {
77         MimeMessage JavaDoc message = mail.getMessage();
78
79         // Utilise features of Set Collections such that they automatically
80
// ensure that no two entries are equal using the equality method
81
// of the element objects. MailAddress objects test equality based
82
// on equivalent but not necessarily visually identical addresses.
83
Collection JavaDoc recipients = mail.getRecipients();
84         // Wipe all the exist recipients
85
recipients.clear();
86         recipients.addAll(getHeaderMailAddresses(message, "Mail-For"));
87         if (recipients.isEmpty()) {
88             recipients.addAll(getHeaderMailAddresses(message, "To"));
89             recipients.addAll(getHeaderMailAddresses(message, "Cc"));
90         }
91         if (isDebug) {
92             log("All recipients = " + recipients.toString());
93             log("Reprocessing mail using recipients in message headers");
94         }
95
96         // Return email to the "root" process.
97
getMailetContext().sendMail(mail.getSender(), mail.getRecipients(), mail.getMessage());
98         mail.setState(Mail.GHOST);
99     }
100
101
102     /**
103      * Return a string describing this mailet.
104      *
105      * @return a string describing this mailet
106      */

107     public String JavaDoc getMailetInfo() {
108         return "UseHeaderRecipients Mailet";
109     }
110
111     /**
112      * Work through all the headers of the email with a matching name and
113      * extract all the mail addresses as a collection of addresses.
114      *
115      * @param mail the mail message to read
116      * @param name the header name as a String
117      * @return the collection of MailAddress objects.
118      */

119     private Collection JavaDoc getHeaderMailAddresses(MimeMessage JavaDoc message, String JavaDoc name) throws MessagingException JavaDoc {
120
121         if (isDebug) {
122             StringBuffer JavaDoc logBuffer =
123                 new StringBuffer JavaDoc(64)
124                         .append("Checking ")
125                         .append(name)
126                         .append(" headers");
127             log(logBuffer.toString());
128         }
129         Collection JavaDoc addresses = new Vector JavaDoc();
130         String JavaDoc[] headers = message.getHeader(name);
131         String JavaDoc addressString;
132         InternetAddress JavaDoc iAddress;
133         if (headers != null) {
134             for(int i = 0; i < headers.length; i++) {
135                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(headers[i], ",", false);
136                 while (st.hasMoreTokens()) {
137                     addressString = st.nextToken();
138                     iAddress = new InternetAddress JavaDoc(addressString);
139                     if (isDebug) {
140                         log("Address = " + iAddress.toString());
141                     }
142                     addresses.add(new MailAddress(iAddress));
143                 }
144             }
145         }
146         return addresses;
147     }
148
149 }
150
Popular Tags