JNDI/LDAP and PagedResultsControl

Discussion in 'Java' started by Mark, Mar 5, 2004.

  1. Mark

    Mark Guest

    I've encountered something that has stumped me for the past 2 days.
    Perhaps someone on this list has seen this before or can explain why
    this might be happening. I'm trying to use the SortControl and
    PagedResultsControl classes that are part of the JNDI/LDAP Booster
    Pack. In the code below, you can see that I create the 2 Control
    object, stick them in an array, and set them as "request controls" for
    the LdapContext. Thankfully, both the search and the sort controls do
    the right thing - the items come out sorted with a page size of 10.

    My issue is trying to get the "response controls" associated with
    invoking the search() method on the LdapContext. According to the
    JNDI JavaDocs, the getResponseControls() method "Retrieves the
    response controls produced as a result of the last method invoked on
    this context" (which in my case is the search() method). If I execute
    a getResponseControls() call on the LdapContext immediately after the
    search(), it returns null. However, after MUCH agonizing I figured
    out that if I walk over the enumeration of results returned from the
    search() method and do a getResponseControls() afterward, the proper
    Control object array is returned (one for the SortControl and one for
    the PagedResultsControl). I noticed that this was the common element
    in code snippets I found on the Web that worked.

    Can someone please explain to me why I have to walk through this
    enumeration? I also verified that you have to walk over the entire
    enumeration, not just an element or 2. Specifically, the hasMore()
    method has to be called to get this to work. If I create a for loop
    and walk through my page set (say 10 elements) and then try to get the
    response controls, they're still null. It only works when hasMore()
    returns false. This is really driving me bonkers…

    Thank you in advance for anyone who can help me here!


    String sortBy = "sn";
    int pageSize = 10;
    byte[] cookie = null;
    int total;

    Control[] requestControls = new Control[]
    {
    new SortControl(new String[]{sortBy}, Control.CRITICAL),
    new PagedResultsControl(pageSize)
    };

    LdapContext ldapContext = new InitialLdapContext(environment, null);
    ldapContext.setRequestControls(requestControls);

    NamingEnumeration results = ldapContext.search(contextName,
    searchFilter, searchControls);

    //IF I DON'T WALK OVER THIS ENUMERATION, THE RESPONSE CONTROLS OBJECT
    IS NULL
    while(results != null && results.hasMore())
    {
    SearchResult entry = (SearchResult) results.next();
    }

    Control[] responseControls = ldapContext.getResponseControls();
    if(responseControls != null)
    {
    for(int i = 0; i < responseControls.length; i++)
    {
    if(responseControls instanceof PagedResultsResponseControl)
    {
    PagedResultsResponseControl p = (PagedResultsResponseControl)
    responseControls;
    cookie = p.getCookie();
    total = p.getResultSize();
    }
    }
    }
    else
    {
    System.out.println("response controls is null");
    }

    ldapContext.close();
     
    Mark, Mar 5, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Dees

    JNDI LDAP problem in JRE 1.4

    Ray Dees, Sep 5, 2003, in forum: Java
    Replies:
    0
    Views:
    743
    Ray Dees
    Sep 5, 2003
  2. Mike S
    Replies:
    0
    Views:
    23,077
    Mike S
    Sep 16, 2004
  3. Mick
    Replies:
    2
    Views:
    9,643
    Marcin Grunwald
    Feb 23, 2005
  4. John English

    Authenticating via LDAP and JNDI

    John English, Feb 25, 2005, in forum: Java
    Replies:
    1
    Views:
    430
  5. Akaketwa
    Replies:
    1
    Views:
    5,023
    impaler
    Sep 22, 2006
Loading...

Share This Page