View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * An utility class to convert back and forth JNDI classes to ADS classes.
83   * 
84   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
85   */
86  public final class JndiUtils
87  {
88      /**
89       * Private constructor.
90       */
91      private JndiUtils()
92      {
93      }
94  
95  
96      /**
97       * Convert a LDAP API control to a JNDI control
98       * @param codec The LDAP API service to use
99       * @param control The control to convert
100      * @return A JNDI control
101      * @throws EncoderException If the conversion failed
102      * @deprecated We don't use JNDI anymore
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      * Convert some LDAP API controls to JNDI controls
114      * @param codec The LDAP API service to use
115      * @param controls The controls to convert
116      * @return Array of JNDI control
117      * @throws EncoderException If the conversion failed
118      * @deprecated We don't use JNDI anymore
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      * Convert a JNDI request control to a LDAP API control
145      * @param codec The LDAP API service to use
146      * @param jndiControl The control to convert
147      * @return A LDAP API request control
148      * @throws DecoderException If the conversion failed
149      * @deprecated We don't use JNDI anymore
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      * Convert a JNDI response control to a LDAP API control
161      * @param codec The LDAP API service to use
162      * @param jndiControl The control to convert
163      * @return A LDAP API response control
164      * @throws DecoderException If the conversion failed
165      * @deprecated We don't use JNDI anymore
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      * Convert some JNDI controls to LDAP API controls
177      * @param codec The LDAP API service to use
178      * @param jndiControls The controls to convert
179      * @return An array of LDAP API control
180      * @throws DecoderException If the conversion failed
181      * @deprecated We don't use JNDI anymore
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      * Wraps a LDAP exception into a NaingException
208      * 
209      * @param t The original exception
210      * @throws NamingException The wrapping JNDI exception
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      * Convert a Dn to a {@link javax.naming.Name}
319      *
320      * @param dn The Dn to convert
321      * @return A Name
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             // TODO : check if we must throw an exception.
332             // Logically, the Dn must be valid.
333             return null;
334         }
335     }
336 
337 
338     /**
339      * Convert a {@link javax.naming.Name} to a Dn
340      *
341      * @param name The Name to convert
342      * @return A Dn
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             // TODO : check if we must throw an exception.
353             // Logically, the Name must be valid.
354             return null;
355         }
356     }
357 }
358 
359 // a ReferralException around the LdapReferralException to be used in tests
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      * {@inheritDoc}
375      */
376     @Override
377     public boolean skipReferral()
378     {
379         return lre.skipReferral();
380     }
381 
382 
383     /**
384      * {@inheritDoc}
385      */
386     @Override
387     public void retryReferral()
388     {
389         lre.retryReferral();
390     }
391 
392 
393     /**
394      * {@inheritDoc}
395      */
396     @Override
397     public Object getReferralInfo()
398     {
399         return lre.getReferralInfo();
400     }
401 
402 
403     /**
404      * {@inheritDoc}
405      */
406     @Override
407     public Context getReferralContext( Hashtable<?, ?> env ) throws NamingException
408     {
409         return lre.getReferralContext( env );
410     }
411 
412 
413     /**
414      * {@inheritDoc}
415      */
416     @Override
417     public Context getReferralContext() throws NamingException
418     {
419         return lre.getReferralContext();
420     }
421 
422 
423     /**
424      * {@inheritDoc}
425      */
426     @Override
427     public Name getRemainingName()
428     {
429         return JndiUtils.toName( lre.getRemainingDn() );
430     }
431 
432 
433     /**
434      * {@inheritDoc}
435      */
436     @Override
437     public Object getResolvedObj()
438     {
439         return lre.getResolvedObject();
440     }
441 
442 
443     /**
444      * {@inheritDoc}
445      */
446     @Override
447     public Name getResolvedName()
448     {
449         return JndiUtils.toName( lre.getResolvedDn() );
450     }
451 }
452 
453 // a PartialResultException around the LdapPartialResultException to be used in tests
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      * {@inheritDoc}
469      */
470     @Override
471     public Name getRemainingName()
472     {
473         return JndiUtils.toName( lpre.getRemainingDn() );
474     }
475 
476 
477     /**
478      * {@inheritDoc}
479      */
480     @Override
481     public Object getResolvedObj()
482     {
483         return lpre.getResolvedObject();
484     }
485 
486 
487     /**
488      * {@inheritDoc}
489      */
490     @Override
491     public Name getResolvedName()
492     {
493         return JndiUtils.toName( lpre.getResolvedDn() );
494     }
495 }