1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.util;
21
22
23 import java.util.Hashtable;
24
25 import javax.naming.AuthenticationException;
26 import javax.naming.AuthenticationNotSupportedException;
27 import javax.naming.CommunicationException;
28 import javax.naming.Context;
29 import javax.naming.ContextNotEmptyException;
30 import javax.naming.InvalidNameException;
31 import javax.naming.Name;
32 import javax.naming.NameAlreadyBoundException;
33 import javax.naming.NameNotFoundException;
34 import javax.naming.NamingException;
35 import javax.naming.NoPermissionException;
36 import javax.naming.OperationNotSupportedException;
37 import javax.naming.PartialResultException;
38 import javax.naming.ReferralException;
39 import javax.naming.ServiceUnavailableException;
40 import javax.naming.TimeLimitExceededException;
41 import javax.naming.directory.AttributeInUseException;
42 import javax.naming.directory.InvalidAttributeIdentifierException;
43 import javax.naming.directory.InvalidAttributeValueException;
44 import javax.naming.directory.InvalidSearchFilterException;
45 import javax.naming.directory.NoSuchAttributeException;
46 import javax.naming.directory.SchemaViolationException;
47 import javax.naming.ldap.LdapName;
48
49 import org.apache.directory.api.asn1.DecoderException;
50 import org.apache.directory.api.asn1.EncoderException;
51 import org.apache.directory.api.ldap.codec.api.LdapApiService;
52 import org.apache.directory.api.ldap.model.exception.LdapAffectMultipleDsaException;
53 import org.apache.directory.api.ldap.model.exception.LdapAliasDereferencingException;
54 import org.apache.directory.api.ldap.model.exception.LdapAliasException;
55 import org.apache.directory.api.ldap.model.exception.LdapAttributeInUseException;
56 import org.apache.directory.api.ldap.model.exception.LdapAuthenticationException;
57 import org.apache.directory.api.ldap.model.exception.LdapAuthenticationNotSupportedException;
58 import org.apache.directory.api.ldap.model.exception.LdapContextNotEmptyException;
59 import org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException;
60 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeTypeException;
61 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
62 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
63 import org.apache.directory.api.ldap.model.exception.LdapInvalidSearchFilterException;
64 import org.apache.directory.api.ldap.model.exception.LdapLoopDetectedException;
65 import org.apache.directory.api.ldap.model.exception.LdapNoPermissionException;
66 import org.apache.directory.api.ldap.model.exception.LdapNoSuchAttributeException;
67 import org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException;
68 import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
69 import org.apache.directory.api.ldap.model.exception.LdapOtherException;
70 import org.apache.directory.api.ldap.model.exception.LdapPartialResultException;
71 import org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException;
72 import org.apache.directory.api.ldap.model.exception.LdapReferralException;
73 import org.apache.directory.api.ldap.model.exception.LdapSchemaViolationException;
74 import org.apache.directory.api.ldap.model.exception.LdapServiceUnavailableException;
75 import org.apache.directory.api.ldap.model.exception.LdapTimeLimitExceededException;
76 import org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException;
77 import org.apache.directory.api.ldap.model.message.Control;
78 import org.apache.directory.api.ldap.model.name.Dn;
79
80
81
82
83
84
85
86 public final class JndiUtils
87 {
88
89
90
91 private JndiUtils()
92 {
93 }
94
95
96
97
98
99
100
101
102
103
104 @Deprecated
105 public static javax.naming.ldap.Control toJndiControl( LdapApiService codec, Control control )
106 throws EncoderException
107 {
108 return codec.toJndiControl( control );
109 }
110
111
112
113
114
115
116
117
118
119
120 @Deprecated
121 public static javax.naming.ldap.Control[] toJndiControls( LdapApiService codec, Control... controls )
122 throws EncoderException
123 {
124 if ( controls != null )
125 {
126 javax.naming.ldap.Control[] jndiControls = new javax.naming.ldap.Control[controls.length];
127 int i = 0;
128
129 for ( Control control : controls )
130 {
131 jndiControls[i++] = toJndiControl( codec, control );
132 }
133
134 return jndiControls;
135 }
136 else
137 {
138 return null;
139 }
140 }
141
142
143
144
145
146
147
148
149
150
151 @Deprecated
152 public static Control fromJndiRequestControl( LdapApiService codec, javax.naming.ldap.Control jndiControl )
153 throws DecoderException
154 {
155 return codec.fromJndiRequestControl( jndiControl );
156 }
157
158
159
160
161
162
163
164
165
166
167 @Deprecated
168 public static Control fromJndiResponseControl( LdapApiService codec, javax.naming.ldap.Control jndiControl )
169 throws DecoderException
170 {
171 return codec.fromJndiResponseControl( jndiControl );
172 }
173
174
175
176
177
178
179
180
181
182
183 @Deprecated
184 public static Control[] fromJndiRequestControls( LdapApiService codec, javax.naming.ldap.Control... jndiControls )
185 throws DecoderException
186 {
187 if ( jndiControls != null )
188 {
189 Control[] controls = new Control[jndiControls.length];
190 int i = 0;
191
192 for ( javax.naming.ldap.Control jndiControl : jndiControls )
193 {
194 controls[i++] = fromJndiRequestControl( codec, jndiControl );
195 }
196
197 return controls;
198 }
199 else
200 {
201 return null;
202 }
203 }
204
205
206
207
208
209
210
211
212 public static void wrap( Throwable t ) throws NamingException
213 {
214 if ( t instanceof NamingException )
215 {
216 throw ( NamingException ) t;
217 }
218
219 NamingException ne;
220
221 if ( ( t instanceof LdapAffectMultipleDsaException )
222 || ( t instanceof LdapAliasDereferencingException )
223 || ( t instanceof LdapLoopDetectedException )
224 || ( t instanceof LdapAliasException )
225 || ( t instanceof LdapOperationErrorException )
226 || ( t instanceof LdapOtherException ) )
227 {
228 ne = new NamingException( t.getLocalizedMessage() );
229 }
230 else if ( t instanceof LdapAttributeInUseException )
231 {
232 ne = new AttributeInUseException( t.getLocalizedMessage() );
233 }
234 else if ( t instanceof LdapAuthenticationException )
235 {
236 ne = new AuthenticationException( t.getLocalizedMessage() );
237 }
238 else if ( t instanceof LdapAuthenticationNotSupportedException )
239 {
240 ne = new AuthenticationNotSupportedException( t.getLocalizedMessage() );
241 }
242 else if ( t instanceof LdapContextNotEmptyException )
243 {
244 ne = new ContextNotEmptyException( t.getLocalizedMessage() );
245 }
246 else if ( t instanceof LdapEntryAlreadyExistsException )
247 {
248 ne = new NameAlreadyBoundException( t.getLocalizedMessage() );
249 }
250 else if ( t instanceof LdapInvalidAttributeTypeException )
251 {
252 ne = new InvalidAttributeIdentifierException( t.getLocalizedMessage() );
253 }
254 else if ( t instanceof LdapInvalidAttributeValueException )
255 {
256 ne = new InvalidAttributeValueException( t.getLocalizedMessage() );
257 }
258 else if ( t instanceof LdapInvalidDnException )
259 {
260 ne = new InvalidNameException( t.getLocalizedMessage() );
261 }
262 else if ( t instanceof LdapInvalidSearchFilterException )
263 {
264 ne = new InvalidSearchFilterException( t.getLocalizedMessage() );
265 }
266 else if ( t instanceof LdapNoPermissionException )
267 {
268 ne = new NoPermissionException( t.getLocalizedMessage() );
269 }
270 else if ( t instanceof LdapNoSuchAttributeException )
271 {
272 ne = new NoSuchAttributeException( t.getLocalizedMessage() );
273 }
274 else if ( t instanceof LdapNoSuchObjectException )
275 {
276 ne = new NameNotFoundException( t.getLocalizedMessage() );
277 }
278 else if ( t instanceof LdapProtocolErrorException )
279 {
280 ne = new CommunicationException( t.getLocalizedMessage() );
281 }
282 else if ( t instanceof LdapReferralException )
283 {
284 ne = new WrappedReferralException( ( LdapReferralException ) t );
285 }
286 else if ( t instanceof LdapPartialResultException )
287 {
288 ne = new WrappedPartialResultException( ( LdapPartialResultException ) t );
289 }
290 else if ( t instanceof LdapSchemaViolationException )
291 {
292 ne = new SchemaViolationException( t.getLocalizedMessage() );
293 }
294 else if ( t instanceof LdapServiceUnavailableException )
295 {
296 ne = new ServiceUnavailableException( t.getLocalizedMessage() );
297 }
298 else if ( t instanceof LdapTimeLimitExceededException )
299 {
300 ne = new TimeLimitExceededException( t.getLocalizedMessage() );
301 }
302 else if ( t instanceof LdapUnwillingToPerformException )
303 {
304 ne = new OperationNotSupportedException( t.getLocalizedMessage() );
305 }
306 else
307 {
308 ne = new NamingException( t.getLocalizedMessage() );
309 }
310
311 ne.setRootCause( t );
312
313 throw ne;
314 }
315
316
317
318
319
320
321
322
323 public static Name toName( Dn dn )
324 {
325 try
326 {
327 return new LdapName( dn.toString() );
328 }
329 catch ( InvalidNameException ine )
330 {
331
332
333 return null;
334 }
335 }
336
337
338
339
340
341
342
343
344 public static Dn fromName( Name name )
345 {
346 try
347 {
348 return new Dn( name.toString() );
349 }
350 catch ( LdapInvalidDnException lide )
351 {
352
353
354 return null;
355 }
356 }
357 }
358
359
360 class WrappedReferralException extends ReferralException
361 {
362 private static final long serialVersionUID = 1L;
363
364 private LdapReferralException lre;
365
366
367 WrappedReferralException( LdapReferralException lre )
368 {
369 this.lre = lre;
370 }
371
372
373
374
375
376 @Override
377 public boolean skipReferral()
378 {
379 return lre.skipReferral();
380 }
381
382
383
384
385
386 @Override
387 public void retryReferral()
388 {
389 lre.retryReferral();
390 }
391
392
393
394
395
396 @Override
397 public Object getReferralInfo()
398 {
399 return lre.getReferralInfo();
400 }
401
402
403
404
405
406 @Override
407 public Context getReferralContext( Hashtable<?, ?> env ) throws NamingException
408 {
409 return lre.getReferralContext( env );
410 }
411
412
413
414
415
416 @Override
417 public Context getReferralContext() throws NamingException
418 {
419 return lre.getReferralContext();
420 }
421
422
423
424
425
426 @Override
427 public Name getRemainingName()
428 {
429 return JndiUtils.toName( lre.getRemainingDn() );
430 }
431
432
433
434
435
436 @Override
437 public Object getResolvedObj()
438 {
439 return lre.getResolvedObject();
440 }
441
442
443
444
445
446 @Override
447 public Name getResolvedName()
448 {
449 return JndiUtils.toName( lre.getResolvedDn() );
450 }
451 }
452
453
454 class WrappedPartialResultException extends PartialResultException
455 {
456 private static final long serialVersionUID = 1L;
457
458 private LdapPartialResultException lpre;
459
460
461 WrappedPartialResultException( LdapPartialResultException lpre )
462 {
463 this.lpre = lpre;
464 }
465
466
467
468
469
470 @Override
471 public Name getRemainingName()
472 {
473 return JndiUtils.toName( lpre.getRemainingDn() );
474 }
475
476
477
478
479
480 @Override
481 public Object getResolvedObj()
482 {
483 return lpre.getResolvedObject();
484 }
485
486
487
488
489
490 @Override
491 public Name getResolvedName()
492 {
493 return JndiUtils.toName( lpre.getResolvedDn() );
494 }
495 }