我正在使用来自此站点的 LDAP SDK:https://www.unboundid.com/products/ldap-sdk/ . 我想进行一个返回大量条目的搜索操作。
根据常见问题解答网站,( https://www.unboundid.com/products/ldap-sdk/docs/ldapsdk-faq.php#search ) 我必须使用 SearchResultListener 实现。
这就是我所做的:
public class UpdateThread extends Thread implements SearchResultListener {
...
// create request
final SearchRequest request = new SearchRequest(this, instance.getBaseDN(),SearchScope.SUB, filter);
// Setting size limit of results.
request.setSizeLimit(2000);
...
// Get every result one by one.
@Override
public void searchEntryReturned(SearchResultEntry arg0) {
System.out.println("entry "+arg0.getDN());
}
问题在于“searchEntryReturned”最多返回 1000 个结果。即使我将大小限制设置为“2000”。
最佳答案
使用标准 java 实现分页 LDAP 查询非常简单,方法是将 PagedResultsControl
添加到 LdapContext
,无需使用 Neil 的第三方 API上面的回答。
Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env
.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL,
"ldap://localhost:389/ou=People,o=JNDITutorial");
try {
LdapContext ctx = new InitialLdapContext(env, null);
// Activate paged results
int pageSize = 5;
byte[] cookie = null;
ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize,
Control.NONCRITICAL) });
int total;
do {
/* perform the search */
NamingEnumeration results = ctx.search("", "(objectclass=*)",
new SearchControls());
/* for each entry print out name + all attrs and values */
while (results != null && results.hasMore()) {
SearchResult entry = (SearchResult) results.next();
System.out.println(entry.getName());
}
// Examine the paged results control response
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
total = prrc.getResultSize();
if (total != 0) {
System.out.println("***************** END-OF-PAGE "
+ "(total : " + total + ") *****************\n");
} else {
System.out.println("***************** END-OF-PAGE "
+ "(total: unknown) ***************\n");
}
cookie = prrc.getCookie();
}
}
} else {
System.out.println("No controls were sent from the server");
}
// Re-activate paged results
ctx.setRequestControls(new Control[] { new PagedResultsControl(
pageSize, cookie, Control.CRITICAL) });
} while (cookie != null);
ctx.close();
示例复制自 here .
关于java - LDAP:如何返回超过 1000 个结果 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311765/