import java.util.Enumeration;
import java.util.Iterator;
import com.novell.ldap.*;
import com.novell.ldap.controls.*;
import java.io.UnsupportedEncodingException;
public class SortControl {
public static void main( String[] args ) {
if (args.length != 4) {
System.err.println("Usage: java SortControl <host name> "
+ "<login dn> <password> <container>");
System.err.println("Example: java SortControl Acme.com \"cn=admin,"
+"o=Acme\" secret \"ou=Sales,o=Acme\"");
System.exit(1);
}
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String searchBase = args[3];
String returnedAttributes[] = {"*", "+"};
String oid;
boolean attributeOnly = false;
boolean isSupported = false;
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String searchFilter= "(objectclass=*)";
LDAPConnection lc = new LDAPConnection();
try {
lc.connect( ldapHost, ldapPort );
lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );
LDAPSearchResults searchResults = lc.search(
"",
LDAPConnection.SCOPE_BASE,
"(objectclass=*)",
returnedAttributes,
attributeOnly );
LDAPEntry entry = null;
try {
entry = searchResults.next();
}
catch(LDAPException e) {
System.out.println("Error: " + e.toString());
}
LDAPAttributeSet attributeSet = entry.getAttributeSet();
Iterator allAttributes = attributeSet.iterator();
while(allAttributes.hasNext()) {
LDAPAttribute attribute = (LDAPAttribute)allAttributes.next();
String attrName = attribute.getName();
Enumeration allValues = attribute.getStringValues();
if( allValues != null) {
while(allValues.hasMoreElements()) {
if (attrName.equalsIgnoreCase("supportedControl")) {
oid = (String) allValues.nextElement();
if(oid.equalsIgnoreCase("1.2.840.113556.1.4.473")
||oid.equalsIgnoreCase("1.2.840.113556.1.4.474"))
isSupported = true;
}
else
allValues.nextElement();
}
}
}
if(isSupported == true) {
String[] attrs = new String[1];
attrs[0] = "sn";
LDAPSortKey[] keys = new LDAPSortKey[1];
keys[0] = new LDAPSortKey( "sn" );
LDAPSortControl sort = new LDAPSortControl( keys, true );
LDAPSearchConstraints cons = lc.getSearchConstraints();
cons.setControls( sort );
lc.setConstraints(cons);
System.out.println( "\nCalling search request...");
LDAPSearchResults res = lc.search(
searchBase,
LDAPConnection.SCOPE_SUB,
searchFilter,
attrs,
false,
(LDAPSearchConstraints) null );
while ( res.hasMore() ) {
LDAPEntry nextEntry;
try {
nextEntry = res.next();
}
catch(LDAPException e) {
if ( e instanceof LDAPReferralException)
continue;
else
break;
}
System.out.println();
System.out.println( "Entry dn: " + nextEntry.getDN() );
LDAPAttributeSet findAttrs = nextEntry.getAttributeSet();
Iterator enumAttrs = findAttrs.iterator();
System.out.println( "Attribute(s):" );
while ( enumAttrs.hasNext() ) {
LDAPAttribute anAttr = (
LDAPAttribute)enumAttrs.next();
String attrName = anAttr.getName();
Enumeration enumVals = anAttr.getStringValues();
while ( enumVals.hasMoreElements() ) {
String aVal = ( String )enumVals.nextElement();
System.out.println( " " + attrName + ": " + aVal );
}
}
}
LDAPControl[] controls = res.getResponseControls();
if ( controls != null ) {
for( int i = 0; i < controls.length; i++ ) {
if ( controls[i] instanceof LDAPSortResponse ) {
System.out.println("Received LDAP Sort Control from "
+ "server");
String bad =
((LDAPSortResponse)controls[i]).getFailedAttribute();
int result =
((LDAPSortResponse)controls[i]).getResultCode();
System.out.println( "Error code: " + result );
if ( bad != null )
System.out.println( "Offending " + "attribute: "
+ bad );
else
System.out.println( "No offending " + "attribute "
+ "returned" );
}
}
}
if ( lc.isConnected() )
lc.disconnect();
}
else {
System.out.println("The Requested Server does not support the Sort Control.");
lc.disconnect();
System.exit(1);
}
}catch( LDAPException e ) {
System.out.println( e.toString() );
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}
}
}