go to  ForumEasy.com   
LdapPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Who Support Sort Control?
 
Subject: Who Support Sort Control?
Author: SteveHB
In response to: Code Example
Posted on: 06/07/2007 01:26:58 PM

Most enterprise LDAP servers support Sort Control. Among them are Active Directory and SunONE Directory Server.

The above code has been tested against AD and SunOne. It works for both server but exceptions may be different when things went wrong.

 

> On 06/07/2007 01:22:21 PM SteveHB wrote:


/**
 * 
 * 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;
  }

}




References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.