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