Chapter 9 Comparing Values in Entries This chapter explains how to compare the value of an attribute in an entry against a specified value.
This chapter explains how to compare the value of an attribute in an entry against a specified value.
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".)
Use the distinguished name of the entry to find the entry in the directory and perform the comparison. (See "Performing the Comparison".)
... LDAPAttribute attr = new LDAPAttribute( "mail", "bjensen@airius.com" ); ...
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." ); } } }