URLs |
You can enumerate the contents of a context by using list(), listBindings(), or search(). When you invoke one of these, you get back a NamingEnumeration. Each item of the enumeration is an instance of NameClassPair or one of its subclasses. To get the name of the item, that is, the name of the object relative to the target context (the context that you're listing or searching), you use NameClassPair.getName(). The string name returned by this method is a composite name. For example, you should be able to feed this name back into one of the Context methods of the target context.However, sometimes the underlying service or service provider cannot return a name relative to the target context, for example, if the item was retrieved by following a referral or an alias. When a relative name cannot be returned, the service provider returns a URL string. You use this URL string by passing it to the InitialContext methods, as described in the previous section.
To determine whether the name returned by getName() is relative, you use NameClassPair.isRelative().
Following is an example that searches a context for entries whose "cn" attribute starts with the letter "S." It then retrieves the "telephonenumber" attribute of the item by using DirContext.getAttributes(). You could have been done this much more easily by using the SearchControls argument to request the attributes. Here, the attribute retrieval is separated out so that the use of isRelative() can be illustrated.
When the example gets an item containing a URL string as a name (that is, isRelative() returns false), it uses the InitialContext to process the URL string.
Here is the output from running this program.// Set up the environment for creating the initial context Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:489/o=JNDItutorial"); // Enable referrals so that we get some nonrelative names env.put(Context.REFERRAL, "follow"); // Create the initial context DirContext initCtx = new InitialDirContext(env); // Get the target context DirContext targetCtx = (DirContext)initCtx.lookup("ou=All"); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // Perform the search on the target context NamingEnumeration namingEnum = targetCtx.search("", "(cn=S*)", constraints); Attributes attrs; NameClassPair item; String[] attrIds = new String[]{"telephonenumber"}; // For each answer found, get its "telephonenumber" attribute // If relative, resolve it relative to the target context // If not relative, resolve it relative to the initial context while (namingEnum.hasMore()) { item = (NameClassPair)namingEnum.next(); System.out.println(">>>>>" + item.getName() + " "); if (item.isRelative()) { attrs = targetCtx.getAttributes(item.getName(), attrIds); } else { attrs = initCtx.getAttributes(item.getName(), attrIds); } System.out.println(attrs); }>>>>>ldap://localhost:389/cn=Scott%20Seligman,ou=People,o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 5252} >>>>>ldap://localhost:389/cn=Samuel%20Clemens,ou=People,o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 0186} >>>>>ldap://localhost:389/cn=Spuds%20Mackenzie,ou=People,o=JNDITutorial {telephonenumber=telephonenumber: +1 408 555 4420} >>>>>ldap://localhost:389/cn=S.%20User,ou=NewHires,o=JNDITutorial No attributesSee also the Referrals lesson and Dereferencing Aliases section for more examples and descriptions.
URLs |