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.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;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.Vector;
29  
30  /***
31   * Rewrites recipient addresses to make sure email for the postmaster is
32   * always handled.  This mailet is silently inserted at the top of the root
33   * spool processor.  All recipients mapped to postmaster@<servernames> are
34   * changed to the postmaster account as specified in the server conf.
35   *
36   */
37  public class PostmasterAlias extends GenericMailet {
38  
39      /***
40       * Make sure that a message that is addressed to a postmaster alias is always
41       * sent to the postmaster address, regardless of delivery to other recipients.
42       *
43       * @param mail the mail to process
44       *
45       * @throws MessagingException if an error is encountered while modifying the message
46       */
47      public void service(Mail mail) throws MessagingException {
48          Collection recipients = mail.getRecipients();
49          Collection recipientsToRemove = null;
50          MailetContext mailetContext = getMailetContext();
51          boolean postmasterAddressed = false;
52  
53          for (Iterator i = recipients.iterator(); i.hasNext(); ) {
54              MailAddress addr = (MailAddress)i.next();
55              if (addr.getUser().equalsIgnoreCase("postmaster") &&
56                  mailetContext.isLocalServer(addr.getHost())) {
57                  //Should remove this address... we want to replace it with
58                  //  the server's postmaster address
59                  if (recipientsToRemove == null) {
60                      recipientsToRemove = new Vector();
61                  }
62                  recipientsToRemove.add(addr);
63                  //Flag this as having found the postmaster
64                  postmasterAddressed = true;
65              }
66          }
67          if (postmasterAddressed) {
68              recipients.removeAll(recipientsToRemove);
69              recipients.add(getMailetContext().getPostmaster());
70          }
71      }
72  
73      /***
74       * Return a string describing this mailet.
75       *
76       * @return a string describing this mailet
77       */
78      public String getMailetInfo() {
79          return "Postmaster aliasing mailet";
80      }
81  }