import com.novell.ldap.*;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class ClientSideSort
{
public static void main( String[] args )
{
if (args.length != 5) {
System.out.println("Usage: java ClientSideSort <host name> "+
"<login dn> <password> <search base>\n"
+ " <search filter>");
System.out.println("Example: java ClientSideSort Acme.com"
+ " \"cn=admin,o=Acme\""
+ " secret \"ou=sales,o=Acme\"\n"
+ " \"(objectclass=*)\"");
System.exit(0);
}
int ldapPort = LDAPConnection.DEFAULT_PORT;
int searchScope = LDAPConnection.SCOPE_ONE;
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String searchBase = args[3];
String searchFilter = args[4];
LDAPConnection conn = new LDAPConnection();
try {
conn.connect( ldapHost, ldapPort );
conn.bind( ldapVersion, loginDN, password.getBytes("UTF8"));
LDAPSearchResults searchResults =
conn.search( searchBase,
searchScope,
searchFilter,
new String[] {"cn", "uid", "sn"},
false);
TreeSet sortedResults = new TreeSet();
while ( searchResults.hasMore()) {
try {
sortedResults.add( searchResults.next() );
}
catch(LDAPException e) {
System.out.println("Error: " + e.toString());
continue;
}
}
System.out.println( "\n"+
"****************************\n"+
"Search results sorted by DN:\n"+
"****************************");
Iterator i = sortedResults.iterator();
while (i.hasNext()){
printEntry( (LDAPEntry) i.next() );
}
String namesToSortBy[] = { "sn", "uid", "cn" };
boolean sortAscending[] = { true, false, true };
LDAPCompareAttrNames myComparator = new LDAPCompareAttrNames(
namesToSortBy, sortAscending);
Object sortedSpecial[] = sortedResults.toArray();
Arrays.sort(sortedSpecial, myComparator);
System.out.println( "\n" +
"*****************************************************\n" +
"Search results sorted by sn, uid(Descending), and cn:\n" +
"*****************************************************");
for(int j=0; j< sortedSpecial.length; j++){
printEntry( (LDAPEntry) sortedSpecial[j] );
}
conn.disconnect();
}
catch( LDAPException e ) {
System.out.println( "Error: " + e.toString() );
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}
System.exit(0);
}
public static void printEntry(LDAPEntry entry){
System.out.println(entry.getDN());
System.out.println("\tAttributes: ");
LDAPAttributeSet attributeSet = entry.getAttributeSet();
Set sortedAttributes = new TreeSet( attributeSet );
Iterator allAttributes = sortedAttributes.iterator();
while(allAttributes.hasNext()) {
LDAPAttribute attribute =
(LDAPAttribute)allAttributes.next();
String attributeName = attribute.getName();
System.out.println("\t\t" + attributeName);
Enumeration allValues = attribute.getStringValues();
if( allValues != null) {
while(allValues.hasMoreElements()) {
String Value = (String) allValues.nextElement();
System.out.println("\t\t\t" + Value);
}
}
}
return;
}
}