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