Previous | Next | Trail Map | Tips for LDAP Users | Referrals

Automatically Following Referrals

If you set the Context.REFERRAL(in the API reference documentation) environment property to "follow", then referrals will be followed automatically. Here's an example.
// Set the referral property to "follow" referrals automatically
env.put(Context.REFERRAL, "follow");

// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Set the controls for performing a subtree search
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

// Perform the search
NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);
Running this example produces the following results.
>>>
>>>ou=All
>>>ldap://localhost:389/ou=People, o=JNDITutorial
>>>ldap://localhost:389/cn=Ted Geisel, ou=People, o=JNDITutorial
>>>ldap://localhost:389/cn=Jon Ruiz, ou=People, o=JNDITutorial
...
>>>ldap://localhost:389/ou=People, o=JNDITutorial
>>>ldap://localhost:389/cn=Ted Geisel, ou=People, o=JNDITutorial
>>>ldap://localhost:389/cn=Jon Ruiz, ou=People, o=JNDITutorial
...
>>>ldap://localhost:389/ou=NewHires,o=JNDITutorial
>>>ldap://localhost:389/cn=S. User,ou=NewHires,o=JNDITutorial
>>>ldap://localhost:389/cn=C. User,ou=NewHires,o=JNDITutorial
The example follows three referrals: "ou=People", "ou=People, ou=All", and "ou=NewHires, ou=All".

Notice that the names of the referred entries are URLs instead of names that are relative to the context being searched. If you examine the SearchResult(in the API reference documentation) object for each of these referred entries and invoke isRelative()(in the API reference documentation) on them, the method will return false. This indicates that the name is not relative and that it should be resolved relative to the initial context.


Previous | Next | Trail Map | Tips for LDAP Users | Referrals