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 org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.util.Oid;
025import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.message.BindRequest;
028import org.apache.directory.api.ldap.model.message.BindResponse;
029import org.apache.directory.api.ldap.model.message.ExtendedRequest;
030import org.apache.directory.api.ldap.model.message.ExtendedResponse;
031import org.apache.directory.api.ldap.model.name.Dn;
032
033
034/**
035 * A class used to monitor the use of a LdapConnection
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public final class MonitoringLdapConnection extends LdapConnectionWrapper
040{
041    private static final Oid START_TLS_OID;
042
043    static
044    {
045        try
046        {
047            START_TLS_OID = Oid.fromString( StartTlsRequest.EXTENSION_OID );
048        }
049        catch ( DecoderException de )
050        {
051            throw new IllegalStateException( "StartTlsRequest.EXTENSION_OID is not a valid oid... This cant happen", de );
052        }
053    }
054
055    private boolean bindCalled = false;
056    private boolean startTlsCalled = false;
057
058
059    MonitoringLdapConnection( LdapConnection connection )
060    {
061        super( connection );
062    }
063
064
065    /**
066     * @return tells if a Bind has been issued 
067     */
068    public boolean bindCalled()
069    {
070        return bindCalled;
071    }
072
073
074    /**
075     * Reset the Bind and StartTLS flags
076     */
077    public void resetMonitors()
078    {
079        bindCalled = false;
080        startTlsCalled = false;
081    }
082
083
084    /**
085     * @return tells if the StarTLS extended operation has been called
086     */
087    public boolean startTlsCalled()
088    {
089        return startTlsCalled;
090    }
091
092
093    @Override
094    public void bind() throws LdapException
095    {
096        connection.bind();
097        bindCalled = true;
098    }
099
100
101    @Override
102    public void anonymousBind() throws LdapException
103    {
104        connection.anonymousBind();
105        bindCalled = true;
106    }
107
108
109    @Override
110    public void bind( String name ) throws LdapException
111    {
112        connection.bind( name );
113        bindCalled = true;
114    }
115
116
117    @Override
118    public void bind( String name, String credentials ) throws LdapException
119    {
120        connection.bind( name, credentials );
121        bindCalled = true;
122    }
123
124
125    @Override
126    public void bind( Dn name ) throws LdapException
127    {
128        connection.bind( name );
129        bindCalled = true;
130    }
131
132
133    @Override
134    public void bind( Dn name, String credentials ) throws LdapException
135    {
136        connection.bind( name, credentials );
137        bindCalled = true;
138    }
139
140
141    @Override
142    public BindResponse bind( BindRequest bindRequest ) throws LdapException
143    {
144        BindResponse response = connection.bind( bindRequest );
145        bindCalled = true;
146        return response;
147    }
148
149
150    @Override
151    public ExtendedResponse extended( String oid ) throws LdapException
152    {
153        if ( StartTlsRequest.EXTENSION_OID.equals( oid ) )
154        {
155            startTlsCalled = true;
156        }
157        return connection.extended( oid );
158    }
159
160
161    @Override
162    public ExtendedResponse extended( String oid, byte[] value ) throws LdapException
163    {
164        if ( StartTlsRequest.EXTENSION_OID.equals( oid ) )
165        {
166            startTlsCalled = true;
167        }
168        return connection.extended( oid, value );
169    }
170
171
172    @Override
173    public ExtendedResponse extended( Oid oid ) throws LdapException
174    {
175        if ( START_TLS_OID.equals( oid ) )
176        {
177            startTlsCalled = true;
178        }
179        return connection.extended( oid );
180    }
181
182
183    @Override
184    public ExtendedResponse extended( Oid oid, byte[] value ) throws LdapException
185    {
186        if ( START_TLS_OID.equals( oid ) )
187        {
188            startTlsCalled = true;
189        }
190        return connection.extended( oid, value );
191    }
192
193
194    @Override
195    public ExtendedResponse extended( ExtendedRequest extendedRequest ) throws LdapException
196    {
197        if ( extendedRequest.hasControl( StartTlsRequest.EXTENSION_OID ) )
198        {
199            startTlsCalled = true;
200        }
201        return connection.extended( extendedRequest );
202    }
203}