Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6451075

java 1.5.0_07: JNDI-ldap: Not being able to specify Custom javax.naming.ldap.Con

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • 5.0-pool
    • 5.0u7
    • core-libs

      My application utilizes JNDI for searching on ldap directories. I jdk 1.4 I developed custom Paging control request and response classes for paging. These classes implemented javax.naming.ldap.Control interface. I also wrote a custom ControlFactory to return the correct paging response control instances. This ControlFactory class returned the correct Custom paged response control instance when directory returned paging response controls.
      This factory classname is passed in environment when creating InitialLdapContext instance.

      The application code compiles and runs fine in jdk1.4 with custom control factory doing its job perfectly.
      However in jdk5.0 this custom control factory is not being picked up at all and LdapContext.responseControls() always returns instance of "javax.naming.ldap.PagedResultsControl".

      Now because of this my code has broken in jdk1.5.


      I have written some sample code to simulate this behaviour.However here for sake of simplicity I am using "com.sun.jndi.ldap.ctl.PagedResultsControl" and "com.sun.jndi.ldap.ctl.PagedResultsResponseControl" instead of my custom Paging control classes.
      This sample code works fine for 1.4 but fails for 5.0.

      SampleControlFactory.java:
      =======================================================================================

      import javax.naming.ldap.Control;
      import javax.naming.ldap.ControlFactory;
      import javax.naming.*;
      import com.sun.jndi.ldap.ctl.*;

      public class SampleControlFactory extends ControlFactory
      {
        public SampleControlFactory()
        {
          System.out.println("=================SampleControlFactory============");
        }
        public Control getControlInstance(Control inCtrl) throws NamingException
        {
          String inOID = inCtrl.getID();
          Control outCtrl = null;
          if (inOID.equals(PagedResultsResponseControl.OID))
          {
            try
            {
              outCtrl = new PagedResultsResponseControl(inOID, inCtrl.isCritical(),
                                                 inCtrl.getEncodedValue());
            }catch (Exception e)
            {
              throw new NamingException(e.getMessage());
            }
          }
          return outCtrl;
        }
      }
      =======================================================================================


      Test4.java:
      =======================================================================================

      import javax.naming.ldap.Control;
      import javax.naming.ldap.HasControls;
      import javax.naming.ldap.LdapContext;
      import javax.naming.ldap.InitialLdapContext;
      import javax.naming.*;
      import javax.naming.directory.*;
      import java.util.*;
      import com.sun.jndi.ldap.ctl.*;
        
        
      public class Test4
      {
        public static void main (String[] args) throws Exception
        {
          Hashtable jndiProps = new Hashtable();
          jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
                          "com.sun.jndi.ldap.LdapCtxFactory");
          jndiProps.put(Context.PROVIDER_URL, "ldap://<URL>");
          jndiProps.put(LdapContext.CONTROL_FACTORIES, "SampleControlFactory");
          jndiProps.put(Context.SECURITY_PRINCIPAL,"<DN>");
          jndiProps.put(Context.SECURITY_CREDENTIALS,"<PASSWORD>");

           // Open an LDAP association
           LdapContext ctx = new InitialLdapContext(jndiProps, null);

           // Activate paged results
           int pageSize = 10; // 20 entries per page
           byte[] cookie = null;
           int total, cnt = 0;;

           SampleControlFactory customCtrlFact = new SampleControlFactory();

           ctx.setRequestControls(new Control[]{
               new PagedResultsControl(pageSize ) });

           do {
               // Perform the search
               SearchControls ctrls = new SearchControls();
               ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
               System.out.println("search() with cookie = "+cookie);
               NamingEnumeration results =
                   ctx.search("<SEARCH_BASE>",
                      "(objectclass=inetorgperson)", ctrls);

               // Iterate over a batch of search results
               cnt++;
               System.out.println("********************Page "+cnt+" **************");
               while (results != null && results.hasMore()) {
                   // Display an entry
                   SearchResult entry = (SearchResult)results.next();
                   System.out.println("DN:" +entry.getName());

                   // Handle the entry's response controls (if any)
                   if (entry instanceof HasControls) {
                       System.out.println("Entry response controls: " +
                                               ((HasControls)entry).getControls());
                   }
               }
               // Examine the paged results control response
               Control[] controls = ctx.getResponseControls();
               if (controls != null) {
                   for (int i = 0; i < controls.length; i++) {
                       System.out.println("Response Control instance of class: "+ controls[i].getClass());

                       // this line is addded as workaround to jdk5.0 ControlFactory
                       // problem
                       //controls[i] = customCtrlFact.getControlInstance(controls[i]);

                       if (controls[i] instanceof PagedResultsResponseControl) {
                           PagedResultsResponseControl prrc =
                               (PagedResultsResponseControl)controls[i];
                           total = prrc.getResultSize();
                           cookie = prrc.getCookie();
                       } else {
                           // Handle other response controls (if any)
                       }
                   }
               }
               System.out.println("**********************************");
               System.out.println();


               if (cookie == null) // search over
                 break;

               // Re-activate paged results
               ctx.setRequestControls(new Control[]{
                   new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
           } while (cookie != null);

           // Close the LDAP association
           ctx.close();
        }
      }

      =======================================================================================

            vtewari Vyom Tewari
            mmma Marvin Ma (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: