001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.shared.ldap.jndi; 021 022 import java.util.Hashtable; 023 024 import javax.naming.AuthenticationException; 025 import javax.naming.AuthenticationNotSupportedException; 026 import javax.naming.CommunicationException; 027 import javax.naming.Context; 028 import javax.naming.ContextNotEmptyException; 029 import javax.naming.InvalidNameException; 030 import javax.naming.Name; 031 import javax.naming.NameAlreadyBoundException; 032 import javax.naming.NameNotFoundException; 033 import javax.naming.NamingException; 034 import javax.naming.NoPermissionException; 035 import javax.naming.OperationNotSupportedException; 036 import javax.naming.PartialResultException; 037 import javax.naming.ReferralException; 038 import javax.naming.ServiceUnavailableException; 039 import javax.naming.TimeLimitExceededException; 040 import javax.naming.directory.AttributeInUseException; 041 import javax.naming.directory.InvalidAttributeIdentifierException; 042 import javax.naming.directory.InvalidAttributeValueException; 043 import javax.naming.directory.InvalidSearchFilterException; 044 import javax.naming.directory.NoSuchAttributeException; 045 import javax.naming.directory.SchemaViolationException; 046 import javax.naming.ldap.BasicControl; 047 048 import org.apache.directory.shared.ldap.codec.controls.ControlImpl; 049 import org.apache.directory.shared.ldap.exception.LdapAffectMultipleDsaException; 050 import org.apache.directory.shared.ldap.exception.LdapAliasDereferencingException; 051 import org.apache.directory.shared.ldap.exception.LdapAliasException; 052 import org.apache.directory.shared.ldap.exception.LdapAttributeInUseException; 053 import org.apache.directory.shared.ldap.exception.LdapAuthenticationException; 054 import org.apache.directory.shared.ldap.exception.LdapAuthenticationNotSupportedException; 055 import org.apache.directory.shared.ldap.exception.LdapContextNotEmptyException; 056 import org.apache.directory.shared.ldap.exception.LdapEntryAlreadyExistsException; 057 import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeTypeException; 058 import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeValueException; 059 import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; 060 import org.apache.directory.shared.ldap.exception.LdapInvalidSearchFilterException; 061 import org.apache.directory.shared.ldap.exception.LdapLoopDetectedException; 062 import org.apache.directory.shared.ldap.exception.LdapNoPermissionException; 063 import org.apache.directory.shared.ldap.exception.LdapNoSuchAttributeException; 064 import org.apache.directory.shared.ldap.exception.LdapNoSuchObjectException; 065 import org.apache.directory.shared.ldap.exception.LdapOperationErrorException; 066 import org.apache.directory.shared.ldap.exception.LdapOtherException; 067 import org.apache.directory.shared.ldap.exception.LdapPartialResultException; 068 import org.apache.directory.shared.ldap.exception.LdapProtocolErrorException; 069 import org.apache.directory.shared.ldap.exception.LdapReferralException; 070 import org.apache.directory.shared.ldap.exception.LdapSchemaViolationException; 071 import org.apache.directory.shared.ldap.exception.LdapServiceUnavailableException; 072 import org.apache.directory.shared.ldap.exception.LdapTimeLimitExceededException; 073 import org.apache.directory.shared.ldap.exception.LdapUnwillingToPerformException; 074 import org.apache.directory.shared.ldap.message.control.Control; 075 import org.apache.directory.shared.ldap.name.DN; 076 077 /** 078 * An utility class to convert back and forth JNDI classes to ADS classes. 079 * 080 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 081 * @version $Rev$, $Date$ 082 */ 083 public class JndiUtils 084 { 085 public static javax.naming.ldap.Control toJndiControl( Control control ) 086 { 087 byte[] value = control.getValue(); 088 javax.naming.ldap.Control jndiControl = new BasicControl( control.getOid(), control.isCritical(), value ); 089 090 return jndiControl; 091 } 092 093 094 public static javax.naming.ldap.Control[] toJndiControls( Control... controls ) 095 { 096 if ( controls != null ) 097 { 098 javax.naming.ldap.Control[] jndiControls = new javax.naming.ldap.Control[controls.length]; 099 int i = 0; 100 101 for ( Control control : controls ) 102 { 103 jndiControls[i++] = toJndiControl( control ); 104 } 105 106 return jndiControls; 107 } 108 else 109 { 110 return null; 111 } 112 } 113 114 115 public static Control fromJndiControl( javax.naming.ldap.Control jndiControl ) 116 { 117 Control control = new ControlImpl( jndiControl.getID() ); 118 119 control.setValue( jndiControl.getEncodedValue() ); 120 121 return control; 122 } 123 124 125 public static Control[] fromJndiControls( javax.naming.ldap.Control... jndiControls ) 126 { 127 if ( jndiControls != null ) 128 { 129 Control[] controls = new Control[jndiControls.length]; 130 int i = 0; 131 132 for ( javax.naming.ldap.Control jndiControl : jndiControls ) 133 { 134 controls[i++] = fromJndiControl( jndiControl ); 135 } 136 137 return controls; 138 } 139 else 140 { 141 return null; 142 } 143 } 144 145 146 public static void wrap( Throwable t ) throws NamingException 147 { 148 if ( t instanceof NamingException ) 149 { 150 throw ( NamingException ) t; 151 } 152 153 NamingException ne = null; 154 155 if ( t instanceof LdapAffectMultipleDsaException ) 156 { 157 ne = new NamingException( t.getLocalizedMessage() ); 158 } 159 else if ( t instanceof LdapAliasDereferencingException ) 160 { 161 ne = new NamingException( t.getLocalizedMessage() ); 162 } 163 else if ( t instanceof LdapAliasException ) 164 { 165 ne = new NamingException( t.getLocalizedMessage() ); 166 } 167 else if ( t instanceof LdapAttributeInUseException ) 168 { 169 ne = new AttributeInUseException( t.getLocalizedMessage() ); 170 } 171 else if ( t instanceof LdapAuthenticationException ) 172 { 173 ne = new AuthenticationException( t.getLocalizedMessage() ); 174 } 175 else if ( t instanceof LdapAuthenticationNotSupportedException ) 176 { 177 ne = new AuthenticationNotSupportedException( t.getLocalizedMessage() ); 178 } 179 else if ( t instanceof LdapContextNotEmptyException ) 180 { 181 ne = new ContextNotEmptyException( t.getLocalizedMessage() ); 182 } 183 else if ( t instanceof LdapEntryAlreadyExistsException ) 184 { 185 ne = new NameAlreadyBoundException( t.getLocalizedMessage() ); 186 } 187 else if ( t instanceof LdapInvalidAttributeTypeException ) 188 { 189 ne = new InvalidAttributeIdentifierException( t.getLocalizedMessage() ); 190 } 191 else if ( t instanceof LdapInvalidAttributeValueException ) 192 { 193 ne = new InvalidAttributeValueException( t.getLocalizedMessage() ); 194 } 195 else if ( t instanceof LdapInvalidDnException ) 196 { 197 ne = new InvalidNameException( t.getLocalizedMessage() ); 198 } 199 else if ( t instanceof LdapInvalidSearchFilterException ) 200 { 201 ne = new InvalidSearchFilterException( t.getLocalizedMessage() ); 202 } 203 else if ( t instanceof LdapLoopDetectedException ) 204 { 205 ne = new NamingException( t.getLocalizedMessage() ); 206 } 207 else if ( t instanceof LdapNoPermissionException ) 208 { 209 ne = new NoPermissionException( t.getLocalizedMessage() ); 210 } 211 else if ( t instanceof LdapNoSuchAttributeException ) 212 { 213 ne = new NoSuchAttributeException( t.getLocalizedMessage() ); 214 } 215 else if ( t instanceof LdapNoSuchObjectException ) 216 { 217 ne = new NameNotFoundException( t.getLocalizedMessage() ); 218 } 219 else if ( t instanceof LdapOperationErrorException ) 220 { 221 ne = new NamingException( t.getLocalizedMessage() ); 222 } 223 else if ( t instanceof LdapOtherException ) 224 { 225 ne = new NamingException( t.getLocalizedMessage() ); 226 } 227 else if ( t instanceof LdapProtocolErrorException ) 228 { 229 ne = new CommunicationException( t.getLocalizedMessage() ); 230 } 231 else if ( t instanceof LdapReferralException ) 232 { 233 ne = new WrappedReferralException( ( LdapReferralException ) t ); 234 } 235 else if ( t instanceof LdapPartialResultException ) 236 { 237 ne = new WrappedPartialResultException( ( LdapPartialResultException ) t ); 238 } 239 else if ( t instanceof LdapSchemaViolationException ) 240 { 241 ne = new SchemaViolationException( t.getLocalizedMessage() ); 242 } 243 else if ( t instanceof LdapServiceUnavailableException ) 244 { 245 ne = new ServiceUnavailableException( t.getLocalizedMessage() ); 246 } 247 else if ( t instanceof LdapTimeLimitExceededException ) 248 { 249 ne = new TimeLimitExceededException( t.getLocalizedMessage() ); 250 } 251 else if ( t instanceof LdapUnwillingToPerformException ) 252 { 253 ne = new OperationNotSupportedException( t.getLocalizedMessage() ); 254 } 255 else 256 { 257 ne = new NamingException( t.getLocalizedMessage() ); 258 } 259 260 ne.setRootCause( t ); 261 262 throw ne; 263 } 264 } 265 266 // a ReferralException around the LdapReferralException to be used in tests 267 class WrappedReferralException extends ReferralException 268 { 269 private static final long serialVersionUID = 1L; 270 271 private LdapReferralException lre; 272 273 public WrappedReferralException( LdapReferralException lre ) 274 { 275 this.lre = lre; 276 } 277 278 @Override 279 public boolean skipReferral() 280 { 281 return lre.skipReferral(); 282 } 283 284 285 @Override 286 public void retryReferral() 287 { 288 lre.retryReferral(); 289 } 290 291 292 @Override 293 public Object getReferralInfo() 294 { 295 return lre.getReferralInfo(); 296 } 297 298 299 @Override 300 public Context getReferralContext( Hashtable<?, ?> env ) throws NamingException 301 { 302 return lre.getReferralContext( env ); 303 } 304 305 306 @Override 307 public Context getReferralContext() throws NamingException 308 { 309 return lre.getReferralContext(); 310 } 311 312 @Override 313 public Name getRemainingName() 314 { 315 return DN.toName( lre.getRemainingDn() ); 316 } 317 318 319 @Override 320 public Object getResolvedObj() 321 { 322 return lre.getResolvedObject(); 323 } 324 325 326 @Override 327 public Name getResolvedName() 328 { 329 // TODO Auto-generated method stub 330 return DN.toName( lre.getResolvedDn() ); 331 } 332 } 333 334 335 // a PartialResultException around the LdapPartialResultException to be used in tests 336 class WrappedPartialResultException extends PartialResultException 337 { 338 private static final long serialVersionUID = 1L; 339 340 private LdapPartialResultException lpre; 341 342 public WrappedPartialResultException( LdapPartialResultException lpre ) 343 { 344 this.lpre = lpre; 345 } 346 347 @Override 348 public Name getRemainingName() 349 { 350 return DN.toName( lpre.getRemainingDn() ); 351 } 352 353 354 @Override 355 public Object getResolvedObj() 356 { 357 return lpre.getResolvedObject(); 358 } 359 360 361 @Override 362 public Name getResolvedName() 363 { 364 return DN.toName( lpre.getResolvedDn() ); 365 } 366 }