25 26 package com.sun.jndi.url.ldap; 27 28 import javax.naming.spi.ResolveResult; 29 import javax.naming.*; 30 import javax.naming.directory.*; 31 import java.util.Hashtable; 32 import java.util.StringTokenizer; 33 import com.sun.jndi.ldap.LdapURL; 34 35 /** 36 * An LDAP URL context. 37 * 38 * @author Rosanna Lee 39 * @author Scott Seligman 40 */ 41 42 final public class ldapURLContext 43 extends com.sun.jndi.toolkit.url.GenericURLDirContext { 44 45 ldapURLContext(Hashtable env) { 46 super(env); 47 } 48 49 /** 50 * Resolves 'name' into a target context with remaining name. 51 * It only resolves the hostname/port number. The remaining name 52 * contains the root DN. 53 * 54 * For example, with a LDAP URL "ldap://localhost:389/o=widget,c=us", 55 * this method resolves "ldap://localhost:389/" to the root LDAP 56 * context on the server 'localhost' on port 389, 57 * and returns as the remaining name "o=widget, c=us". 58 */ 59 protected ResolveResult getRootURLContext(String name, Hashtable env) 60 throws NamingException { 61 return ldapURLContextFactory.getUsingURLIgnoreRootDN(name, env); 62 } 63 64 /** 65 * Return the suffix of an ldap url. 66 * prefix parameter is ignored. 67 */ 68 protected Name getURLSuffix(String prefix, String url) 69 throws NamingException { 70 71 LdapURL ldapUrl = new LdapURL(url); 72 String dn = (ldapUrl.getDN() != null? ldapUrl.getDN() : ""); 73 74 // Represent DN as empty or single-component composite name. 75 CompositeName remaining = new CompositeName(); 76 if (!"".equals(dn)) { 77 // if nonempty, add component 78 remaining.add(dn); 79 } 154 public void rename(String oldName, String newName) throws NamingException { 155 if (LdapURL.hasQueryComponents(oldName)) { 156 throw new InvalidNameException(oldName); 157 } else if (LdapURL.hasQueryComponents(newName)) { 158 throw new InvalidNameException(newName); 159 } else { 160 super.rename(oldName, newName); 161 } 162 } 163 164 public void rename(Name oldName, Name newName) throws NamingException { 165 if (LdapURL.hasQueryComponents(oldName.get(0))) { 166 throw new InvalidNameException(oldName.toString()); 167 } else if (LdapURL.hasQueryComponents(newName.get(0))) { 168 throw new InvalidNameException(newName.toString()); 169 } else { 170 super.rename(oldName, newName); 171 } 172 } 173 174 public NamingEnumeration list(String name) throws NamingException { 175 if (LdapURL.hasQueryComponents(name)) { 176 throw new InvalidNameException(name); 177 } else { 178 return super.list(name); 179 } 180 } 181 182 public NamingEnumeration list(Name name) throws NamingException { 183 if (LdapURL.hasQueryComponents(name.get(0))) { 184 throw new InvalidNameException(name.toString()); 185 } else { 186 return super.list(name); 187 } 188 } 189 190 public NamingEnumeration listBindings(String name) throws NamingException { 191 if (LdapURL.hasQueryComponents(name)) { 192 throw new InvalidNameException(name); 193 } else { 194 return super.listBindings(name); 195 } 196 } 197 198 public NamingEnumeration listBindings(Name name) throws NamingException { 199 if (LdapURL.hasQueryComponents(name.get(0))) { 200 throw new InvalidNameException(name.toString()); 201 } else { 202 return super.listBindings(name); 203 } 204 } 205 206 public void destroySubcontext(String name) throws NamingException { 207 if (LdapURL.hasQueryComponents(name)) { 208 throw new InvalidNameException(name); 209 } else { 210 super.destroySubcontext(name); 211 } 212 } 213 214 public void destroySubcontext(Name name) throws NamingException { 215 if (LdapURL.hasQueryComponents(name.get(0))) { 216 throw new InvalidNameException(name.toString()); 217 } else { 218 super.destroySubcontext(name); 430 431 public DirContext getSchemaClassDefinition(String name) 432 throws NamingException { 433 if (LdapURL.hasQueryComponents(name)) { 434 throw new InvalidNameException(name); 435 } else { 436 return super.getSchemaClassDefinition(name); 437 } 438 } 439 440 public DirContext getSchemaClassDefinition(Name name) 441 throws NamingException { 442 if (LdapURL.hasQueryComponents(name.get(0))) { 443 throw new InvalidNameException(name.toString()); 444 } else { 445 return super.getSchemaClassDefinition(name); 446 } 447 } 448 449 // divert the search operation when the LDAP URL has query components 450 public NamingEnumeration search(String name, 451 Attributes matchingAttributes) 452 throws NamingException { 453 454 if (LdapURL.hasQueryComponents(name)) { 455 return searchUsingURL(name); 456 } else { 457 return super.search(name, matchingAttributes); 458 } 459 } 460 461 // divert the search operation when name has a single component 462 public NamingEnumeration search(Name name, 463 Attributes matchingAttributes) 464 throws NamingException { 465 if (name.size() == 1) { 466 return search(name.get(0), matchingAttributes); 467 } else if (LdapURL.hasQueryComponents(name.get(0))) { 468 throw new InvalidNameException(name.toString()); 469 } else { 470 return super.search(name, matchingAttributes); 471 } 472 } 473 474 // divert the search operation when the LDAP URL has query components 475 public NamingEnumeration search(String name, 476 Attributes matchingAttributes, 477 String[] attributesToReturn) 478 throws NamingException { 479 480 if (LdapURL.hasQueryComponents(name)) { 481 return searchUsingURL(name); 482 } else { 483 return super.search(name, matchingAttributes, attributesToReturn); 484 } 485 } 486 487 // divert the search operation when name has a single component 488 public NamingEnumeration search(Name name, 489 Attributes matchingAttributes, 490 String[] attributesToReturn) 491 throws NamingException { 492 493 if (name.size() == 1) { 494 return search(name.get(0), matchingAttributes, attributesToReturn); 495 } else if (LdapURL.hasQueryComponents(name.get(0))) { 496 throw new InvalidNameException(name.toString()); 497 } else { 498 return super.search(name, matchingAttributes, attributesToReturn); 499 } 500 } 501 502 // divert the search operation when the LDAP URL has query components 503 public NamingEnumeration search(String name, 504 String filter, 505 SearchControls cons) 506 throws NamingException { 507 508 if (LdapURL.hasQueryComponents(name)) { 509 return searchUsingURL(name); 510 } else { 511 return super.search(name, filter, cons); 512 } 513 } 514 515 // divert the search operation when name has a single component 516 public NamingEnumeration search(Name name, 517 String filter, 518 SearchControls cons) 519 throws NamingException { 520 521 if (name.size() == 1) { 522 return search(name.get(0), filter, cons); 523 } else if (LdapURL.hasQueryComponents(name.get(0))) { 524 throw new InvalidNameException(name.toString()); 525 } else { 526 return super.search(name, filter, cons); 527 } 528 } 529 530 // divert the search operation when the LDAP URL has query components 531 public NamingEnumeration search(String name, 532 String filterExpr, 533 Object[] filterArgs, 534 SearchControls cons) 535 throws NamingException { 536 537 if (LdapURL.hasQueryComponents(name)) { 538 return searchUsingURL(name); 539 } else { 540 return super.search(name, filterExpr, filterArgs, cons); 541 } 542 } 543 544 // divert the search operation when name has a single component 545 public NamingEnumeration search(Name name, 546 String filterExpr, 547 Object[] filterArgs, 548 SearchControls cons) 549 throws NamingException { 550 551 if (name.size() == 1) { 552 return search(name.get(0), filterExpr, filterArgs, cons); 553 } else if (LdapURL.hasQueryComponents(name.get(0))) { 554 throw new InvalidNameException(name.toString()); 555 } else { 556 return super.search(name, filterExpr, filterArgs, cons); 557 } 558 } 559 560 // Search using the LDAP URL in name. 561 // LDAP URL query components override the search argments. 562 private NamingEnumeration searchUsingURL(String name) 563 throws NamingException { 564 565 LdapURL url = new LdapURL(name); 566 567 ResolveResult res = getRootURLContext(name, myEnv); 568 DirContext ctx = (DirContext)res.getResolvedObj(); 569 try { 570 return ctx.search(res.getRemainingName(), 571 setFilterUsingURL(url), 572 setSearchControlsUsingURL(url)); 573 } finally { 574 ctx.close(); 575 } 576 } 577 578 /* 579 * Initialize a String filter using the LDAP URL filter component. 580 * If filter is not present in the URL it is initialized to its default 581 * value as specified in RFC-2255. 582 */ | 25 26 package com.sun.jndi.url.ldap; 27 28 import javax.naming.spi.ResolveResult; 29 import javax.naming.*; 30 import javax.naming.directory.*; 31 import java.util.Hashtable; 32 import java.util.StringTokenizer; 33 import com.sun.jndi.ldap.LdapURL; 34 35 /** 36 * An LDAP URL context. 37 * 38 * @author Rosanna Lee 39 * @author Scott Seligman 40 */ 41 42 final public class ldapURLContext 43 extends com.sun.jndi.toolkit.url.GenericURLDirContext { 44 45 ldapURLContext(Hashtable<?,?> env) { 46 super(env); 47 } 48 49 /** 50 * Resolves 'name' into a target context with remaining name. 51 * It only resolves the hostname/port number. The remaining name 52 * contains the root DN. 53 * 54 * For example, with a LDAP URL "ldap://localhost:389/o=widget,c=us", 55 * this method resolves "ldap://localhost:389/" to the root LDAP 56 * context on the server 'localhost' on port 389, 57 * and returns as the remaining name "o=widget, c=us". 58 */ 59 protected ResolveResult getRootURLContext(String name, Hashtable<?,?> env) 60 throws NamingException { 61 return ldapURLContextFactory.getUsingURLIgnoreRootDN(name, env); 62 } 63 64 /** 65 * Return the suffix of an ldap url. 66 * prefix parameter is ignored. 67 */ 68 protected Name getURLSuffix(String prefix, String url) 69 throws NamingException { 70 71 LdapURL ldapUrl = new LdapURL(url); 72 String dn = (ldapUrl.getDN() != null? ldapUrl.getDN() : ""); 73 74 // Represent DN as empty or single-component composite name. 75 CompositeName remaining = new CompositeName(); 76 if (!"".equals(dn)) { 77 // if nonempty, add component 78 remaining.add(dn); 79 } 154 public void rename(String oldName, String newName) throws NamingException { 155 if (LdapURL.hasQueryComponents(oldName)) { 156 throw new InvalidNameException(oldName); 157 } else if (LdapURL.hasQueryComponents(newName)) { 158 throw new InvalidNameException(newName); 159 } else { 160 super.rename(oldName, newName); 161 } 162 } 163 164 public void rename(Name oldName, Name newName) throws NamingException { 165 if (LdapURL.hasQueryComponents(oldName.get(0))) { 166 throw new InvalidNameException(oldName.toString()); 167 } else if (LdapURL.hasQueryComponents(newName.get(0))) { 168 throw new InvalidNameException(newName.toString()); 169 } else { 170 super.rename(oldName, newName); 171 } 172 } 173 174 public NamingEnumeration<NameClassPair> list(String name) 175 throws NamingException { 176 if (LdapURL.hasQueryComponents(name)) { 177 throw new InvalidNameException(name); 178 } else { 179 return super.list(name); 180 } 181 } 182 183 public NamingEnumeration<NameClassPair> list(Name name) 184 throws NamingException { 185 if (LdapURL.hasQueryComponents(name.get(0))) { 186 throw new InvalidNameException(name.toString()); 187 } else { 188 return super.list(name); 189 } 190 } 191 192 public NamingEnumeration<Binding> listBindings(String name) 193 throws NamingException { 194 if (LdapURL.hasQueryComponents(name)) { 195 throw new InvalidNameException(name); 196 } else { 197 return super.listBindings(name); 198 } 199 } 200 201 public NamingEnumeration<Binding> listBindings(Name name) 202 throws NamingException { 203 if (LdapURL.hasQueryComponents(name.get(0))) { 204 throw new InvalidNameException(name.toString()); 205 } else { 206 return super.listBindings(name); 207 } 208 } 209 210 public void destroySubcontext(String name) throws NamingException { 211 if (LdapURL.hasQueryComponents(name)) { 212 throw new InvalidNameException(name); 213 } else { 214 super.destroySubcontext(name); 215 } 216 } 217 218 public void destroySubcontext(Name name) throws NamingException { 219 if (LdapURL.hasQueryComponents(name.get(0))) { 220 throw new InvalidNameException(name.toString()); 221 } else { 222 super.destroySubcontext(name); 434 435 public DirContext getSchemaClassDefinition(String name) 436 throws NamingException { 437 if (LdapURL.hasQueryComponents(name)) { 438 throw new InvalidNameException(name); 439 } else { 440 return super.getSchemaClassDefinition(name); 441 } 442 } 443 444 public DirContext getSchemaClassDefinition(Name name) 445 throws NamingException { 446 if (LdapURL.hasQueryComponents(name.get(0))) { 447 throw new InvalidNameException(name.toString()); 448 } else { 449 return super.getSchemaClassDefinition(name); 450 } 451 } 452 453 // divert the search operation when the LDAP URL has query components 454 public NamingEnumeration<SearchResult> search(String name, 455 Attributes matchingAttributes) 456 throws NamingException { 457 458 if (LdapURL.hasQueryComponents(name)) { 459 return searchUsingURL(name); 460 } else { 461 return super.search(name, matchingAttributes); 462 } 463 } 464 465 // divert the search operation when name has a single component 466 public NamingEnumeration<SearchResult> search(Name name, 467 Attributes matchingAttributes) 468 throws NamingException { 469 if (name.size() == 1) { 470 return search(name.get(0), matchingAttributes); 471 } else if (LdapURL.hasQueryComponents(name.get(0))) { 472 throw new InvalidNameException(name.toString()); 473 } else { 474 return super.search(name, matchingAttributes); 475 } 476 } 477 478 // divert the search operation when the LDAP URL has query components 479 public NamingEnumeration<SearchResult> search(String name, 480 Attributes matchingAttributes, 481 String[] attributesToReturn) 482 throws NamingException { 483 484 if (LdapURL.hasQueryComponents(name)) { 485 return searchUsingURL(name); 486 } else { 487 return super.search(name, matchingAttributes, attributesToReturn); 488 } 489 } 490 491 // divert the search operation when name has a single component 492 public NamingEnumeration<SearchResult> search(Name name, 493 Attributes matchingAttributes, 494 String[] attributesToReturn) 495 throws NamingException { 496 497 if (name.size() == 1) { 498 return search(name.get(0), matchingAttributes, attributesToReturn); 499 } else if (LdapURL.hasQueryComponents(name.get(0))) { 500 throw new InvalidNameException(name.toString()); 501 } else { 502 return super.search(name, matchingAttributes, attributesToReturn); 503 } 504 } 505 506 // divert the search operation when the LDAP URL has query components 507 public NamingEnumeration<SearchResult> search(String name, 508 String filter, 509 SearchControls cons) 510 throws NamingException { 511 512 if (LdapURL.hasQueryComponents(name)) { 513 return searchUsingURL(name); 514 } else { 515 return super.search(name, filter, cons); 516 } 517 } 518 519 // divert the search operation when name has a single component 520 public NamingEnumeration<SearchResult> search(Name name, 521 String filter, 522 SearchControls cons) 523 throws NamingException { 524 525 if (name.size() == 1) { 526 return search(name.get(0), filter, cons); 527 } else if (LdapURL.hasQueryComponents(name.get(0))) { 528 throw new InvalidNameException(name.toString()); 529 } else { 530 return super.search(name, filter, cons); 531 } 532 } 533 534 // divert the search operation when the LDAP URL has query components 535 public NamingEnumeration<SearchResult> search(String name, 536 String filterExpr, 537 Object[] filterArgs, 538 SearchControls cons) 539 throws NamingException { 540 541 if (LdapURL.hasQueryComponents(name)) { 542 return searchUsingURL(name); 543 } else { 544 return super.search(name, filterExpr, filterArgs, cons); 545 } 546 } 547 548 // divert the search operation when name has a single component 549 public NamingEnumeration<SearchResult> search(Name name, 550 String filterExpr, 551 Object[] filterArgs, 552 SearchControls cons) 553 throws NamingException { 554 555 if (name.size() == 1) { 556 return search(name.get(0), filterExpr, filterArgs, cons); 557 } else if (LdapURL.hasQueryComponents(name.get(0))) { 558 throw new InvalidNameException(name.toString()); 559 } else { 560 return super.search(name, filterExpr, filterArgs, cons); 561 } 562 } 563 564 // Search using the LDAP URL in name. 565 // LDAP URL query components override the search argments. 566 private NamingEnumeration<SearchResult> searchUsingURL(String name) 567 throws NamingException { 568 569 LdapURL url = new LdapURL(name); 570 571 ResolveResult res = getRootURLContext(name, myEnv); 572 DirContext ctx = (DirContext)res.getResolvedObj(); 573 try { 574 return ctx.search(res.getRemainingName(), 575 setFilterUsingURL(url), 576 setSearchControlsUsingURL(url)); 577 } finally { 578 ctx.close(); 579 } 580 } 581 582 /* 583 * Initialize a String filter using the LDAP URL filter component. 584 * If filter is not present in the URL it is initialized to its default 585 * value as specified in RFC-2255. 586 */ |