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: Comparing Values in Entries
Previous Next Contents Index


Chapter 9 Comparing Values in Entries

This chapter explains how to compare the value of an attribute in an entry against a specified value.


Comparing the Value of an Attribute
The LDAP Java classes allow you to compare a specified value against the value of an entry in the directory. For example, you can check to see if the "mail" attribute of an entry contains the value "bjensen@airius.com".

To compare a specified value against an attribute of an entry in the directory, you need to follow this general procedure:

  1. Specify the name of the attribute that you want to check and the value that you want to use for comparison. (See "Specifying the Attribute and Value".)
  2. Use the distinguished name of the entry to find the entry in the directory and perform the comparison. (See "Performing the Comparison".)
For a complete example, see "Example of Comparing a Value Against an Attribute".

Specifying the Attribute and Value
Use an LDAPAttribute object to specify the name of the attribute that you want to check and the value that you want to use in the comparison.

For example, to compare the value "bjensen@airius.com" against the value of the "mail" attribute in an entry, use the following constructor:

...
LDAPAttribute attr = new LDAPAttribute( "mail", "bjensen@airius.com" );
...
Performing the Comparison
To perform the comparison, use the compare method of the LDAPConnection object. Specify the distinguished name (DN) of the entry that you want to compare.

This method returns true if the attribute in the entry contains the specified value.

Example of Comparing a Value Against an Attribute
The following section of code determines if the values "person" and "xyzzy" are in the "objectclass" attribute of the entry for Barbara Jensen.

import netscape.ldap.*;
import java.util.*;
public class Compare {
   public static void main( String[] args ) {

      /* Connect to the server and perform the comparison. */
      LDAPConnection ld = null;
      int status = -1;
      try {
         ld = new LDAPConnection();

         /* Connect to the server. */
         String HOSTNAME = "localhost";
         ld.connect( HOSTNAME, LDAPv2.DEFAULT_PORT );

         /* Authenticate to the server as the directory manager. */
         String MGR_DN = "cn=Directory Manager";
         String MGR_PW = "23skidoo";
         ld.authenticate( MGR_DN, MGR_PW );

         /* Perform the comparisons. */
         String ENTRYDN = "uid=bjensen, ou=People, o=Airius.com";
         LDAPAttribute attr = new LDAPAttribute( "objectclass",
            "person" );
         boolean ok = ld.compare( ENTRYDN, attr );
         reportResults( ok, attr );

         attr = new LDAPAttribute( "objectclass", "xyzzy" );
         ok = ld.compare( ENTRYDN, attr );
         reportResults( ok, attr );
      }
      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);
   }

   /* Print the results of the comparison. */
   private static void reportResults( boolean ok, LDAPAttribute attr ) {
      String result;
      if ( ok )
         result = new String();
      else
         result = new String( "not " );
      Enumeration en = attr.getStringValues();
      if (en != null) {
         String val = (String)en.nextElement();
         System.out.println(
            "The value " + val + " is " + result + "contained in the " +
            attr.getName() + " attribute." );
      }
   }
}

 

© Copyright 1999 Netscape Communications Corporation. All rights reserved.