View Javadoc
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  package org.apache.directory.server.ldap.handlers;
21  
22  
23  import org.apache.directory.api.ldap.model.message.Response;
24  import org.apache.directory.server.ldap.LdapServer;
25  import org.apache.directory.server.ldap.LdapSession;
26  import org.apache.mina.core.session.IoSession;
27  import org.apache.mina.handler.demux.MessageHandler;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  
32  /**
33   * A base class for all LDAP response handlers.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public abstract class LdapResponseHandler<T extends Response> implements MessageHandler<T>
38  {
39      /** The logger for this class */
40      protected static final Logger LOG = LoggerFactory.getLogger( LdapResponseHandler.class );
41  
42      /** The reference on the Ldap server instance */
43      protected LdapServer ldapServer;
44  
45      /**
46       * @return The associated ldap server instance
47       */
48      public final LdapServer getLdapServer()
49      {
50          return ldapServer;
51      }
52  
53  
54      /**
55       * Associates a Ldap server instance to the message handler
56       * @param ldapServer the associated ldap server instance
57       */
58      public final void setLdapServer( LdapServer ldapServer )
59      {
60          this.ldapServer = ldapServer;
61      }
62  
63  
64      /**
65       *{@inheritDoc} 
66       */
67      public final void handleMessage( IoSession session, T message ) throws Exception
68      {
69          LdapSession ldapSession = ldapServer.getLdapSessionManager().getLdapSession( session );
70  
71          if ( ldapSession == null )
72          {
73              // in some cases the session is becoming null though the client is sending the UnbindRequest
74              // before closing
75              LOG.info( "ignoring the message {} received from null session", message );
76              
77              return;
78          }
79  
80          // TODO - session you get from LdapServer should have the ldapServer 
81          // member already set no?  Should remove these lines where ever they
82          // may be if that's the case.
83          ldapSession.setLdapServer( ldapServer );
84  
85          handle( ldapSession, message );
86      }
87  
88  
89      /**
90       * Handle a Ldap message associated with a session
91       * 
92       * @param session The associated session
93       * @param message The message we have to handle
94       * @throws Exception If there is an error during the processing of this message
95       */
96      public abstract void handle( LdapSession session, T message ) throws Exception;
97  }