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.ldap.client.api;
021
022
023import static org.apache.directory.api.ldap.model.message.ResultCodeEnum.processResponse;
024
025import java.util.concurrent.atomic.AtomicInteger;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
030import org.apache.directory.api.ldap.model.exception.LdapException;
031import org.apache.directory.api.ldap.model.message.BindRequest;
032import org.apache.directory.api.ldap.model.message.BindRequestImpl;
033import org.apache.directory.api.ldap.model.message.BindResponse;
034import org.apache.directory.api.ldap.model.message.Control;
035import org.apache.directory.api.ldap.model.name.Dn;
036import org.apache.directory.api.ldap.model.schema.SchemaManager;
037import org.apache.directory.api.util.Strings;
038import org.apache.mina.core.service.IoHandlerAdapter;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042
043/**
044 * An abstract LdapConnection class gathering the common behavior of LdapConnection
045 * concrete classes.
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public abstract class AbstractLdapConnection extends IoHandlerAdapter implements LdapConnection
050{
051    /** logger for reporting errors that might not be handled properly upstream */
052    private static final Logger LOG = LoggerFactory.getLogger( AbstractLdapConnection.class );
053
054    /** the schema manager */
055    protected SchemaManager schemaManager;
056
057    /** A Message ID which is incremented for each operation */
058    protected AtomicInteger messageId;
059
060    /** the ldap codec service */
061    protected LdapApiService codec;
062
063
064    /**
065     * Creates a new instance of an AbstractLdapConnection
066     */
067    protected AbstractLdapConnection()
068    {
069        this( LdapApiServiceFactory.getSingleton() );
070    }
071
072    protected AbstractLdapConnection( LdapApiService codec )
073    {
074        messageId = new AtomicInteger( 0 );
075        this.codec = codec;
076    }
077
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public void bind( Dn name ) throws LdapException
084    {
085        byte[] credBytes = Strings.EMPTY_BYTES;
086
087        BindRequest bindRequest = new BindRequestImpl();
088        bindRequest.setDn( name );
089        bindRequest.setCredentials( credBytes );
090
091        BindResponse bindResponse = bind( bindRequest );
092
093        processResponse( bindResponse );
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public void bind( String name ) throws LdapException
102    {
103        if ( LOG.isDebugEnabled() )
104        {
105            LOG.debug( I18n.msg( I18n.MSG_04145_BIND_REQUEST, name ) );
106        }
107
108        bind( new Dn( schemaManager, name ), null );
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public void bind( String name, String credentials ) throws LdapException
117    {
118        bind( new Dn( schemaManager, name ), credentials );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public void bind( Dn name, String credentials ) throws LdapException
127    {
128        byte[] credBytes = credentials == null ? Strings.EMPTY_BYTES : Strings.getBytesUtf8( credentials );
129
130        BindRequest bindRequest = new BindRequestImpl();
131        bindRequest.setDn( name );
132        bindRequest.setCredentials( credBytes );
133
134        BindResponse bindResponse = bind( bindRequest );
135
136        processResponse( bindResponse );
137    }
138
139
140    /**
141     * Create a complete BindRequest ready to be sent.
142     *
143     * @param name The DN to bind with
144     * @param credentials The user's password
145     * @param saslMechanism The SASL mechanism to use
146     * @param controls The controls to send
147     * @return The created BindRequest
148     */
149    protected BindRequest createBindRequest( String name, byte[] credentials, String saslMechanism, Control... controls )
150    {
151        // Set the new messageId
152        BindRequest bindRequest = new BindRequestImpl();
153
154        // Set the version
155        bindRequest.setVersion3( true );
156
157        // Set the name
158        bindRequest.setName( name );
159
160        // Set the credentials
161        if ( Strings.isEmpty( saslMechanism ) )
162        {
163            // Simple bind
164            bindRequest.setSimple( true );
165            bindRequest.setCredentials( credentials );
166        }
167        else
168        {
169            // SASL bind
170            bindRequest.setSimple( false );
171            bindRequest.setCredentials( credentials );
172            bindRequest.setSaslMechanism( saslMechanism );
173        }
174
175        // Add the controls
176        if ( ( controls != null ) && ( controls.length != 0 ) )
177        {
178            bindRequest.addAllControls( controls );
179        }
180
181        return bindRequest;
182    }
183}