001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    
021    
022    
023    package org.apache.james.protocols.smtp.core.fastfail;
024    
025    import java.util.Collection;
026    
027    import org.apache.james.protocols.smtp.SMTPRetCode;
028    import org.apache.james.protocols.smtp.SMTPSession;
029    import org.apache.james.protocols.smtp.dsn.DSNStatus;
030    import org.apache.james.protocols.smtp.hook.HookResult;
031    import org.apache.james.protocols.smtp.hook.HookReturnCode;
032    import org.apache.james.protocols.smtp.hook.RcptHook;
033    import org.apache.mailet.MailAddress;
034    
035    /**
036     * 
037     * This handler can be used to just ignore duplicated recipients. 
038     */
039    public class SupressDuplicateRcptHandler implements RcptHook {
040    
041        /**
042         * @see org.apache.james.protocols.smtp.hook.RcptHook#doRcpt(org.apache.james.protocols.smtp.SMTPSession, org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
043         */
044        @SuppressWarnings("unchecked")
045        public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) {
046            Collection rcptList = (Collection) session.getState().get(SMTPSession.RCPT_LIST);
047        
048            // Check if the recipient is already in the rcpt list
049            if(rcptList != null && rcptList.contains(rcpt)) {
050                StringBuilder responseBuffer = new StringBuilder();
051            
052                responseBuffer.append(DSNStatus.getStatus(DSNStatus.SUCCESS, DSNStatus.ADDRESS_VALID))
053                              .append(" Recipient <")
054                              .append(rcpt.toString())
055                              .append("> OK");
056                session.getLogger().debug("Duplicate recipient not add to recipient list: " + rcpt.toString());
057                return new HookResult(HookReturnCode.OK,SMTPRetCode.MAIL_OK, responseBuffer.toString());
058            }
059            return new HookResult(HookReturnCode.DECLINED);
060        }
061    }