1 17 package org.apache.ldap.server.jndi; 18 19 20 import org.apache.ldap.common.exception.LdapNoPermissionException; 21 import org.apache.ldap.common.filter.PresenceNode; 22 import org.apache.ldap.common.message.LockableAttributesImpl; 23 import org.apache.ldap.common.name.LdapName; 24 import org.apache.ldap.common.util.NamespaceTools; 25 import org.apache.ldap.server.PartitionNexus; 26 import org.apache.ldap.server.authn.AuthenticationService; 27 import org.apache.ldap.server.authn.LdapPrincipal; 28 29 import javax.naming.*; 30 import javax.naming.directory.Attribute ; 31 import javax.naming.directory.Attributes ; 32 import javax.naming.directory.DirContext ; 33 import javax.naming.directory.SearchControls ; 34 import javax.naming.ldap.Control ; 35 import javax.naming.spi.DirStateFactory ; 36 import javax.naming.spi.DirectoryManager ; 37 import java.io.Serializable ; 38 import java.util.Hashtable ; 39 40 41 47 public abstract class ServerContext implements Context 48 { 49 50 public static final String DELETE_OLD_RDN_PROP = "java.naming.ldap.deleteRDN"; 51 52 53 private final PartitionNexus nexusProxy; 54 55 56 private final Hashtable env; 57 58 59 private final LdapName dn; 60 61 62 private LdapPrincipal principal; 63 64 68 69 83 protected ServerContext( PartitionNexus nexusProxy, Hashtable env ) throws NamingException 84 { 85 String url; 86 87 this.nexusProxy = nexusProxy; 89 90 this.env = ( Hashtable ) env.clone(); 91 92 96 if ( ! env.containsKey( Context.PROVIDER_URL ) ) 97 { 98 String msg = "Expected property " + Context.PROVIDER_URL; 99 100 msg += " but could not find it in env!"; 101 102 throw new ConfigurationException( msg ); 103 } 104 105 url = ( String ) env.get( Context.PROVIDER_URL ); 106 107 if ( url == null ) 108 { 109 String msg = "Expected value for property " + Context.PROVIDER_URL; 110 111 msg += " but it was set to null in env!"; 112 113 throw new ConfigurationException( msg ); 114 } 115 116 dn = new LdapName( url ); 117 118 if ( ! nexusProxy.hasEntry( dn ) ) 119 { 120 throw new NameNotFoundException( dn + " does not exist" ); 121 } 122 } 123 124 125 135 protected ServerContext( LdapPrincipal principal, PartitionNexus nexusProxy, Hashtable env, Name dn ) 136 { 137 this.dn = ( LdapName ) dn.clone(); 138 139 this.env = ( Hashtable ) env.clone(); 140 141 this.env.put( PROVIDER_URL, dn.toString() ); 142 143 this.nexusProxy = nexusProxy; 144 145 this.principal = principal; 146 } 147 148 149 153 154 157 public LdapPrincipal getPrincipal() 158 { 159 return principal; 160 } 161 162 163 170 public void setPrincipal( AuthenticationService.TrustedPrincipalWrapper wrapper ) 171 { 172 this.principal = wrapper.getPrincipal(); 173 } 174 175 176 180 181 186 protected PartitionNexus getNexusProxy() 187 { 188 return nexusProxy ; 189 } 190 191 192 197 protected Name getDn() 198 { 199 return dn; 200 } 201 202 203 207 208 211 public void close() throws NamingException 212 { 213 } 215 216 217 220 public String getNameInNamespace() throws NamingException 221 { 222 return dn.toString(); 223 } 224 225 226 229 public Hashtable getEnvironment() 230 { 231 return env; 232 } 233 234 235 239 public Object addToEnvironment( String propName, Object propVal ) throws NamingException 240 { 241 return env.put( propName, propVal ); 242 } 243 244 245 248 public Object removeFromEnvironment( String propName ) throws NamingException 249 { 250 return env.remove( propName ); 251 } 252 253 254 257 public Context createSubcontext( String name ) throws NamingException 258 { 259 return createSubcontext( new LdapName( name ) ); 260 } 261 262 263 266 public Context createSubcontext( Name name ) throws NamingException 267 { 268 Attributes attributes = new LockableAttributesImpl(); 269 270 LdapName target = buildTarget( name ); 271 272 String rdn = name.get( name.size() - 1 ); 273 274 String rdnAttribute = NamespaceTools.getRdnAttribute( rdn ); 275 276 String rdnValue = NamespaceTools.getRdnValue( rdn ); 277 278 attributes.put( rdnAttribute, rdnValue ); 279 280 attributes.put( JavaLdapSupport.OBJECTCLASS_ATTR, JavaLdapSupport.JCONTAINER_ATTR ); 281 282 attributes.put( JavaLdapSupport.OBJECTCLASS_ATTR, JavaLdapSupport.TOP_ATTR ); 283 284 291 nexusProxy.add( target.toString(), target, attributes ); 292 293 ServerLdapContext ctx = new ServerLdapContext( principal, nexusProxy, env, target ); 294 295 Control [] controls = ( Control [] ) ( ( ServerLdapContext ) this ).getRequestControls().clone(); 296 297 ctx.setRequestControls( controls ); 298 299 return ctx; 300 } 301 302 303 306 public void destroySubcontext( String name ) throws NamingException 307 { 308 destroySubcontext( new LdapName( name ) ); 309 } 310 311 312 315 public void destroySubcontext( Name name ) throws NamingException 316 { 317 Name target = buildTarget( name ); 318 319 if ( target.size() == 0 ) 320 { 321 throw new LdapNoPermissionException( "can't delete the rootDSE" ); 322 } 323 324 nexusProxy.delete( target ); 325 } 326 327 328 331 public void bind( String name, Object obj ) throws NamingException 332 { 333 bind( new LdapName( name ), obj ); 334 } 335 336 337 340 public void bind( Name name, Object obj ) throws NamingException 341 { 342 DirStateFactory.Result res = DirectoryManager.getStateToBind( obj, name, this, env, null ); 344 345 Attributes outAttrs = res.getAttributes(); 346 347 if ( outAttrs != null ) 348 { 349 Name target = buildTarget( name ); 350 351 nexusProxy.add( target.toString(), target, outAttrs ); 352 353 return; 354 } 355 356 if ( obj instanceof Referenceable ) 358 { 359 obj = ( ( Referenceable ) obj ).getReference(); 360 361 throw new NamingException( "Do not know how to store Referenceables yet!" ); 362 } 363 364 if ( obj instanceof Reference ) 366 { 367 369 throw new NamingException( "Do not know how to store References yet!" ); 370 } 371 else if ( obj instanceof Serializable ) 372 { 373 375 Attributes attributes = new LockableAttributesImpl(); 376 377 if ( outAttrs != null && outAttrs.size() > 0 ) 378 { 379 NamingEnumeration list = outAttrs.getAll(); 380 381 while ( list.hasMore() ) 382 { 383 attributes.put( ( Attribute ) list.next() ); 384 } 385 } 386 387 Name target = buildTarget( name ); 388 389 391 JavaLdapSupport.serialize( attributes, obj ); 392 393 nexusProxy.add( target.toString(), target, attributes ); 394 } 395 else if ( obj instanceof DirContext ) 396 { 397 399 Attributes attributes = ( ( DirContext ) obj ).getAttributes( "" ); 400 401 if ( outAttrs != null && outAttrs.size() > 0 ) 402 { 403 NamingEnumeration list = outAttrs.getAll(); 404 405 while ( list.hasMore() ) 406 { 407 attributes.put( ( Attribute ) list.next() ); 408 } 409 } 410 411 Name target = buildTarget( name ); 412 413 nexusProxy.add( target.toString(), target, attributes ); 414 } 415 else 416 { 417 throw new NamingException( "Can't find a way to bind: " + obj ); 418 } 419 } 420 421 422 425 public void rename( String oldName, String newName ) throws NamingException 426 { 427 rename( new LdapName( oldName ), new LdapName( newName ) ); 428 } 429 430 431 434 public void rename( Name oldName, Name newName ) throws NamingException 435 { 436 Name oldDn = buildTarget( oldName ); 437 438 Name newDn = buildTarget( newName ); 439 440 if ( oldDn.size() == 0 ) 441 { 442 throw new LdapNoPermissionException( "can't rename the rootDSE" ); 443 } 444 445 Name oldBase = oldName.getSuffix( 1 ); 446 447 Name newBase = newName.getSuffix( 1 ); 448 449 String newRdn = newName.get( newName.size() - 1 ); 450 451 String oldRdn = oldName.get( oldName.size() - 1 ); 452 453 boolean delOldRdn = true; 454 455 459 if ( null != env.get( DELETE_OLD_RDN_PROP ) ) 460 { 461 String delOldRdnStr = ( String ) env.get( DELETE_OLD_RDN_PROP ); 462 463 delOldRdn = ! delOldRdnStr.equals( "false" ); 464 465 delOldRdn = delOldRdn || delOldRdnStr.equals( "no" ); 466 467 delOldRdn = delOldRdn || delOldRdnStr.equals( "0" ); 468 } 469 470 478 if ( oldName.size() == newName.size() && oldBase.equals( newBase ) ) 479 { 480 nexusProxy.modifyRn( oldDn, newRdn, delOldRdn ); 481 } 482 else 483 { 484 Name parent = newDn.getSuffix( 1 ); 485 486 if ( newRdn.equalsIgnoreCase( oldRdn ) ) 487 { 488 nexusProxy.move( oldDn, parent ); 489 } 490 else 491 { 492 nexusProxy.move( oldDn, parent, newRdn, delOldRdn ); 493 } 494 } 495 } 496 497 498 501 public void rebind( String name, Object obj ) throws NamingException 502 { 503 rebind( new LdapName( name ), obj ); 504 } 505 506 507 510 public void rebind( Name name, Object obj ) throws NamingException 511 { 512 Name target = buildTarget( name ); 513 514 if ( nexusProxy.hasEntry( target ) ) 515 { 516 nexusProxy.delete( target ); 517 } 518 519 bind( name, obj ); 520 } 521 522 523 526 public void unbind( String name ) throws NamingException 527 { 528 unbind( new LdapName( name ) ); 529 } 530 531 532 535 public void unbind( Name name ) throws NamingException 536 { 537 nexusProxy.delete( buildTarget( name ) ); 538 } 539 540 541 544 public Object lookup( String name ) throws NamingException 545 { 546 return lookup( new LdapName( name ) ); 547 } 548 549 550 553 public Object lookup( Name name ) throws NamingException 554 { 555 Object obj = null; 556 557 LdapName target = buildTarget( name ); 558 559 Attributes attributes = nexusProxy.lookup( target ); 560 561 try 562 { 563 obj = DirectoryManager.getObjectInstance( null, name, this, env, attributes ); 564 } 565 catch ( Exception e ) 566 { 567 throw new NamingException( e.getMessage() ); 568 } 569 570 if ( obj != null ) 571 { 572 return obj; 573 } 574 575 if ( attributes.get( JavaLdapSupport.JCLASSNAME_ATTR ) != null ) 577 { 578 return JavaLdapSupport.deserialize( attributes ); 580 } 581 582 ServerLdapContext ctx = new ServerLdapContext( principal, nexusProxy, env, target ); 584 585 Control [] controls = ( ( ServerLdapContext ) this ).getRequestControls(); 587 588 if ( null != controls ) 589 { 590 ctx.setRequestControls( ( Control [] ) controls.clone() ); 591 } 592 593 return ctx; 594 } 595 596 597 600 public Object lookupLink( String name ) throws NamingException 601 { 602 throw new UnsupportedOperationException (); 603 } 604 605 606 609 public Object lookupLink( Name name ) throws NamingException 610 { 611 throw new UnsupportedOperationException (); 612 } 613 614 615 623 public NameParser getNameParser( String name ) throws NamingException 624 { 625 return LdapName.getNameParser(); 626 } 627 628 629 637 public NameParser getNameParser( Name name ) throws NamingException 638 { 639 return LdapName.getNameParser(); 640 } 641 642 643 646 public NamingEnumeration list( String name ) throws NamingException 647 { 648 return list( new LdapName( name ) ); 649 } 650 651 652 655 public NamingEnumeration list( Name name ) throws NamingException 656 { 657 return nexusProxy.list( buildTarget( name ) ); 658 } 659 660 661 664 public NamingEnumeration listBindings( String name ) throws NamingException 665 { 666 return listBindings( new LdapName( name ) ); 667 } 668 669 670 673 public NamingEnumeration listBindings( Name name ) throws NamingException 674 { 675 Name base = buildTarget( name ); 677 678 PresenceNode filter = new PresenceNode( "objectClass" ); 679 680 SearchControls ctls = new SearchControls (); 681 682 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE ); 683 684 return nexusProxy.search( base , getEnvironment(), filter, ctls ); 685 } 686 687 688 691 public String composeName( String name, String prefix ) throws NamingException 692 { 693 return composeName( new LdapName( name ), new LdapName( prefix ) ).toString(); 694 } 695 696 697 701 public Name composeName( Name name, Name prefix ) throws NamingException 702 { 703 if ( prefix == null || prefix.size() == 0 ) 705 { 706 return name; 707 } 708 709 723 724 Name fqn = buildTarget( name ); 726 727 String head = prefix.get( 0 ); 728 729 while ( fqn.size() > 0 ) 731 { 732 if ( fqn.get( 0 ).equalsIgnoreCase( head ) ) 734 { 735 return fqn; 736 } 737 else { 739 fqn.remove( 0 ); 740 } 741 } 742 743 String msg = "The prefix '" + prefix + "' is not an ancestor of this "; 744 745 msg += "entry '" + dn + "'"; 746 747 throw new NamingException( msg ); 748 } 749 750 751 755 756 765 LdapName buildTarget( Name relativeName ) throws InvalidNameException 766 { 767 LdapName target = ( LdapName ) dn.clone(); 769 770 target.addAll( target.size(), relativeName ); 772 773 return target; 774 } 775 } 776 | Popular Tags |