Referrals

 


Automatically Following Referrals

If you set the Context.REFERRAL 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=JNDIDocs
>>>ldap://localhost:389/cn=Ted Geisel, ou=People, o=JNDIDocs
>>>ldap://localhost:389/cn=Jon Ruiz, ou=People, o=JNDIDocs
...
>>>ldap://localhost:389/ou=People, o=JNDIDocs
>>>ldap://localhost:389/cn=Ted Geisel, ou=People, o=JNDIDocs
>>>ldap://localhost:389/cn=Jon Ruiz, ou=People, o=JNDIDocs
...
>>>ldap://localhost:389/ou=NewHires,o=JNDIDocs
>>>ldap://localhost:389/cn=S. User,ou=NewHires,o=JNDIDocs
>>>ldap://localhost:389/cn=C. User,ou=NewHires,o=JNDIDocs
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 object for each of these referred entries and invoke isRelative() 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.