Apache Directory LDAP - 分页搜索

时间:2013-07-31 07:17:35

标签: search ldap apache-directory

我正在寻找使用Apache Directory API执行分页搜索的信息,但我没有找到任何关于如何使用正确的PagedResults控件构建SearchRequest然后执行搜索的示例或任何信息。

你们有什么小费吗?或者知道在哪里可以找到这样的信息?

或者,也许你应该建议我使用其他一些API,比如unboundid sdk

提前致谢并致以亲切的问候。

3 个答案:

答案 0 :(得分:7)

今天我努力从kayyagara工作中制作链接的例子。

它有一些问题:

  • 需要一个特殊的系统变量才能完成这项工作
  • 坏饼干打破了整个事情
  • 不需要某些设置/代码行

以下是工作示例:

        //Without this you get a class Cast Exception:
        //java.lang.ClassCastException: org.apache.directory.api.ldap.codec.BasicControlDecorator cannot be cast to org.apache.directory.api.ldap.model.message.controls.PagedResults
        System.setProperty(StandaloneLdapApiService.CONTROLS_LIST,
            PagedResultsFactory.class.getName());

        PagedResults pagedSearchControl = new PagedResultsDecorator(
                connection.getCodecService());
        pagedSearchControl.setSize(300);

        // Loop over all the elements
        List<Entry> results = new ArrayList<Entry>();
        boolean hasUnwillingToPerform = false;

        //inspired by http://markmail.org/message/43qjepg6shvfvqud
        while (true) {
            EntryCursor cursor = null;

            try {
                SearchRequest searchRequest = new SearchRequestImpl();
                searchRequest.setBase(new Dn(searchRoot));
                searchRequest.setFilter(searchFilter);
                searchRequest.setScope(SearchScope.SUBTREE);
                searchRequest.addAttributes("*");
                searchRequest.addControl(pagedSearchControl);

                cursor = new EntryCursorImpl(
                        connection.search(searchRequest));

                while (cursor.next()) {
                    Entry result = cursor.get();
                    results.add(result);
                }

                SearchResultDone result = cursor.getSearchResultDone();
                pagedSearchControl = (PagedResults) result
                        .getControl(PagedResults.OID);

                if (result.getLdapResult().getResultCode() == ResultCodeEnum.UNWILLING_TO_PERFORM) {
                    hasUnwillingToPerform = true;
                    break;
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

            // check if this is over
            byte[] cookie = pagedSearchControl.getCookie();

            if (Strings.isEmpty(cookie)) {
                // If so, exit the loop
                break;
            }

            // Prepare the next iteration
            pagedSearchControl.setSize(300);
        }

        if (hasUnwillingToPerform) {
            throw new IllegalStateException("AD can't handle paging");
        }

        // Cleanup the session
        connection.unBind();
        connection.close();

答案 1 :(得分:5)

看看这个http://markmail.org/message/43qjepg6shvfvqud OTOH,总是建议发布ApacheDS相关问题 用户邮件列表可以快速回复,我们可能并不总是监控SO

答案 2 :(得分:1)

file包含RFC2696中描述的简单分页结果控件扩展的演示。要编译和运行,需要UnboundID LDAP SDK

另见