1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.ldap.client.api;
22
23
24 import java.security.KeyStore;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.SecureRandom;
28
29 import javax.net.ssl.KeyManager;
30 import javax.net.ssl.TrustManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import javax.net.ssl.X509TrustManager;
33
34 import org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector;
35 import org.apache.directory.api.ldap.codec.api.LdapApiService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45 public class LdapConnectionConfig
46 {
47
48 private static final Logger LOG = LoggerFactory.getLogger( LdapConnectionConfig.class );
49
50
51 public static final int DEFAULT_LDAP_PORT = 389;
52
53
54 public static final int DEFAULT_LDAPS_PORT = 636;
55
56
57 public static final String DEFAULT_LDAP_HOST = "127.0.0.1";
58
59
60 public static final int LDAP_V3 = 3;
61
62
63 public static final long DEFAULT_TIMEOUT = 30000L;
64
65
66 public static final String DEFAULT_SSL_PROTOCOL = "TLS";
67
68
69
70 private boolean useSsl = false;
71
72
73 private long timeout = DEFAULT_TIMEOUT;
74
75
76 private boolean useTls = false;
77
78
79 private int ldapPort;
80
81
82 private String ldapHost;
83
84
85 private String name;
86
87
88 private String credentials;
89
90
91 private KeyManager[] keyManagers;
92
93
94 private SecureRandom secureRandom;
95
96
97 private TrustManager[] trustManagers;
98
99
100 private String[] enabledCipherSuites;
101
102
103 private String[] enabledProtocols;
104
105
106 private String sslProtocol = DEFAULT_SSL_PROTOCOL;
107
108
109 private BinaryAttributeDetector binaryAttributeDetector;
110
111
112 private LdapApiService ldapApiService;
113
114
115
116
117
118 public LdapConnectionConfig()
119 {
120 setDefaultTrustManager();
121 }
122
123
124
125
126
127 private void setDefaultTrustManager()
128 {
129 String trustMgmtAlgo = TrustManagerFactory.getDefaultAlgorithm();
130
131 try
132 {
133 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance( trustMgmtAlgo );
134 tmFactory.init( ( KeyStore ) null );
135
136 TrustManager factoryTrustManagers[] = tmFactory.getTrustManagers();
137
138 for ( int i = 0; i < factoryTrustManagers.length; i++ )
139 {
140 if ( factoryTrustManagers[i] instanceof X509TrustManager )
141 {
142 trustManagers = new TrustManager[]
143 { factoryTrustManagers[i] };
144 LOG.debug( "found X509TrustManager {}", factoryTrustManagers[i] );
145 break;
146 }
147 }
148 }
149 catch ( NoSuchAlgorithmException e )
150 {
151 LOG.warn( "couldn't find any default X509 TrustManager with algorithm {}", trustMgmtAlgo );
152 }
153 catch ( KeyStoreException e )
154 {
155 LOG.warn( "couldn't initialize TrustManagerFactory with keystore {}", KeyStore.getDefaultType() );
156 }
157 }
158
159
160
161
162
163
164
165 public boolean isUseSsl()
166 {
167 return useSsl;
168 }
169
170
171
172
173
174
175
176 public void setUseSsl( boolean useSsl )
177 {
178 this.useSsl = useSsl;
179 }
180
181
182
183
184
185
186
187 public int getLdapPort()
188 {
189 return ldapPort;
190 }
191
192
193
194
195
196
197
198 public void setLdapPort( int ldapPort )
199 {
200 this.ldapPort = ldapPort;
201 }
202
203
204
205
206
207
208
209 public String getLdapHost()
210 {
211 return ldapHost;
212 }
213
214
215
216
217
218
219
220 public void setLdapHost( String ldapHost )
221 {
222 this.ldapHost = ldapHost;
223 }
224
225
226
227
228
229
230
231 public String getName()
232 {
233 return name;
234 }
235
236
237
238
239
240
241
242 public void setName( String name )
243 {
244 this.name = name;
245 }
246
247
248
249
250
251
252
253 public String getCredentials()
254 {
255 return credentials;
256 }
257
258
259
260
261
262
263
264 public void setCredentials( String credentials )
265 {
266 this.credentials = credentials;
267 }
268
269
270
271
272
273
274
275 public int getDefaultLdapPort()
276 {
277 return DEFAULT_LDAP_PORT;
278 }
279
280
281
282
283
284
285
286 public int getDefaultLdapsPort()
287 {
288 return DEFAULT_LDAPS_PORT;
289 }
290
291
292
293
294
295
296
297 public String getDefaultLdapHost()
298 {
299 return DEFAULT_LDAP_HOST;
300 }
301
302
303
304
305
306
307
308 public long getDefaultTimeout()
309 {
310 return DEFAULT_TIMEOUT;
311 }
312
313
314
315
316
317
318
319 public long getTimeout()
320 {
321 return timeout;
322 }
323
324
325
326
327
328
329
330 public void setTimeout( long timeout )
331 {
332 this.timeout = timeout;
333 }
334
335
336
337
338
339
340
341 public int getSupportedLdapVersion()
342 {
343 return LDAP_V3;
344 }
345
346
347
348
349
350
351
352 public TrustManager[] getTrustManagers()
353 {
354 return trustManagers;
355 }
356
357
358
359
360
361
362
363 public void setTrustManagers( TrustManager... trustManagers )
364 {
365 this.trustManagers = trustManagers;
366 }
367
368
369
370
371
372
373
374 public String getSslProtocol()
375 {
376 return sslProtocol;
377 }
378
379
380
381
382
383
384
385 public void setSslProtocol( String sslProtocol )
386 {
387 this.sslProtocol = sslProtocol;
388 }
389
390
391
392
393
394
395
396 public KeyManager[] getKeyManagers()
397 {
398 return keyManagers;
399 }
400
401
402
403
404
405
406
407 public void setKeyManagers( KeyManager[] keyManagers )
408 {
409 this.keyManagers = keyManagers;
410 }
411
412
413
414
415
416
417
418 public SecureRandom getSecureRandom()
419 {
420 return secureRandom;
421 }
422
423
424
425
426
427
428
429 public void setSecureRandom( SecureRandom secureRandom )
430 {
431 this.secureRandom = secureRandom;
432 }
433
434
435
436
437
438
439
440 public String[] getEnabledCipherSuites()
441 {
442 return enabledCipherSuites;
443 }
444
445
446
447
448
449
450
451 public void setEnabledCipherSuites( String[] enabledCipherSuites )
452 {
453 this.enabledCipherSuites = enabledCipherSuites;
454 }
455
456
457
458
459
460
461
462 public String[] getEnabledProtocols()
463 {
464 return enabledProtocols;
465 }
466
467
468
469
470
471
472
473 public void setEnabledProtocols( String... enabledProtocols )
474 {
475 this.enabledProtocols = enabledProtocols;
476 }
477
478
479
480
481
482 public BinaryAttributeDetector getBinaryAttributeDetector()
483 {
484 return binaryAttributeDetector;
485 }
486
487
488
489
490
491 public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
492 {
493 this.binaryAttributeDetector = binaryAttributeDetector;
494 }
495
496
497
498
499
500
501
502 public boolean isUseTls()
503 {
504 return useTls;
505 }
506
507
508
509
510
511
512
513 public void setUseTls( boolean useTls )
514 {
515 this.useTls = useTls;
516 }
517
518
519
520
521
522 public LdapApiService getLdapApiService()
523 {
524 return ldapApiService;
525 }
526
527
528
529
530
531 public void setLdapApiService( LdapApiService ldapApiService )
532 {
533 this.ldapApiService = ldapApiService;
534 }
535 }