/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
package org.apache.james.smtpserver.netty;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLEngine;

import org.apache.james.protocols.api.LineHandler;
import org.apache.james.protocols.impl.AbstractSession;
import org.apache.james.protocols.impl.LineHandlerUpstreamHandler;
import org.apache.james.protocols.smtp.SMTPConfiguration;
import org.apache.james.protocols.smtp.SMTPSession;
import org.apache.james.smtpserver.netty.SMTPServer.SMTPHandlerConfigurationDataImpl;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;

/**
 * {@link SMTPSession} implementation for use with Netty
 */
public class SMTPNettySession extends AbstractSession implements SMTPSession {
    public final static String SMTP_SESSION = "SMTP_SESSION";

    private boolean relayingAllowed;

    private Map<String, Object> connectionState;

    private SMTPConfiguration theConfigData;

    private int lineHandlerCount = 0;

    public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, ChannelHandlerContext handlerContext, SSLEngine engine) {
        super(logger, handlerContext, engine);
        this.theConfigData = theConfigData;
        connectionState = new HashMap<String, Object>();

        relayingAllowed = theConfigData.isRelayingAllowed(getRemoteIPAddress());
    }

    public SMTPNettySession(SMTPConfiguration theConfigData, Logger logger, ChannelHandlerContext handlerContext) {
        this(theConfigData, logger, handlerContext, null);
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getConnectionState()
     */
    public Map<String, Object> getConnectionState() {
        return connectionState;
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getState()
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> getState() {
        Map<String, Object> res = (Map<String, Object>) getConnectionState().get(SMTPSession.SESSION_STATE_MAP);
        if (res == null) {
            res = new HashMap<String, Object>();
            getConnectionState().put(SMTPSession.SESSION_STATE_MAP, res);
        }
        return res;
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#isRelayingAllowed()
     */
    public boolean isRelayingAllowed() {
        return relayingAllowed;
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#resetState()
     */
    public void resetState() {
        // remember the ehlo mode between resets
        Object currentHeloMode = getState().get(CURRENT_HELO_MODE);

        getState().clear();

        // start again with the old helo mode
        if (currentHeloMode != null) {
            getState().put(CURRENT_HELO_MODE, currentHeloMode);
        }
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#popLineHandler()
     */
    public void popLineHandler() {
        if (lineHandlerCount > 0) {
            getChannelHandlerContext().getPipeline().remove("lineHandler" + lineHandlerCount);
            lineHandlerCount--;
        }
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#pushLineHandler(org.apache.james.smtpserver.protocol.LineHandler)
     */
    public void pushLineHandler(LineHandler<SMTPSession> overrideCommandHandler) {
        lineHandlerCount++;

        getChannelHandlerContext().getPipeline().addAfter("timeoutHandler", "lineHandler" + lineHandlerCount, new LineHandlerUpstreamHandler<SMTPSession>(overrideCommandHandler));
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getHelloName()
     */
    public String getHelloName() {
        return theConfigData.getHelloName();
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getMaxMessageSize()
     */
    public long getMaxMessageSize() {
        return theConfigData.getMaxMessageSize();
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getRcptCount()
     */
    @SuppressWarnings("unchecked")
    public int getRcptCount() {
        int count = 0;

        // check if the key exists
        if (getState().get(SMTPSession.RCPT_LIST) != null) {
            count = ((Collection) getState().get(SMTPSession.RCPT_LIST)).size();
        }

        return count;
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#getSMTPGreeting()
     */
    public String getSMTPGreeting() {
        return theConfigData.getSMTPGreeting();
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#isAuthSupported()
     */
    public boolean isAuthSupported() {
        return theConfigData.isAuthRequired(socketAddress.getAddress().getHostAddress());
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#setRelayingAllowed(boolean)
     */
    public void setRelayingAllowed(boolean relayingAllowed) {
        this.relayingAllowed = relayingAllowed;
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#sleep(long)
     */
    public void sleep(long ms) {
        // session.getFilterChain().addAfter("connectionFilter",
        // "tarpitFilter",new TarpitFilter(ms));
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#useAddressBracketsEnforcement()
     */
    public boolean useAddressBracketsEnforcement() {
        return theConfigData.useAddressBracketsEnforcement();
    }

    /**
     * @see org.apache.james.protocols.smtp.SMTPSession#useHeloEhloEnforcement()
     */
    public boolean useHeloEhloEnforcement() {
        return theConfigData.useHeloEhloEnforcement();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.james.protocols.smtp.SMTPSession#getPushedLineHandlerCount()
     */
    public int getPushedLineHandlerCount() {
        return lineHandlerCount;
    }

    public boolean verifyIdentity() {
        if (theConfigData instanceof SMTPHandlerConfigurationDataImpl) {
            return ((SMTPHandlerConfigurationDataImpl) theConfigData).verifyIdentity();
        } else {
            return true;
        }
    }
}

@@ -4640,16 +4640,224 @@
 unt++;%0D%0A
+        // Add the linehandler in front of the coreHandler so we can be sure %0D%0A        // it is executed with the same ExecutorHandler as the coreHandler (if one exist)%0D%0A        // %0D%0A        // See JAMES-1277
 %0D%0A      
@@ -4906,22 +4906,20 @@
 .add
-After(%22timeout
+Before(%22core
 Hand

Fix problem with PIPELINE support when using NIO version of SMTPServer. See JAMES-1277