@Test
public void ldapPageSearch() {
try {
int numSearches = 0;
int totalEntriesReturned = 0;
LDAPConnection connection = new LDAPConnection("127.0.0.1", 389, "cn=Manager,c=cn", "123456");
// SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
// SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person"));
SearchRequest searchRequest = new SearchRequest("c=cn", SearchScope.SUB, "objectClass=*");
ASN1OctetString resumeCookie = null;
SearchResult searchResult = null;
while (true) {
searchRequest.setControls(
new SimplePagedResultsControl(3, resumeCookie));
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);
SimplePagedResultsControl responseControl =
SimplePagedResultsControl.get(searchResult);
System.out.println("/////////////////////////////////////////////");
System.out.println("numSearches:" + numSearches);
System.out.println("totalEntriesReturned:" + totalEntriesReturned);
// System.out.println("preCookie:" + preCookie);
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();
System.out.println("/////////////////////////////////////////////");
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
参考链接:https://docs.ldap.com/ldap-sdk/docs/javadoc/com/unboundid/ldap/sdk/controls/SimplePagedResultsControl.html