UnboundId - 搜索结果中的响应控制

时间:2014-06-20 00:00:55

标签: unboundid-ldap-sdk

我使用UnboundId SDK搜索LDAP并使用SimplePagedResultsControl来分页我的结果。我能够正确搜索并根据页面大小获得第一组所需结果,但由于来自SearchResult的响应控制对象为NULL,因此无法检索后续结果集。我需要检索cookie值并在搜索请求的下一个搜索请求中设置它以继续检索剩余的结果。

我使用的是UnboundId SDK网站和其他网站中提供的类似代码。任何帮助解决这个问题将不胜感激。

// Perform a search to retrieve all users in the server, but only retrieving
// ten at a time.

int numSearches = 0;
int totalEntriesReturned = 0;

SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
   SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person"));

ASN1OctetString resumeCookie = null;

while (true)
{
  searchRequest.setControls(
       new SimplePagedResultsControl(10, resumeCookie));
  SearchResult searchResult = connection.search(searchRequest);
  numSearches++;
  totalEntriesReturned += searchResult.getEntryCount();

 for (SearchResultEntry e : searchResult.getSearchEntries())
 {
   // Do something with each entry...
 }

 LDAPTestUtils.assertHasControl(searchResult,
    SimplePagedResultsControl.PAGED_RESULTS_OID); -*Failing here as the SearchResult obj
                                                    is not having any Response Control*
 SimplePagedResultsControl responseControl =
     SimplePagedResultsControl.get(searchResult);

 if (responseControl.moreResultsToReturn())
 {
   // The resume cookie can be included in the simple paged results
   // control included in the next search to get the next page of results.
   resumeCookie = responseControl.getCookie();
 }
 else
 {
   break;
 }
}

1 个答案:

答案 0 :(得分:1)

我没有看到任何明显错误的代码,所以我的第一个猜测是你使用的服务器不支持控件(如果它确实那么“1.2.840.113556.1.4.319”应该出现在根DSE的supportedControl属性中),或者连接被认证为没有权限使用它的用户(在这种情况下,作为更强大的用户进行身份验证应该允许按预期处理控件)。由于控件未标记为关键,因此服务器应忽略该控件(如果它不支持它),并且如果请求者没有使用它的权限,则某些服务器可能会选择忽略该控件而不是拒绝该请求。

确定控件是否正在处理的最佳方法可能是检查搜索是否仅返回页面大小中指定的条目数或与搜索匹配的整个结果集。如果搜索仅返回单页结果,则服务器应该已经确定地返回了响应控件。如果服务器忽略了控件并处理了请求,就好像未提供控件一样,那么结果集应该包括服务器中与搜索条件匹配的所有条目。