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 org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.util.Oid;
25  import org.apache.directory.api.ldap.extras.extended.startTls.StartTlsRequest;
26  import org.apache.directory.api.ldap.model.exception.LdapException;
27  import org.apache.directory.api.ldap.model.message.BindRequest;
28  import org.apache.directory.api.ldap.model.message.BindResponse;
29  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
30  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
31  import org.apache.directory.api.ldap.model.name.Dn;
32  
33  
34  /**
35   * A class used to monitor the use of a LdapConnection
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public final class MonitoringLdapConnection extends LdapConnectionWrapper
40  {
41      private static final Oid START_TLS_OID;
42  
43      static
44      {
45          try
46          {
47              START_TLS_OID = Oid.fromString( StartTlsRequest.EXTENSION_OID );
48          }
49          catch ( DecoderException de )
50          {
51              throw new IllegalStateException( "StartTlsRequest.EXTENSION_OID is not a valid oid... This cant happen", de );
52          }
53      }
54  
55      private boolean bindCalled = false;
56      private boolean startTlsCalled = false;
57  
58  
59      MonitoringLdapConnection( LdapConnection connection )
60      {
61          super( connection );
62      }
63  
64  
65      /**
66       * @return tells if a Bind has been issued 
67       */
68      public boolean bindCalled()
69      {
70          return bindCalled;
71      }
72  
73  
74      /**
75       * Reset the Bind and StartTLS flags
76       */
77      public void resetMonitors()
78      {
79          bindCalled = false;
80          startTlsCalled = false;
81      }
82  
83  
84      /**
85       * @return tells if the StarTLS extended operation has been called
86       */
87      public boolean startTlsCalled()
88      {
89          return startTlsCalled;
90      }
91  
92  
93      @Override
94      public void bind() throws LdapException
95      {
96          connection.bind();
97          bindCalled = true;
98      }
99  
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 }