java - LDAP:如何返回超过 1000 个结果 (java)

标签 java ldap unboundid-ldap-sdk

我正在使用来自此站点的 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/

相关文章:

java - 设置 Spring Data LDAP Embedded 以使用基本 DN 进行测试

java - 多行循环体什么时候不需要花括号?

java - 如果程序在n秒后没有返回任何内容,如何调用java中的函数

java - 为什么在 LinkedHashMap 中通过桶迭代比 HashMap 更快?

spring - Spring Security LDAP VS CAS VS OpenID 的区别

ldap - LDIF 文件错误?无效的格式?

java - 使用未绑定(bind) ID 通过 SSL 绑定(bind)到 Active Directory

java - 使用 UnboundID LDAP sdk 进行匿名绑定(bind)

java - 验证是否从 Node.js 安装了 Java

c# - 如何将字符串转换为 OctetString (C#)?