Chapter 3 Quick Start This chapter provides a simple example of an LDAP client written with the Netscape Directory SDK for Java. The chapter contains the following sections:
This chapter provides a simple example of an LDAP client written with the Netscape Directory SDK for Java.
"Understanding the Sample Client"
"Sample Code"
Creates a new LDAPConnection object, which represents the connection to the LDAP server.
Connects to the server.
Performs a search that retrieves a single entry, identified by its DN. To do this, the search criteria is set up so that:
The base distinguished name (the starting point of the search) is the entry "uid=bjensen, ou=People, o=Airius.com"
The scope of the search is LDAPConnection.SCOPE_BASE, which only includes the base DN
The search filter is "(objectclass=*)", which finds anything that matches. Since the scope narrows the search down to a single entry, the search filter does not need to be used.
(Note that performing a search for a single entry is equivalent to invoking the LDAPConnection.read method.)
The string array attrNames contains the attributes that the search will return.
The attrsOnly property is set to false, which means that the names and values of the specified attributes will be returned.
Iterates through the enumerated search results to retrieve and print the values of the cn, sn, mail, and telephoneNumber attributes. This iteration also allows the client to obtain multiple instances of a single attribute (two telephone numbers, for example).
Disconnects from the server.
import netscape.ldap.*; import java.util.*; public class GetAttrs { public static void main( String[] args ) { LDAPConnection ld = null; LDAPEntry findEntry=null; int status = -1; try { ld = new LDAPConnection(); /* Connect to server */ String MY_HOST = "localhost"; int MY_PORT = 389; ld.connect( MY_HOST, MY_PORT ); String ENTRYDN = "uid=bjensen, ou=People, o=Airius.com"; String[] attrNames = { "cn", /* Get canonical name(s) (full name) */ "sn", /* Get surname(s) (last name) */ "mail", /* Get email address(es) */ "telephonenumber" }; /* Get telephone number(s) */ LDAPSearchResults res = ld.search( ENTRYDN, LDAPConnection.SCOPE_BASE, "objectclass=*", attrNames, false ); /* Loop on results until finished; will only be one! */ while ( res.hasMoreElements() ) { /* Next directory entry, really only one at most */ LDAPEntry findEntry = null; try { findEntry = res.next(); /* If the next result is a search reference, print the LDAP URLs in the reference. */ } catch ( LDAPReferralException e ) { System.out.println( "Search reference: " ); LDAPUrl refUrls[] = e.getURLs(); for (int i=0; i<refUrls.length; i++) { System.out.println( "\t" + refUrls[i].getURL() ); } continue; } catch ( LDAPException e ) { System.out.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(); while ( enumVals.hasMoreElements() ) { String aVal = ( String )enumVals.nextElement(); System.out.println( "\t" + aVal ); } } } } catch( LDAPException e ) { System.out.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); } }