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

Automatically Following Referrals

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

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

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

// Perform search
NamingEnumeration answer = ctx.search("", "(objectclass=*)", ctls);
If you run this example, it 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 followed three referrals: "ou=People", "ou=People, ou=All", and "ou=NewHires, ou=All".

Notice the names of the referred entries. They 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 the isRelative()(in the API reference documentation) method 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