Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNDI/LDAP and PagedResultsControl

Reply
Thread Tools

JNDI/LDAP and PagedResultsControl

 
 
Mark
Guest
Posts: n/a
 
      03-05-2004
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[i] instanceof PagedResultsResponseControl)
{
PagedResultsResponseControl p = (PagedResultsResponseControl)
responseControls[i];
cookie = p.getCookie();
total = p.getResultSize();
}
}
}
else
{
System.out.println("response controls is null");
}

ldapContext.close();
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments