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 */
020package org.apache.directory.server.ldap.handlers;
021
022
023import org.apache.directory.api.ldap.model.message.Response;
024import org.apache.directory.server.ldap.LdapServer;
025import org.apache.directory.server.ldap.LdapSession;
026import org.apache.mina.core.session.IoSession;
027import org.apache.mina.handler.demux.MessageHandler;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031
032/**
033 * A base class for all LDAP response handlers.
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public abstract class LdapResponseHandler<T extends Response> implements MessageHandler<T>
038{
039    /** The logger for this class */
040    protected static final Logger LOG = LoggerFactory.getLogger( LdapResponseHandler.class );
041
042    /** The reference on the Ldap server instance */
043    protected LdapServer ldapServer;
044
045    /**
046     * @return The associated ldap server instance
047     */
048    public final LdapServer getLdapServer()
049    {
050        return ldapServer;
051    }
052
053
054    /**
055     * Associates a Ldap server instance to the message handler
056     * @param ldapServer the associated ldap server instance
057     */
058    public final void setLdapServer( LdapServer ldapServer )
059    {
060        this.ldapServer = ldapServer;
061    }
062
063
064    /**
065     *{@inheritDoc} 
066     */
067    public final void handleMessage( IoSession session, T message ) throws Exception
068    {
069        LdapSession ldapSession = ldapServer.getLdapSessionManager().getLdapSession( session );
070
071        if ( ldapSession == null )
072        {
073            // in some cases the session is becoming null though the client is sending the UnbindRequest
074            // before closing
075            LOG.info( "ignoring the message {} received from null session", message );
076            
077            return;
078        }
079
080        // TODO - session you get from LdapServer should have the ldapServer 
081        // member already set no?  Should remove these lines where ever they
082        // may be if that's the case.
083        ldapSession.setLdapServer( ldapServer );
084
085        handle( ldapSession, message );
086    }
087
088
089    /**
090     * Handle a Ldap message associated with a session
091     * 
092     * @param session The associated session
093     * @param message The message we have to handle
094     * @throws Exception If there is an error during the processing of this message
095     */
096    public abstract void handle( LdapSession session, T message ) throws Exception;
097}