/**
*
* SortControlJndiClient.java
* Sample code to demonstrate how Sort Control works.
*
*/
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import java.util.Hashtable;
public class SortControlJndiClient
{
static final String SORT_CONTROL_OID = "1.2.840.113556.1.4.473";
public static void main(String[] args)
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://myAD.mydomain.com:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "administrator@mydomain.com");
env.put(Context.SECURITY_CREDENTIALS, "mypassword");
try{
/* Open an LDAP connection for the provided principal and credentials */
LdapContext ctx = new InitialLdapContext(env, null);
System.out.println("Initial binding done!");
/* Query the server to see if the sort control is supported */
if(!isSortControlSupported(ctx)){
System.out.println("The server does not support Sort Control.");
System.exit(1);
}
// Activate Sorting: A list of people sorted by:
// ascending family name and descending given name
SortKey[] sk = new SortKey[2];
sk[0] = new SortKey("sn"); // ascending
sk[1] = new SortKey("givenName", false, null); // descending
ctx.setRequestControls(
new Control[]{new SortControl(sk, Control.CRITICAL)});
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
ctls.setCountLimit(0);
// Perform the search
NamingEnumeration results = ctx.search("ou=people,dc=mydomain,dc=com",
"(objectclass=*)", ctls);
// Iterate over the search results
while (results != null && results.hasMore()) {
// Display an entry
SearchResult entry = (SearchResult)results.next();
System.out.println("entryDN=" + entry.getName());
/* other output here */
}
// Examine the sort control response
Control[] controls = ctx.getResponseControls();
if(controls!=null){
for(int k = 0; k<controls.length; k++){
if(controls[k] instanceof SortResponseControl){
SortResponseControl src =
(SortResponseControl)controls[k];
if(!src.isSorted()){
throw src.getException();
}
}else{
// Handle other response controls (if any)
}
}
}
// Close the LDAP association
ctx.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* Is Sort control supported?
*
* Query the rootDSE object to find out if the sort control
* is supported.
*/
static boolean isSortControlSupported(LdapContext ctx)
throws NamingException
{
SearchControls ctl = new SearchControls();
ctl.setReturningAttributes(new String[]{"supportedControl"});
ctl.setSearchScope(SearchControls.OBJECT_SCOPE);
/* search for the rootDSE object */
NamingEnumeration results = ctx.search("", "(objectClass=*)", ctl);
while(results.hasMore()){
SearchResult entry = (SearchResult)results.next();
NamingEnumeration attrs = entry.getAttributes().getAll();
while (attrs.hasMore()){
Attribute attr = (Attribute)attrs.next();
NamingEnumeration vals = attr.getAll();
while (vals.hasMore()){
String value = (String) vals.next();
if(value.equals(SORT_CONTROL_OID))
return true;
}
}
}
return false;
}
}