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