KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.mailet.MailetContext;
24
25 import javax.mail.MessagingException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Vector JavaDoc;
29 //import com.workingdogs.town.*;
30

31 /**
32  * Rewrites recipient addresses to make sure email for the postmaster is
33  * always handled. This mailet is silently inserted at the top of the root
34  * spool processor. All recipients mapped to postmaster@<servernames> are
35  * changed to the postmaster account as specified in the server conf.
36  *
37  */

38 public class PostmasterAlias extends GenericMailet {
39
40     /**
41      * Make sure that a message that is addressed to a postmaster alias is always
42      * sent to the postmaster address, regardless of delivery to other recipients.
43      *
44      * @param mail the mail to process
45      *
46      * @throws MessagingException if an error is encountered while modifying the message
47      */

48     public void service(Mail mail) throws MessagingException JavaDoc {
49         Collection JavaDoc recipients = mail.getRecipients();
50         Collection JavaDoc recipientsToRemove = null;
51         MailetContext mailetContext = getMailetContext();
52         boolean postmasterAddressed = false;
53
54         for (Iterator JavaDoc i = recipients.iterator(); i.hasNext(); ) {
55             MailAddress addr = (MailAddress)i.next();
56             if (addr.getUser().equalsIgnoreCase("postmaster") &&
57                 mailetContext.isLocalServer(addr.getHost())) {
58                 //Should remove this address... we want to replace it with
59
// the server's postmaster address
60
if (recipientsToRemove == null) {
61                     recipientsToRemove = new Vector JavaDoc();
62                 }
63                 recipientsToRemove.add(addr);
64                 //Flag this as having found the postmaster
65
postmasterAddressed = true;
66             }
67         }
68         if (postmasterAddressed) {
69             recipients.removeAll(recipientsToRemove);
70             recipients.add(getMailetContext().getPostmaster());
71         }
72     }
73
74     /**
75      * Return a string describing this mailet.
76      *
77      * @return a string describing this mailet
78      */

79     public String JavaDoc getMailetInfo() {
80         return "Postmaster aliasing mailet";
81     }
82 }
83
Popular Tags