001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.mailet;
017    
018    import java.util.HashSet;
019    import java.util.Set;
020    
021    import javax.mail.MessagingException;
022    
023    import org.apache.log4j.Logger;
024    
025    import com.hs.mail.container.config.Config;
026    import com.hs.mail.smtp.message.Recipient;
027    import com.hs.mail.smtp.message.SmtpMessage;
028    
029    /**
030     * Receives a Mail from SmtpMessageConsumer and takes care of delivery of the
031     * message to local inboxes.
032     * 
033     * @author Won Chul Doh
034     * @since Jun 7, 2010
035     * 
036     */
037    public class LocalDelivery extends AbstractMailet {
038    
039            static Logger logger = Logger.getLogger(LocalDelivery.class);
040    
041            /**
042             * Mailet that apply aliasing and forwarding
043             */
044            private AliasingForwarding aliasingMailet;
045            /**
046             * Mailet that actually stores the message
047             */
048            private ToRepository deliveryMailet;
049    
050            public void init(MailetContext context) {
051                    super.init(context);
052                    aliasingMailet = new AliasingForwarding();
053                    aliasingMailet.init(context);
054                    deliveryMailet = new ToRepository();
055                    deliveryMailet.init(context);
056            }
057    
058            public boolean accept(Set<Recipient> recipients, SmtpMessage message) {
059                    return message.getNode() == SmtpMessage.LOCAL
060                                    || message.getNode() == SmtpMessage.ALL;
061            }
062    
063            public void service(Set<Recipient> recipients, SmtpMessage message)
064                            throws MessagingException {
065                    Set<Recipient> temp = new HashSet<Recipient>();
066                    for (Recipient recipient : recipients) {
067                            if (Config.isLocal(recipient.getHost())) {
068                                    temp.add(recipient);
069                            }
070                    }
071                    
072                    if (aliasingMailet.accept(temp, message)) {
073                            aliasingMailet.service(temp, message);
074                            if (deliveryMailet.accept(temp, message)) {
075                                    deliveryMailet.service(temp, message);
076                            }
077                    }
078            }
079    
080    }