1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets;
21
22 import org.apache.mailet.GenericMailet;
23 import org.apache.mailet.Mail;
24 import org.apache.mailet.MailAddress;
25 import org.apache.mailet.MailetContext;
26
27 import javax.mail.MessagingException;
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.Vector;
31
32 /***
33 * Rewrites recipient addresses to make sure email for the postmaster is
34 * always handled. This mailet is silently inserted at the top of the root
35 * spool processor. All recipients mapped to postmaster@<servernames> are
36 * changed to the postmaster account as specified in the server conf.
37 *
38 */
39 public class PostmasterAlias extends GenericMailet {
40
41 /***
42 * Make sure that a message that is addressed to a postmaster alias is always
43 * sent to the postmaster address, regardless of delivery to other recipients.
44 *
45 * @param mail the mail to process
46 *
47 * @throws MessagingException if an error is encountered while modifying the message
48 */
49 public void service(Mail mail) throws MessagingException {
50 Collection recipients = mail.getRecipients();
51 Collection recipientsToRemove = null;
52 MailetContext mailetContext = getMailetContext();
53 boolean postmasterAddressed = false;
54
55 for (Iterator i = recipients.iterator(); i.hasNext(); ) {
56 MailAddress addr = (MailAddress)i.next();
57 if (addr.getUser().equalsIgnoreCase("postmaster") &&
58 mailetContext.isLocalServer(addr.getHost())) {
59
60
61 if (recipientsToRemove == null) {
62 recipientsToRemove = new Vector();
63 }
64 recipientsToRemove.add(addr);
65
66 postmasterAddressed = true;
67 }
68 }
69 if (postmasterAddressed) {
70 recipients.removeAll(recipientsToRemove);
71 recipients.add(getMailetContext().getPostmaster());
72 }
73 }
74
75 /***
76 * Return a string describing this mailet.
77 *
78 * @return a string describing this mailet
79 */
80 public String getMailetInfo() {
81 return "Postmaster aliasing mailet";
82 }
83 }