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.ldap.client.api;
21  
22  
23  import static org.apache.directory.api.ldap.model.message.ResultCodeEnum.processResponse;
24  
25  import java.util.concurrent.atomic.AtomicInteger;
26  
27  import org.apache.directory.api.ldap.codec.api.LdapApiService;
28  import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.api.ldap.model.message.BindRequest;
31  import org.apache.directory.api.ldap.model.message.BindRequestImpl;
32  import org.apache.directory.api.ldap.model.message.BindResponse;
33  import org.apache.directory.api.ldap.model.message.Control;
34  import org.apache.directory.api.ldap.model.name.Dn;
35  import org.apache.directory.api.ldap.model.schema.SchemaManager;
36  import org.apache.directory.api.util.StringConstants;
37  import org.apache.directory.api.util.Strings;
38  import org.apache.mina.core.service.IoHandlerAdapter;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  
43  /**
44   * An abstract LdapConnection class gathering the common behavior of LdapConnection
45   * concrete classes.
46   * 
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public abstract class AbstractLdapConnection extends IoHandlerAdapter implements LdapConnection
50  {
51      /** logger for reporting errors that might not be handled properly upstream */
52      private static final Logger LOG = LoggerFactory.getLogger( AbstractLdapConnection.class );
53  
54      /** the schema manager */
55      protected SchemaManager schemaManager;
56  
57      /** A Message ID which is incremented for each operation */
58      protected AtomicInteger messageId;
59  
60      /** the ldap codec service */
61      protected LdapApiService codec;
62  
63  
64      /**
65       * Creates a new instance of an AbstractLdapConnection
66       */
67      protected AbstractLdapConnection()
68      {
69          this( LdapApiServiceFactory.getSingleton() );
70      }
71  
72      protected AbstractLdapConnection( LdapApiService codec )
73      {
74          messageId = new AtomicInteger( 0 );
75          this.codec = codec;
76      }
77  
78  
79      /**
80       * {@inheritDoc}
81       */
82      public void bind( Dn name ) throws LdapException
83      {
84          byte[] credBytes = StringConstants.EMPTY_BYTES;
85  
86          BindRequest bindRequest = new BindRequestImpl();
87          bindRequest.setDn( name );
88          bindRequest.setCredentials( credBytes );
89  
90          BindResponse bindResponse = bind( bindRequest );
91  
92          processResponse( bindResponse );
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void bind( String name ) throws LdapException
100     {
101         LOG.debug( "Bind request : {}", name );
102 
103         bind( new Dn( schemaManager, name ), null );
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     public void bind( String name, String credentials ) throws LdapException
111     {
112         bind( new Dn( schemaManager, name ), credentials );
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public void bind( Dn name, String credentials ) throws LdapException
120     {
121         byte[] credBytes = ( credentials == null ? StringConstants.EMPTY_BYTES : Strings.getBytesUtf8( credentials ) );
122 
123         BindRequest bindRequest = new BindRequestImpl();
124         bindRequest.setDn( name );
125         bindRequest.setCredentials( credBytes );
126 
127         BindResponse bindResponse = bind( bindRequest );
128 
129         processResponse( bindResponse );
130     }
131 
132 
133     /**
134      * Create a complete BindRequest ready to be sent.
135      */
136     protected BindRequest createBindRequest( String name, byte[] credentials, String saslMechanism, Control... controls )
137         throws LdapException
138     {
139         // Set the new messageId
140         BindRequest bindRequest = new BindRequestImpl();
141 
142         // Set the version
143         bindRequest.setVersion3( true );
144 
145         // Set the name
146         bindRequest.setName( name );
147 
148         // Set the credentials
149         if ( Strings.isEmpty( saslMechanism ) )
150         {
151             // Simple bind
152             bindRequest.setSimple( true );
153             bindRequest.setCredentials( credentials );
154         }
155         else
156         {
157             // SASL bind
158             bindRequest.setSimple( false );
159             bindRequest.setCredentials( credentials );
160             bindRequest.setSaslMechanism( saslMechanism );
161         }
162 
163         // Add the controls
164         if ( ( controls != null ) && ( controls.length != 0 ) )
165         {
166             bindRequest.addAllControls( controls );
167         }
168 
169         return bindRequest;
170     }
171 }