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 fromJndiControl( LdapApiService codec, javax.naming.ldap.Control jndiControl )
153 throws DecoderException
154 {
155 return codec.fromJndiControl( jndiControl );
156 }
157
158
159
160
161
162
163
164
165
166
167 @Deprecated
168 public static Control[] fromJndiControls( LdapApiService codec, javax.naming.ldap.Control... jndiControls )
169 throws DecoderException
170 {
171 if ( jndiControls != null )
172 {
173 Control[] controls = new Control[jndiControls.length];
174 int i = 0;
175
176 for ( javax.naming.ldap.Control jndiControl : jndiControls )
177 {
178 controls[i++] = fromJndiControl( codec, jndiControl );
179 }
180
181 return controls;
182 }
183 else
184 {
185 return null;
186 }
187 }
188
189
190
191
192
193
194
195
196 public static void wrap( Throwable t ) throws NamingException
197 {
198 if ( t instanceof NamingException )
199 {
200 throw ( NamingException ) t;
201 }
202
203 NamingException ne = null;
204
205 if ( t instanceof LdapAffectMultipleDsaException )
206 {
207 ne = new NamingException( t.getLocalizedMessage() );
208 }
209 else if ( t instanceof LdapAliasDereferencingException )
210 {
211 ne = new NamingException( t.getLocalizedMessage() );
212 }
213 else if ( t instanceof LdapAliasException )
214 {
215 ne = new NamingException( t.getLocalizedMessage() );
216 }
217 else if ( t instanceof LdapAttributeInUseException )
218 {
219 ne = new AttributeInUseException( t.getLocalizedMessage() );
220 }
221 else if ( t instanceof LdapAuthenticationException )
222 {
223 ne = new AuthenticationException( t.getLocalizedMessage() );
224 }
225 else if ( t instanceof LdapAuthenticationNotSupportedException )
226 {
227 ne = new AuthenticationNotSupportedException( t.getLocalizedMessage() );
228 }
229 else if ( t instanceof LdapContextNotEmptyException )
230 {
231 ne = new ContextNotEmptyException( t.getLocalizedMessage() );
232 }
233 else if ( t instanceof LdapEntryAlreadyExistsException )
234 {
235 ne = new NameAlreadyBoundException( t.getLocalizedMessage() );
236 }
237 else if ( t instanceof LdapInvalidAttributeTypeException )
238 {
239 ne = new InvalidAttributeIdentifierException( t.getLocalizedMessage() );
240 }
241 else if ( t instanceof LdapInvalidAttributeValueException )
242 {
243 ne = new InvalidAttributeValueException( t.getLocalizedMessage() );
244 }
245 else if ( t instanceof LdapInvalidDnException )
246 {
247 ne = new InvalidNameException( t.getLocalizedMessage() );
248 }
249 else if ( t instanceof LdapInvalidSearchFilterException )
250 {
251 ne = new InvalidSearchFilterException( t.getLocalizedMessage() );
252 }
253 else if ( t instanceof LdapLoopDetectedException )
254 {
255 ne = new NamingException( t.getLocalizedMessage() );
256 }
257 else if ( t instanceof LdapNoPermissionException )
258 {
259 ne = new NoPermissionException( t.getLocalizedMessage() );
260 }
261 else if ( t instanceof LdapNoSuchAttributeException )
262 {
263 ne = new NoSuchAttributeException( t.getLocalizedMessage() );
264 }
265 else if ( t instanceof LdapNoSuchObjectException )
266 {
267 ne = new NameNotFoundException( t.getLocalizedMessage() );
268 }
269 else if ( t instanceof LdapOperationErrorException )
270 {
271 ne = new NamingException( t.getLocalizedMessage() );
272 }
273 else if ( t instanceof LdapOtherException )
274 {
275 ne = new NamingException( t.getLocalizedMessage() );
276 }
277 else if ( t instanceof LdapProtocolErrorException )
278 {
279 ne = new CommunicationException( t.getLocalizedMessage() );
280 }
281 else if ( t instanceof LdapReferralException )
282 {
283 ne = new WrappedReferralException( ( LdapReferralException ) t );
284 }
285 else if ( t instanceof LdapPartialResultException )
286 {
287 ne = new WrappedPartialResultException( ( LdapPartialResultException ) t );
288 }
289 else if ( t instanceof LdapSchemaViolationException )
290 {
291 ne = new SchemaViolationException( t.getLocalizedMessage() );
292 }
293 else if ( t instanceof LdapServiceUnavailableException )
294 {
295 ne = new ServiceUnavailableException( t.getLocalizedMessage() );
296 }
297 else if ( t instanceof LdapTimeLimitExceededException )
298 {
299 ne = new TimeLimitExceededException( t.getLocalizedMessage() );
300 }
301 else if ( t instanceof LdapUnwillingToPerformException )
302 {
303 ne = new OperationNotSupportedException( t.getLocalizedMessage() );
304 }
305 else
306 {
307 ne = new NamingException( t.getLocalizedMessage() );
308 }
309
310 ne.setRootCause( t );
311
312 throw ne;
313 }
314
315
316
317
318
319
320
321
322 public static Name toName( Dn dn )
323 {
324 try
325 {
326 return new LdapName( dn.toString() );
327 }
328 catch ( InvalidNameException ine )
329 {
330
331
332 return null;
333 }
334 }
335
336
337
338
339
340
341
342
343 public static Dn fromName( Name name )
344 {
345 try
346 {
347 Dn dn = new Dn( name.toString() );
348
349 return dn;
350 }
351 catch ( LdapInvalidDnException lide )
352 {
353
354
355 return null;
356 }
357 }
358 }
359
360
361 class WrappedReferralException extends ReferralException
362 {
363 private static final long serialVersionUID = 1L;
364
365 private LdapReferralException lre;
366
367
368 WrappedReferralException( LdapReferralException lre )
369 {
370 this.lre = lre;
371 }
372
373
374 @Override
375 public boolean skipReferral()
376 {
377 return lre.skipReferral();
378 }
379
380
381 @Override
382 public void retryReferral()
383 {
384 lre.retryReferral();
385 }
386
387
388 @Override
389 public Object getReferralInfo()
390 {
391 return lre.getReferralInfo();
392 }
393
394
395 @Override
396 public Context getReferralContext( Hashtable<?, ?> env ) throws NamingException
397 {
398 return lre.getReferralContext( env );
399 }
400
401
402 @Override
403 public Context getReferralContext() throws NamingException
404 {
405 return lre.getReferralContext();
406 }
407
408
409 @Override
410 public Name getRemainingName()
411 {
412 return JndiUtils.toName( lre.getRemainingDn() );
413 }
414
415
416 @Override
417 public Object getResolvedObj()
418 {
419 return lre.getResolvedObject();
420 }
421
422
423 @Override
424 public Name getResolvedName()
425 {
426 return JndiUtils.toName( lre.getResolvedDn() );
427 }
428 }
429
430
431 class WrappedPartialResultException extends PartialResultException
432 {
433 private static final long serialVersionUID = 1L;
434
435 private LdapPartialResultException lpre;
436
437
438 WrappedPartialResultException( LdapPartialResultException lpre )
439 {
440 this.lpre = lpre;
441 }
442
443
444 @Override
445 public Name getRemainingName()
446 {
447 return JndiUtils.toName( lpre.getRemainingDn() );
448 }
449
450
451 @Override
452 public Object getResolvedObj()
453 {
454 return lpre.getResolvedObject();
455 }
456
457
458 @Override
459 public Name getResolvedName()
460 {
461 return JndiUtils.toName( lpre.getResolvedDn() );
462 }
463 }