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 boolean useTls = false;
74
75
76 private int ldapPort;
77
78
79 private String ldapHost;
80
81
82 private String name;
83
84
85 private String credentials;
86
87
88 private KeyManager[] keyManagers;
89
90
91 private SecureRandom secureRandom;
92
93
94 private TrustManager[] trustManagers;
95
96
97 private String[] enabledCipherSuites;
98
99
100 private String sslProtocol = DEFAULT_SSL_PROTOCOL;
101
102
103 private BinaryAttributeDetector binaryAttributeDetector;
104
105
106 private LdapApiService ldapApiService;
107
108
109
110
111
112 public LdapConnectionConfig()
113 {
114 setDefaultTrustManager();
115 }
116
117
118
119
120
121 private void setDefaultTrustManager()
122 {
123 String trustMgmtAlgo = TrustManagerFactory.getDefaultAlgorithm();
124
125 try
126 {
127 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance( trustMgmtAlgo );
128 tmFactory.init( ( KeyStore ) null );
129
130 TrustManager factoryTrustManagers[] = tmFactory.getTrustManagers();
131
132 for ( int i = 0; i < factoryTrustManagers.length; i++ )
133 {
134 if ( factoryTrustManagers[i] instanceof X509TrustManager )
135 {
136 trustManagers = new TrustManager[]
137 { factoryTrustManagers[i] };
138 LOG.debug( "found X509TrustManager {}", factoryTrustManagers[i] );
139 break;
140 }
141 }
142 }
143 catch ( NoSuchAlgorithmException e )
144 {
145 LOG.warn( "couldn't find any default X509 TrustManager with algorithm {}", trustMgmtAlgo );
146 }
147 catch ( KeyStoreException e )
148 {
149 LOG.warn( "couldn't initialize TrustManagerFactory with keystore {}", KeyStore.getDefaultType() );
150 }
151 }
152
153
154
155
156
157
158
159 public boolean isUseSsl()
160 {
161 return useSsl;
162 }
163
164
165
166
167
168
169
170 public void setUseSsl( boolean useSsl )
171 {
172 this.useSsl = useSsl;
173 }
174
175
176
177
178
179
180
181 public int getLdapPort()
182 {
183 return ldapPort;
184 }
185
186
187
188
189
190
191
192 public void setLdapPort( int ldapPort )
193 {
194 this.ldapPort = ldapPort;
195 }
196
197
198
199
200
201
202
203 public String getLdapHost()
204 {
205 return ldapHost;
206 }
207
208
209
210
211
212
213
214 public void setLdapHost( String ldapHost )
215 {
216 this.ldapHost = ldapHost;
217 }
218
219
220
221
222
223
224
225 public String getName()
226 {
227 return name;
228 }
229
230
231
232
233
234
235
236 public void setName( String name )
237 {
238 this.name = name;
239 }
240
241
242
243
244
245
246
247 public String getCredentials()
248 {
249 return credentials;
250 }
251
252
253
254
255
256
257
258 public void setCredentials( String credentials )
259 {
260 this.credentials = credentials;
261 }
262
263
264
265
266
267
268
269 public int getDefaultLdapPort()
270 {
271 return DEFAULT_LDAP_PORT;
272 }
273
274
275
276
277
278
279
280 public int getDefaultLdapsPort()
281 {
282 return DEFAULT_LDAPS_PORT;
283 }
284
285
286
287
288
289
290
291 public String getDefaultLdapHost()
292 {
293 return DEFAULT_LDAP_HOST;
294 }
295
296
297
298
299
300
301
302 public long getDefaultTimeout()
303 {
304 return DEFAULT_TIMEOUT;
305 }
306
307
308
309
310
311
312
313 public int getSupportedLdapVersion()
314 {
315 return LDAP_V3;
316 }
317
318
319
320
321
322
323
324 public TrustManager[] getTrustManagers()
325 {
326 return trustManagers;
327 }
328
329
330
331
332
333
334
335 public void setTrustManagers( TrustManager... trustManagers )
336 {
337 this.trustManagers = trustManagers;
338 }
339
340
341
342
343
344
345
346 public String getSslProtocol()
347 {
348 return sslProtocol;
349 }
350
351
352
353
354
355
356
357 public void setSslProtocol( String sslProtocol )
358 {
359 this.sslProtocol = sslProtocol;
360 }
361
362
363
364
365
366
367
368 public KeyManager[] getKeyManagers()
369 {
370 return keyManagers;
371 }
372
373
374
375
376
377
378
379 public void setKeyManagers( KeyManager[] keyManagers )
380 {
381 this.keyManagers = keyManagers;
382 }
383
384
385
386
387
388
389
390 public SecureRandom getSecureRandom()
391 {
392 return secureRandom;
393 }
394
395
396
397
398
399
400
401 public void setSecureRandom( SecureRandom secureRandom )
402 {
403 this.secureRandom = secureRandom;
404 }
405
406
407
408
409
410
411
412 public String[] getEnabledCipherSuites()
413 {
414 return enabledCipherSuites;
415 }
416
417
418
419
420
421
422
423 public void setEnabledCipherSuites( String[] enabledCipherSuites )
424 {
425 this.enabledCipherSuites = enabledCipherSuites;
426 }
427
428
429
430
431
432 public BinaryAttributeDetector getBinaryAttributeDetector()
433 {
434 return binaryAttributeDetector;
435 }
436
437
438
439
440
441 public void setBinaryAttributeDetector( BinaryAttributeDetector binaryAttributeDetector )
442 {
443 this.binaryAttributeDetector = binaryAttributeDetector;
444 }
445
446
447
448
449
450
451
452 public boolean isUseTls()
453 {
454 return useTls;
455 }
456
457
458
459
460
461
462
463 public void setUseTls( boolean useTls )
464 {
465 this.useTls = useTls;
466 }
467
468
469
470
471
472 public LdapApiService getLdapApiService()
473 {
474 return ldapApiService;
475 }
476
477
478
479
480
481 public void setLdapApiService( LdapApiService ldapApiService )
482 {
483 this.ldapApiService = ldapApiService;
484 }
485 }