Mozilla LDAP SDK Programmer's Guide/LDAP URLs With LDAP Java SDK

From MozillaWiki
Jump to: navigation, search

This section describes how to use LDAP URLs to search and retrieve data from the directory.

Getting the Components of an LDAP URL With LDAP Java SDK

To get the individual components of an LDAP URL, pass the URL to the LDAPUrl constructor to create a new LDAPUrl object.

Then, use the following methods:

  • To get an array of the attributes that should be returned in the search results, use the getAttributeArray method.
    To get these attributes as an enumeration, use the getAttributes method.
  • To get the host name of the LDAP server, use the getHost method.
  • To get the port number of the LDAP server, use the getPort method.
  • To get the base DN, use the getDN method.
  • To get the scope of the search, use the getScope method.
  • To get the search filter, use the getFilter method.

Processing an LDAP URL With LDAP Java SDK

To process the search request specified by an LDAP URL, you can invoke one of the following methods, passing in the LDAPUrl object:

  • If the URL specifies a base search for a single entry, invoke the read method of the LDAPConnection object.
    This method reads the entry from the directory.
  • Otherwise, invoke the search method of the LDAPConnection object to perform the search.

Both methods create a new LDAPConnection object and connect to the LDAP server specified in the URL. Next, the methods perform the search. Then the methods disconnect.

Searching Using an LDAP URL With LDAP Java SDK

The following example demonstrates a search that uses an LDAP URL, invoking the search method of the LDAPConnection object to perform the search. Before the search is performed, the LDAP URL is exploded using the methods suggested previously.

import netscape.ldap.*;
import netscape.ldap.util.*;
import java.net.MalformedURLException;
import java.util.*;

public class SrchUrl {
    public static void main( String[] args ) {
        LDAPConnection ld = null;
        LDAPEntry findEntry = null;
        int status = -1;
        try {
            UserArgs userArgs = new UserArgs("SrchUrl", args, false);
            ld = new LDAPConnection();
            ld.connect(userArgs.getHost(), userArgs.getPort());

            LDAPUrl myUrl = new LDAPUrl(
                "ldap://" +
                userArgs.getHost() +            // server host
                ":" +
                userArgs.getPort() +            // server port
                "/" +
                "dc=example,dc=com" +           // base DN
                "?" +
                "cn,sn,mail,telephonenumber" +  // attrs to retrieve
                "?" +
                "sub" +                         // search scope
                "?" +
                "(uid=bjensen)");               // search filter

            System.out.println( "LDAP URL : " + myUrl.toString() );
            System.out.println( "   host  : " + myUrl.getHost() );
            System.out.println( "   port  : " + myUrl.getPort() );
            System.out.println( "   baseDN: " + myUrl.getDN() );
            String [] myAttrs = myUrl.getAttributeArray();
            for ( String myAttr: myAttrs ) {
                 System.out.println( "   attrs : " + myAttr );
            }
            System.out.println( "   scope : " + myUrl.getScope() );
            System.out.println( "   filter: " + myUrl.getFilter() );

            LDAPSearchResults res = ld.search( myUrl );

            /* Loop on results until finished; will only be one! */
            while ( res.hasMoreElements() ) {

                /* Next directory entry, really only one at most */
                try {
                    findEntry = res.next();
                } catch ( LDAPReferralException e ) {
                    System.out.println( "Search reference: " );
                    LDAPUrl refUrls[] = e.getURLs();
                    for (int i=0; irefUrls.length; i++) {
                        System.out.println( "\t" + refUrls[i].getUrl() );
                    }
                    continue;
                } catch ( LDAPException e ) {
                    System.err.println( "Error: " + e.toString() );
                    continue;
                }

                /* Get the attributes of the entry */
                LDAPAttributeSet findAttrs = findEntry.getAttributeSet();
                Enumeration enumAttrs = findAttrs.getAttributes();

                /* Loop on attributes */
                while ( enumAttrs.hasMoreElements() ) {
                    LDAPAttribute anAttr =
                        (LDAPAttribute)enumAttrs.nextElement();
                    String attrName = anAttr.getName();
                    if ( attrName.equals( "cn" ) )
                        System.out.println( "Full name:" );
                    else if ( attrName.equals( "sn" ) )
                        System.out.println( "Last name (surname):" );
                    else if ( attrName.equals( "mail" ) )
                        System.out.println( "Email address:" );
                    else if ( attrName.equals( "telephonenumber" ) )
                        System.out.println( "Telephone number:" );
                    /* Loop on values for this attribute */
                    Enumeration enumVals = anAttr.getStringValues();
                    if (enumVals != null) {
                        while ( enumVals.hasMoreElements() ) {
                            String aVal = ( String )enumVals.nextElement();
                            System.out.println( "\t" + aVal );
                        }
                    }
                }
            }
        }
        catch( LDAPException e ) {
            System.err.println( "Error: " + e.toString() );
        }
        catch( MalformedURLException e ) {
            System.err.println( "Error: " + e.toString() );
        }

        /* Done, so disconnect */
        if ( (ld != null)  ld.isConnected() ) {
            try {
                ld.disconnect();
            } catch ( LDAPException e ) {
                System.out.println( "Error: " + e.toString() );
            }
        }
        System.exit(status);
    }
}

When this program is compiled and run against a directory that holds Barbara Jensen's entry, the program generates the following output.

$ java SrchUrl -h myhost -p 1389
LDAP URL : ldap://myhost:1389/dc=example,dc=com?cn,sn,mail,telephonenumber?sub?(uid=bjensen)
   host  : mykryten
   port  : 1389
   baseDN: dc=example,dc=com
   attrs : cn
   attrs : sn
   attrs : mail
   attrs : telephonenumber
   scope : 2
   filter: (uid=bjensen)
Full name:
        Barbara Jensen
        Babs Jensen
Last name (surname):
        Jensen
Email address:
        bjensen@example.com
Telephone number:
        +1 408 555 1862