You can determine what version an LDAP server supports by getting the supportedLDAPVersion attribute from the root DSE. This attribute should contain the value 3. (It may also contain other values, such as 2, so you may want to check through the values of this attribute.)
Note that you do not need to authenticate or bind (see "Binding and Authenticating to an LDAP Server" for details) before searching the directory. Unlike the LDAP v2 protocol, the LDAP v3 protocol states that clients do not need to bind to the server before performing LDAP operations.
The following section of code connects to an LDAP server and determines whether or not that server supports the LDAP v3 protocol.
...
import netscape.ldap.*;
import java.util.*;
...
/* Create a new connection. */
LDAPConnection ld = new LDAPConnection();
String hostname = "localhost";
int portnumber = LDAPv2.DEFAULT_PORT;
boolean supportsV3 = false;
try {
/* Connect to the LDAP server. */
ld.connect( 3, hostname, portnumber );
/* Get the root DSE by doing a search where:
- The scope is SCOPE_BASE
- The base is ""
- The search filter is "(objectclass=*)"
*/
int MY_SCOPE = LDAPv2.SCOPE_BASE;
String MY_FILTER = "(objectclass=*)";
String MY_SEARCHBASE = "";
LDAPSearchResults res = ld.search( MY_SEARCHBASE,
MY_SCOPE, MY_FILTER, null, false );
/* There should be only one entry in the results (the root DSE). */
while ( res.hasMoreElements() ) {
LDAPEntry findEntry = (LDAPEntry)res.nextElement();
/* Get the supportedLDAPVersion attribute. */
LDAPAttribute versionAttr =
findEntry.getAttribute( "supportedLDAPVersion" );
/* Check if "3" is one of the supported LDAP versions. */
Enumeration enumVals = versionAttr.getStringValues();
if ( enumVals == null ) {
System.out.println( "\tNo values found." );
continue;
}
while ( enumVals.hasMoreElements() ) {
String aVal = ( String )enumVals.nextElement();
if ( aVal.equalsIgnoreCase( "3" ) ) {
supportsV3 = true;
break;
}
}
}
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
if ( supportsV3 ) {
System.out.println( "This server supports LDAP v3." );
} else {
System.out.println( "This server does not support LDAP v3." );
}
...
|