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  
21  package org.apache.directory.ldap.client.api;
22  
23  
24  import java.security.SecureRandom;
25  
26  import javax.net.ssl.KeyManager;
27  import javax.net.ssl.TrustManager;
28  import javax.net.ssl.X509TrustManager;
29  
30  import org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.util.Network;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * A class to hold the configuration for creating an LdapConnection.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class LdapConnectionConfig
43  {
44      /** A logger for this class */
45      private static final Logger LOG = LoggerFactory.getLogger( LdapConnectionConfig.class );
46  
47      /** Default ports for LDAP */
48      public static final int DEFAULT_LDAP_PORT = 389;
49  
50      /** Default port for LDAPS */
51      public static final int DEFAULT_LDAPS_PORT = 636;
52  
53      /** The default host : localhost */
54      public static final String DEFAULT_LDAP_HOST = "localhost";
55  
56      /** The LDAP version */
57      public static final int LDAP_V3 = 3;
58  
59      /** The default timeout for operation : 30 seconds */
60      public static final long DEFAULT_TIMEOUT = 30000L;
61  
62      /** the default protocol used for creating SSL context */
63      public static final String DEFAULT_SSL_PROTOCOL = "TLS";
64  
65      // --- private members ----
66      /** A flag indicating if we are using SSL or not, default value is false */
67      private boolean useSsl = false;
68  
69      /** The session timeout */
70      private long timeout = DEFAULT_TIMEOUT;
71  
72      /** A flag indicating if we are using TLS or not, default value is false */
73      private boolean useTls = false;
74  
75      /** The selected LDAP port */
76      private int ldapPort;
77  
78      /** the remote LDAP host */
79      private String ldapHost;
80  
81      /** a valid Dn to authenticate the user */
82      private String name;
83  
84      /** user's credentials ( current implementation supports password only); it must be a non-null value */
85      private String credentials;
86  
87      /** an array of key managers, if set, will be used while initializing the SSL context */
88      private KeyManager[] keyManagers;
89  
90      /** an instance of SecureRandom, if set, will be used while initializing the SSL context */
91      private SecureRandom secureRandom;
92  
93      /** an array of certificate trust managers, if set, will be used while initializing the SSL context */
94      private TrustManager[] trustManagers;
95  
96      /** an array of cipher suites which are enabled, if set, will be used while initializing the SSL context */
97      private String[] enabledCipherSuites;
98  
99      /** an array of protocols which are enabled, if set, will be used while initializing the SSL context */
100     private String[] enabledProtocols;
101 
102     /** name of the protocol used for creating SSL context, default value is "TLS" */
103     private String sslProtocol = DEFAULT_SSL_PROTOCOL;
104 
105     /** The class used to detect if an attribute is HR or not */
106     private BinaryAttributeDetector binaryAttributeDetector;
107 
108     /** The Service to use internally when creating connections */
109     private LdapApiService ldapApiService;
110 
111 
112     /**
113      * Creates a default LdapConnectionConfig instance
114      */
115     public LdapConnectionConfig()
116     {
117         setDefaultTrustManager();
118     }
119 
120 
121     /**
122      * sets the default trust manager based on the SunX509 trustManagement algorithm.
123      * 
124      * We use a non-verification Trust Manager
125      */
126     private void setDefaultTrustManager()
127     {
128         trustManagers = new X509TrustManager[] { new NoVerificationTrustManager() };
129     }
130 
131 
132     /**
133      * Checks if SSL (ldaps://) is used.
134      *
135      * @return true, if SSL is used
136      */
137     public boolean isUseSsl()
138     {
139         return useSsl;
140     }
141 
142 
143     /**
144      * Sets whether SSL should be used.
145      *
146      * @param useSsl true to use SSL
147      */
148     public void setUseSsl( boolean useSsl )
149     {
150         this.useSsl = useSsl;
151     }
152 
153 
154     /**
155      * Gets the LDAP port.
156      *
157      * @return the LDAP port
158      */
159     public int getLdapPort()
160     {
161         return ldapPort;
162     }
163 
164 
165     /**
166      * Sets the LDAP port.
167      *
168      * @param ldapPort the new LDAP port
169      */
170     public void setLdapPort( int ldapPort )
171     {
172         this.ldapPort = ldapPort;
173     }
174 
175 
176     /**
177      * Gets the LDAP host.
178      *
179      * @return the LDAP host
180      */
181     public String getLdapHost()
182     {
183         return ldapHost;
184     }
185 
186 
187     /**
188      * Sets the LDAP host.
189      *
190      * @param ldapHost the new LDAP host
191      */
192     public void setLdapHost( String ldapHost )
193     {
194         this.ldapHost = ldapHost;
195     }
196 
197 
198     /**
199      * Gets the name that is used to authenticate the user.
200      *
201      * @return the name
202      */
203     public String getName()
204     {
205         return name;
206     }
207 
208 
209     /**
210      * Sets the name which is used to authenticate the user.
211      *
212      * @param name the new name
213      */
214     public void setName( String name )
215     {
216         this.name = name;
217     }
218 
219 
220     /**
221      * Gets the credentials.
222      *
223      * @return the credentials
224      */
225     public String getCredentials()
226     {
227         return credentials;
228     }
229 
230 
231     /**
232      * Sets the credentials.
233      *
234      * @param credentials the new credentials
235      */
236     public void setCredentials( String credentials )
237     {
238         this.credentials = credentials;
239     }
240 
241 
242     /**
243      * Gets the default LDAP port.
244      *
245      * @return the default LDAP port
246      */
247     public int getDefaultLdapPort()
248     {
249         return DEFAULT_LDAP_PORT;
250     }
251 
252 
253     /**
254      * Gets the default LDAPS port.
255      *
256      * @return the default LDAPS port
257      */
258     public int getDefaultLdapsPort()
259     {
260         return DEFAULT_LDAPS_PORT;
261     }
262 
263 
264     /**
265      * Gets the default LDAP host.
266      *
267      * @return the default LDAP host
268      */
269     public String getDefaultLdapHost()
270     {
271         return Network.LOOPBACK_HOSTNAME;
272     }
273 
274 
275     /**
276      * Gets the default timeout.
277      *
278      * @return the default timeout
279      */
280     public long getDefaultTimeout()
281     {
282         return DEFAULT_TIMEOUT;
283     }
284 
285 
286     /**
287      * Gets the timeout.
288      *
289      * @return the timeout
290      */
291     public long getTimeout()
292     {
293         return timeout;
294     }
295 
296 
297     /**
298      * Sets the timeout.
299      *
300      * @param timeout the timeout to set
301      */
302     public void setTimeout( long timeout )
303     {
304         this.timeout = timeout;
305     }
306 
307 
308     /**
309      * Gets the supported LDAP version.
310      *
311      * @return the supported LDAP version
312      */
313     public int getSupportedLdapVersion()
314     {
315         return LDAP_V3;
316     }
317 
318 
319     /**
320      * Gets the trust managers.
321      *
322      * @return the trust managers
323      */
324     public TrustManager[] getTrustManagers()
325     {
326         return trustManagers;
327     }
328 
329 
330     /**
331      * Sets the trust managers.
332      *
333      * @param trustManagers the new trust managers
334      */
335     public void setTrustManagers( TrustManager... trustManagers )
336     {
337         this.trustManagers = trustManagers;
338     }
339 
340 
341     /**
342      * Gets the SSL protocol.
343      *
344      * @return the SSL protocol
345      */
346     public String getSslProtocol()
347     {
348         return sslProtocol;
349     }
350 
351 
352     /**
353      * Sets the SSL protocol.
354      *
355      * @param sslProtocol the new SSL protocol
356      */
357     public void setSslProtocol( String sslProtocol )
358     {
359         this.sslProtocol = sslProtocol;
360     }
361 
362 
363     /**
364      * Gets the key managers.
365      *
366      * @return the key managers
367      */
368     public KeyManager[] getKeyManagers()
369     {
370         return keyManagers;
371     }
372 
373 
374     /**
375      * Sets the key managers.
376      *
377      * @param keyManagers the new key managers
378      */
379     public void setKeyManagers( KeyManager[] keyManagers )
380     {
381         this.keyManagers = keyManagers;
382     }
383 
384 
385     /**
386      * Gets the secure random.
387      *
388      * @return the secure random
389      */
390     public SecureRandom getSecureRandom()
391     {
392         return secureRandom;
393     }
394 
395 
396     /**
397      * Sets the secure random.
398      *
399      * @param secureRandom the new secure random
400      */
401     public void setSecureRandom( SecureRandom secureRandom )
402     {
403         this.secureRandom = secureRandom;
404     }
405 
406 
407     /**
408      * Gets the cipher suites which are enabled.
409      * 
410      * @return the cipher suites which are enabled
411      */
412     public String[] getEnabledCipherSuites()
413     {
414         return enabledCipherSuites;
415     }
416 
417 
418     /**
419      * Sets the cipher suites which are enabled
420      * 
421      * @param enabledCipherSuites the cipher suites which are enabled
422      */
423     public void setEnabledCipherSuites( String[] enabledCipherSuites )
424     {
425         this.enabledCipherSuites = enabledCipherSuites;
426     }
427 
428 
429     /**
430      * Gets the protocols which are enabled.
431      * 
432      * @return the protocol which are enabled
433      */
434     public String[] getEnabledProtocols()
435     {
436         return enabledProtocols;
437     }
438 
439 
440     /**
441      * Sets the protocols which are enabled
442      * 
443      * @param enabledProtocols the protocols which are enabled
444      */
445     public void setEnabledProtocols( String... enabledProtocols )
446     {
447         this.enabledProtocols = enabledProtocols;
448     }
449 
450 
451     /**
452      * @return the binaryAttributeDetector
453      */
454     public BinaryAttributeDetector getBinaryAttributeDetector()
455     {
456         return binaryAttributeDetector;
457     }
458 
459 
460     /**
461      * @param binaryAttributeDetector the binaryAttributeDetector to set
462      */
463     public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
464     {
465         this.binaryAttributeDetector = binaryAttributeDetector;
466     }
467 
468 
469     /**
470      * Checks if TLS is used.
471      *
472      * @return true, if TLS is used
473      */
474     public boolean isUseTls()
475     {
476         return useTls;
477     }
478 
479 
480     /**
481      * Sets whether TLS should be used.
482      *
483      * @param useTls true to use TLS
484      */
485     public void setUseTls( boolean useTls )
486     {
487         this.useTls = useTls;
488     }
489 
490 
491     /**
492      * @return the ldapApiService
493      */
494     public LdapApiService getLdapApiService()
495     {
496         return ldapApiService;
497     }
498 
499 
500     /**
501      * @param ldapApiService the ldapApiService to set
502      */
503     public void setLdapApiService( LdapApiService ldapApiService )
504     {
505         this.ldapApiService = ldapApiService;
506     }
507 }