Complete Contents
Preface
Chapter 1 Understanding LDAP
Chapter 2 Using the Netscape Directory SDK for Java
Chapter 3 Quick Start
Chapter 4 Writing an LDAP Client
Chapter 5 Using the LDAP Java Classes
Chapter 6 Searching the Directory
Chapter 7 Using Filter Configuration Files
Chapter 8 Adding, Updating, and Deleting Entries
Chapter 9 Comparing Values in Entries
Chapter 10 Working with LDAP URLs
Chapter 11 Getting Server Information
Chapter 12 Connecting Over SSL
Chapter 13 Working with LDAP Controls
Chapter 14 Using SASL Authentication
Chapter 15 Using Netscape's JNDI Service Provider
Chapter 16 Working with Extended Operations
Chapter 17 Using the Asynchronous Interface
Glossary
Directory SDK for Java 4.0 Programmer's Guide: Quick Start
Previous Next Contents Index


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:


Understanding the Sample Client
The following is the source code for a program that retrieves the full name ("cn"), last name ("sn"), email address ("mail"), and telephone number ("telephoneNumber") of Barbara Jensen. You can find this program in the GetAttrs.java file in the examples/java directory.

Basically, the example does the following:

  1. Creates a new LDAPConnection object, which represents the connection to the LDAP server.
  2. Connects to the server.
  3. Performs a search that retrieves a single entry, identified by its DN. To do this, the search criteria is set up so that:

  1. 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).
  2. Disconnects from the server.
Before you compile the sample client, replace "localhost" and "389" with the hostname and port number of the LDAP server that you are using. Also, make sure that the packages/ldapjdk.jar file is in your CLASSPATH.


Sample Code
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);
   }
}

 

© Copyright 1999 Netscape Communications Corporation. All rights reserved.