Paged Results

Request that the server return results in batches of a specific size. See RFC 2696. This control is often used to work around server result size limits.

Using the Paged Results Client

The PagedResultClient encapsulates the cookie management associated with this control and exposes convenient methods for common operations. Note that you must use a SingleConnectionFactory with this client as each search request must occur on the same connection. In addition, the connection factory should not be used for other purposes while in use by the PagedResultsClient.

Perform paged search to completion

SingleConnectionFactory cf = new SingleConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
PagedResultsClient client = new PagedResultsClient(cf, 25); // return 25 entries at a time
SearchRequest request = new SearchRequest("dc=ldaptive,dc=org","(givenName=d*)", "cn", "sn");
SearchResponse response = client.executeToCompletion(request);
for (LdapEntry entry : response.getEntries()) {
  // do something useful with the entry
}
cf.close();

Inspect each response myself

SingleConnectionFactory cf = new SingleConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
PagedResultsClient client = new PagedResultsClient(cf, 25); // return 25 entries at a time
SearchRequest request = new SearchRequest("dc=ldaptive,dc=org","(givenName=d*)", "cn", "sn");
SearchResponse response = client.execute(request);
while (client.hasMore(response)) {
  response = client.execute(request, response);
  // inspect the response and break out of the loop if necessary
}
cf.close();

Using the PagedResultsControl

If you need fine grain control over this operation, this sample code illustrates how to use the PagedResultsControl directly with a SearchOperation.

SingleConnectionFactory cf = new SingleConnectionFactory("ldap://directory.ldaptive.org");
cf.initialize();
SearchOperation search = new SearchOperation(cf);
SearchRequest request = SearchRequest.builder()
  .dn("dc=ldaptive,dc=org")
  .filter("(givenName=d*)")
  .returnAttributes("cn", "sn")
  .build();
PagedResultsControl prc = new PagedResultsControl(25); // return 25 entries at a time
request.setControls(prc);
SearchResponse response = new SearchResponse();
byte[] cookie = null;
do {
  prc.setCookie(cookie);
  SearchResponse res = search.execute(request);
  response.addEntries(res.getEntries());
  cookie = null;
  PagedResultsControl ctl = (PagedResultsControl) response.getControl(PagedResultsControl.OID);
  if (ctl != null) {
    if (ctl.getCookie() != null && ctl.getCookie().length > 0) {
      cookie = ctl.getCookie();
    }
  }
} while (cookie != null);
cf.close();