Previous | Next | Trail Map | Beyond the Basics | URLs

URLs as Names Returned by Naming Enumerations

You can enumerate the contents of a context by using list()(in the API reference documentation), listBindings()(in the API reference documentation), or search()(in the API reference documentation). When you invoke one of these, you get back a NamingEnumeration(in the API reference documentation). Each item of the enumeration is an instance of NameClassPair(in the API reference documentation) 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()(in the API reference documentation). 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(in the API reference documentation) 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(in the API reference documentation) methods, as described in the previous section.

To determine whether the name returned by getName() is relative, you use NameClassPair.isRelative()(in the API reference documentation).

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()(in the API reference documentation). You could have been done this much more easily by using the SearchControls(in the API reference documentation) 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.

// 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();

// Perform the search on the target context
NamingEnumeration enum ="", "(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 (enum.hasMore()) {
    item = (NameClassPair);
    System.out.println(">>>>>" + item.getName() + " ");
    if (item.isRelative()) {
        attrs = targetCtx.getAttributes(item.getName(), attrIds);
    } else {
        attrs = initCtx.getAttributes(item.getName(), attrIds);
Here is the output from running this program.
>>>>>ldap://localhost:389/cn=Scott Seligman, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 5252}
>>>>>ldap://localhost:389/cn=Samuel Clemens, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 0186}
>>>>>ldap://localhost:389/cn=Spuds Mackenzie, ou=People, o=JNDITutorial
{telephonenumber=telephonenumber: +1 408 555 4420}
>>>>>ldap://localhost:389/cn=S. User,ou=NewHires,o=JNDITutorial
No attributes

See also the Referrals (in the Tips for LDAP Users trail) lesson and Dereferencing Aliases (in the Tips for LDAP Users trail) section for more examples and descriptions.

Previous | Next | Trail Map | Beyond the Basics | URLs