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   *     http://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      // @TODO not really needed and can be moved out
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     // @TODO not really needed and can be moved out
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     // @TODO not really needed and can be moved out
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     // @TODO not really needed and can be moved out
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      * Convert a Dn to a {@link javax.naming.Name}
280      *
281      * @param name The Dn to convert
282      * @return A Name
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             // TODO : check if we must throw an exception.
295             // Logically, the Dn must be valid.
296             return null;
297         }
298     }
299 
300 
301     /**
302      * Convert a {@link javax.naming.Name} to a Dn
303      *
304      * @param name The Name to convert
305      * @return A Dn
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             // TODO : check if we must throw an exception.
318             // Logically, the Name must be valid.
319             return null;
320         }
321     }
322 }
323 
324 // a ReferralException around the LdapReferralException to be used in tests
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 // a PartialResultException around the LdapPartialResultException to be used in tests
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 }