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


RFC 2255 describes the syntactic format of LDAP v3 URLs. The format contains all of the elements necessary to specify an LDAP "search" operation, with provisions for supporting future version 3 extensions:
Authentication information may be specified in the extensions portion of the URL. See the RFC for a complete description of the format.

URLs play a role in several places in the JNDI. This section describes how LDAP URLs can be used with the LDAP service provider.

URL as a Name to the Initial Context

If you pass an LDAP URL to the methods in InitialContext(in the API reference documentation) or InitialDirContext(in the API reference documentation), then the JNDI will look for a context implementation (called a URL context implementation) to process the LDAP URL.

Here is an example that performs a search from the initial context, using an LDAP URL as the name argument.

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

// Perform the search by using the URL
NamingEnumeration answer =
     "ldap://localhost:389/ou=People,o=JNDITutorial", "(sn=Geisel)", null);
This example produces the following output.
>>>cn=Ted Geisel
{sn=sn: Geisel, 
 objectclass=objectclass: top, person, organizationalPerson, inetOrgPerson, 
 jpegphoto=jpegphoto: [B@1dacd78a, 
 facsimiletelephonenumber=facsimiletelephonenumber: +1 408 555 2329, 
 telephonenumber=telephonenumber: +1 408 555 5252, 
 cn=cn: Ted Geisel}

You might have noticed that you did not need to set up any environment properties to perform this search. This is because the JNDI automatically searches for the URL context implementation. If the URL context implementation is not found, it will use only the implementation specified by the environment properties (if any). For an LDAP URL, it looks for a class with the name ldapURLContextFactory from package locations specified by the environment property Context.URL_PKG_PREFIXES(in the API reference documentation) ("java.naming.factory.url.pkgs"). This property contains a list of package prefixes separated by colon characters (":"). If no class with the right name is found in these packages, then the package com.sun.jndi.url.ldap is searched.

Query Components in a URL

With the exception of the the API reference documentation) methods, when an LDAP URL is passed as a name to the initial context, the URL should not contain any query ('?') components. If it does, then an InvalidNameException(in the API reference documentation) is thrown by the LDAP service provider.

For the search() methods, if a URL contains query components, then all other arguments (including the filter and SearchControls(in the API reference documentation)) are ignored. The query components of the URL and its defaults are used instead. For example, if an LDAP URL containing a scope component is supplied, then that scope overrides any scope setting that is passed in an argument. If the URL contains other query components but not the scope, then the LDAP URL's default scope ("base object") is used.

Here is an example that performs a subtree search by using a filter of "(sn=Geisel)".

// Perform the search by using URL
NamingEnumeration answer =
	"" /* ignored*/, 
        null /* ignored */);

Note: Version 1.2 of Sun's LDAP provider does not treat query components properly.

URL for Configuring Service Providers

To configure an LDAP service provider, you typically supply an LDAP URL in the Context.PROVIDER_URL(in the API reference documentation) ("java.naming.provider.url") environment property. The LDAP service provider uses this URL to configure its connection to the directory server. Only the host, port, and dn parts of the URL are relevant in this setting. Supplying other parts of the URL results in a ConfigurationException(in the API reference documentation).

URL for Specifying Referrals

An LDAP referral contains a list of one or more URLs. To process an LDAP referral, the service provider uses the information in these URLs to create connections to the LDAP servers to which they refer. Multiple LDAP URLs in a single referral are treated as alternatives, each followed until one succeeds. The complete URL (including any query components) is used.

You set up referrals by creating referral entries in the directory that contain the "REF" attribute. This attribute contains one or more referral URLs (usually LDAP URLs). See the Referrals (in the Tips for LDAP Users trail) lesson for details on referrals.

URL as a Name in NamingEnumeration

When you perform a Context.list()(in the API reference documentation), Context.listBindings()(in the API reference documentation), or the API reference documentation), you get back a NamingEnumeration(in the API reference documentation). Each item in this enumeration is an instance or subclass of NameClassPair(in the API reference documentation). When the name of the item ( NameClassPair.getName()(in the API reference documentation)) is not relative to the target context, the name is returned as a URL. You can use NameClassPair.isRelative()(in the API reference documentation) to check whether the name is relative. One of main reason why the name might not be relative is because a referral was followed, in which case, the name of the object is that in the referred namespace and not the one at which the operation was initiated. See the URLs (in the Beyond the Basics trail) lesson for more details and an example.

URL as an Argument to getObjectInstance()

When an LDAP namespace is federated under another namespace (such as DNS), the information that is stored in the superior namespace might be an LDAP URL. In such a scenario, a lookup()/list()/search() method invocation in the superior namespace will return a Reference (in the API reference documentation) that contains the LDAP URL for the LDAP namespace. The service provider for the superior namespace will then pass the Reference to NamingManager.getObjectInstance()(in the API reference documentation) or DirectoryManager.getObjectInstance()(in the API reference documentation) to create an instance of an LDAP context. See the Beyond the Basics (in the Beyond the Basics trail) trail for details on federation.

Miscellaneous: End of Lesson

What's next? Now you can:

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