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