M
Mark
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();
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();