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.

Perform paged search to completion

Connection conn = DefaultConnectionFactory.getConnection("ldap://directory.ldaptive.org");
try {
  conn.open();
  PagedResultsClient client = new PagedResultsClient(conn, 25); // return 25 entries at a time
  SearchRequest request = new SearchRequest("dc=ldaptive,dc=org","(givenName=d*)", "cn", "sn");
  Response<SearchResult> response = client.executeToCompletion(request);
  SearchResult result = response.getResult();
  for (LdapEntry entry : result.getEntries()) {
    // do something useful with the entry
  }
} finally {
  conn.close();
}

Inspect each response myself

Connection conn = DefaultConnectionFactory.getConnection("ldap://directory.ldaptive.org");
try {
  conn.open();
  PagedResultsClient client = new PagedResultsClient(conn, 25); // return 25 entries at a time
  SearchRequest request = new SearchRequest("dc=ldaptive,dc=org","(givenName=d*)", "cn", "sn");
  Response<SearchResult> response = client.execute(request);
  while (client.hasMore(response)) {
    response = client.execute(request, response);
    // inspect the response and break out of the loop if necessary
  }
} finally {
  conn.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.

Connection conn = DefaultConnectionFactory.getConnection("ldap://directory.ldaptive.org");
try {
  conn.open();
  SearchOperation search = new SearchOperation(conn);
  SearchRequest request = new SearchRequest("dc=ldaptive,dc=org","(givenName=d*)", "cn", "sn");
  PagedResultsControl prc = new PagedResultsControl(25); // return 25 entries at a time
  request.setControls(prc);
  SearchResult result = new SearchResult();
  byte[] cookie = null;
  do {
    prc.setCookie(cookie);
    Response<SearchResult> response = search.execute(request);
    result.addEntries(response.getResult().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);
} finally {
  conn.close();
}

Provider Support

JNDI JLDAP Apache LDAP UnboundID OpenDJ
Functionality exists in the provider API and has been confirmed
Functionality does not exist in the provider API
Functionality exists in the provider API, but did not work in our test environment
? Functionality exists in the provider API, but has not been integrated into the ldaptive provider