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
33 import org.apache.directory.api.i18n.I18n;
34 import org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector;
35 import org.apache.directory.api.ldap.codec.api.LdapApiService;
36 import org.apache.directory.api.util.Network;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46 public class LdapConnectionConfig
47 {
48
49 private static final Logger LOG = LoggerFactory.getLogger( LdapConnectionConfig.class );
50
51
52 public static final int DEFAULT_LDAP_PORT = 389;
53
54
55 public static final int DEFAULT_LDAPS_PORT = 636;
56
57
58 public static final String DEFAULT_LDAP_HOST = "localhost";
59
60
61 public static final int LDAP_V3 = 3;
62
63
64 public static final long DEFAULT_TIMEOUT = 30000L;
65
66
67 public static final String DEFAULT_SSL_PROTOCOL = "TLS";
68
69
70
71 private boolean useSsl = false;
72
73
74 private long timeout = DEFAULT_TIMEOUT;
75
76
77 private Long connectTimeout;
78
79
80 private Long writeOperationTimeout;
81
82
83 private Long readOperationTimeout;
84
85
86 private Long closeTimeout;
87
88
89 private Long sendTimeout;
90
91
92 private boolean useTls = false;
93
94
95 private int ldapPort;
96
97
98 private String ldapHost;
99
100
101 private String name;
102
103
104 private String credentials;
105
106
107 private KeyManager[] keyManagers;
108
109
110 private SecureRandom secureRandom;
111
112
113 private TrustManager[] trustManagers;
114
115
116 private String[] enabledCipherSuites;
117
118
119 private String[] enabledProtocols;
120
121
122 private String sslProtocol = DEFAULT_SSL_PROTOCOL;
123
124
125 private BinaryAttributeDetector binaryAttributeDetector;
126
127
128 private LdapApiService ldapApiService;
129
130
131
132
133
134 public LdapConnectionConfig()
135 {
136 setDefaultTrustManager();
137 }
138
139
140
141
142
143 private void setDefaultTrustManager()
144 {
145 String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
146
147 try
148 {
149 TrustManagerFactory tmf = TrustManagerFactory.getInstance( defaultAlgorithm );
150 tmf.init( ( KeyStore ) null );
151 trustManagers = tmf.getTrustManagers();
152 }
153 catch ( KeyStoreException kse )
154 {
155 LOG.error( I18n.err( I18n.ERR_04172_KEYSTORE_INIT_FAILURE ) );
156 throw new RuntimeException( kse.getMessage(), kse );
157 }
158 catch ( NoSuchAlgorithmException nsae )
159 {
160 LOG.error( I18n.err( I18n.ERR_04173_ALGORITHM_NOT_FOUND, defaultAlgorithm ) );
161 throw new RuntimeException( nsae.getMessage(), nsae );
162 }
163 }
164
165
166
167
168
169
170
171 public boolean isUseSsl()
172 {
173 return useSsl;
174 }
175
176
177
178
179
180
181
182 public void setUseSsl( boolean useSsl )
183 {
184 this.useSsl = useSsl;
185 }
186
187
188
189
190
191
192
193 public int getLdapPort()
194 {
195 return ldapPort;
196 }
197
198
199
200
201
202
203
204 public void setLdapPort( int ldapPort )
205 {
206 this.ldapPort = ldapPort;
207 }
208
209
210
211
212
213
214
215 public String getLdapHost()
216 {
217 return ldapHost;
218 }
219
220
221
222
223
224
225
226 public void setLdapHost( String ldapHost )
227 {
228 this.ldapHost = ldapHost;
229 }
230
231
232
233
234
235
236
237 public String getName()
238 {
239 return name;
240 }
241
242
243
244
245
246
247
248 public void setName( String name )
249 {
250 this.name = name;
251 }
252
253
254
255
256
257
258
259 public String getCredentials()
260 {
261 return credentials;
262 }
263
264
265
266
267
268
269
270 public void setCredentials( String credentials )
271 {
272 this.credentials = credentials;
273 }
274
275
276
277
278
279
280
281 public int getDefaultLdapPort()
282 {
283 return DEFAULT_LDAP_PORT;
284 }
285
286
287
288
289
290
291
292 public int getDefaultLdapsPort()
293 {
294 return DEFAULT_LDAPS_PORT;
295 }
296
297
298
299
300
301
302
303 public String getDefaultLdapHost()
304 {
305 return Network.LOOPBACK_HOSTNAME;
306 }
307
308
309
310
311
312
313
314 public long getDefaultTimeout()
315 {
316 return DEFAULT_TIMEOUT;
317 }
318
319
320
321
322
323
324
325
326
327
328 public long getTimeout()
329 {
330 return timeout;
331 }
332
333
334
335
336
337
338
339
340
341
342 public void setTimeout( long timeout )
343 {
344 if ( timeout == -1L )
345 {
346 this.timeout = Long.MAX_VALUE;
347 }
348 else
349 {
350 this.timeout = timeout;
351 }
352 }
353
354
355
356
357
358
359
360
361
362 public Long getConnectTimeout()
363 {
364 return connectTimeout;
365 }
366
367
368
369
370
371
372
373
374
375 public void setConnectTimeout( Long timeout )
376 {
377 if ( timeout == -1L )
378 {
379 this.connectTimeout = Long.MAX_VALUE;
380 }
381 else
382 {
383 this.connectTimeout = timeout;
384 }
385 }
386
387
388
389
390
391
392
393
394
395 public Long getWriteOperationTimeout()
396 {
397 return writeOperationTimeout;
398 }
399
400
401
402
403
404
405
406
407
408 public void setWriteOperationTimeout( Long timeout )
409 {
410 if ( timeout == -1L )
411 {
412 this.writeOperationTimeout = Long.MAX_VALUE;
413 }
414 else
415 {
416 this.writeOperationTimeout = timeout;
417 }
418 }
419
420
421
422
423
424
425
426
427
428 public Long getReadOperationTimeout()
429 {
430 return readOperationTimeout;
431 }
432
433
434
435
436
437
438
439
440
441 public void setReadOperationTimeout( Long timeout )
442 {
443 if ( timeout == -1L )
444 {
445 this.readOperationTimeout = Long.MAX_VALUE;
446 }
447 else
448 {
449 this.readOperationTimeout = timeout;
450 }
451 }
452
453
454
455
456
457
458
459
460
461 public Long getCloseTimeout()
462 {
463 return closeTimeout;
464 }
465
466
467
468
469
470
471
472
473
474 public void setCloseTimeout( Long timeout )
475 {
476 if ( timeout == -1L )
477 {
478 this.closeTimeout = Long.MAX_VALUE;
479 }
480 else
481 {
482 this.closeTimeout = timeout;
483 }
484 }
485
486
487
488
489
490
491
492
493
494 public Long getSendTimeout()
495 {
496 return sendTimeout;
497 }
498
499
500
501
502
503
504
505
506
507 public void setSendTimeout( Long timeout )
508 {
509 if ( timeout == -1L )
510 {
511 this.sendTimeout = Long.MAX_VALUE;
512 }
513 else
514 {
515 this.sendTimeout = timeout;
516 }
517 }
518
519
520
521
522
523
524
525 public int getSupportedLdapVersion()
526 {
527 return LDAP_V3;
528 }
529
530
531
532
533
534
535
536 public TrustManager[] getTrustManagers()
537 {
538 return trustManagers;
539 }
540
541
542
543
544
545
546
547
548 public void setTrustManagers( TrustManager... trustManagers )
549 {
550 if ( ( trustManagers == null ) || ( trustManagers.length == 0 )
551 || ( trustManagers.length == 1 && trustManagers[0] == null ) )
552 {
553 throw new IllegalArgumentException( "TrustManagers must not be null or empty" );
554 }
555 this.trustManagers = trustManagers;
556 }
557
558
559
560
561
562
563
564 public String getSslProtocol()
565 {
566 return sslProtocol;
567 }
568
569
570
571
572
573
574
575 public void setSslProtocol( String sslProtocol )
576 {
577 this.sslProtocol = sslProtocol;
578 }
579
580
581
582
583
584
585
586 public KeyManager[] getKeyManagers()
587 {
588 return keyManagers;
589 }
590
591
592
593
594
595
596
597 public void setKeyManagers( KeyManager[] keyManagers )
598 {
599 this.keyManagers = keyManagers;
600 }
601
602
603
604
605
606
607
608 public SecureRandom getSecureRandom()
609 {
610 return secureRandom;
611 }
612
613
614
615
616
617
618
619 public void setSecureRandom( SecureRandom secureRandom )
620 {
621 this.secureRandom = secureRandom;
622 }
623
624
625
626
627
628
629
630 public String[] getEnabledCipherSuites()
631 {
632 return enabledCipherSuites;
633 }
634
635
636
637
638
639
640
641 public void setEnabledCipherSuites( String[] enabledCipherSuites )
642 {
643 this.enabledCipherSuites = enabledCipherSuites;
644 }
645
646
647
648
649
650
651
652 public String[] getEnabledProtocols()
653 {
654 return enabledProtocols;
655 }
656
657
658
659
660
661
662
663 public void setEnabledProtocols( String... enabledProtocols )
664 {
665 this.enabledProtocols = enabledProtocols;
666 }
667
668
669
670
671
672 public BinaryAttributeDetector getBinaryAttributeDetector()
673 {
674 return binaryAttributeDetector;
675 }
676
677
678
679
680
681 public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
682 {
683 this.binaryAttributeDetector = binaryAttributeDetector;
684 }
685
686
687
688
689
690
691
692 public boolean isUseTls()
693 {
694 return useTls;
695 }
696
697
698
699
700
701
702
703 public void setUseTls( boolean useTls )
704 {
705 this.useTls = useTls;
706 }
707
708
709
710
711
712 public LdapApiService getLdapApiService()
713 {
714 return ldapApiService;
715 }
716
717
718
719
720
721 public void setLdapApiService( LdapApiService ldapApiService )
722 {
723 this.ldapApiService = ldapApiService;
724 }
725 }