01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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: }
|