【问题标题】:JNDI-LDAP pagingJNDI-LDAP 分页
【发布时间】:2011-04-21 14:46:53
【问题描述】:

我设法让分页像here 描述的那样工作。问题是我需要公开一个看起来像这样的 API:getUsers(pageSize, pageNumber),这与 JNDI/LDAP 进行分页的方式并不相符(每次都将 cookie 传递给搜索方法)。代码如下所示:

private NamingEnumeration ldapPagedSearch(String filter, int pageSize, int pageNumber){
    InitialLdapContext ctx = getInitialContext();

    //TODO: get the id also, need to spec it in UI
    // Create the search controls
    SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    //keep a session
    byte[] cookie = null;

    //Request the paged results control
    Control[] ctls = new Control[]{new PagedResultsControl(pageSize, true)};
    ctx.setRequestControls(ctls);

    //Specify the search scope
    NamingEnumeration results = null;
    int currentPage = 1;
    do {
        results = ctx.search(getConfiguration().get(BASEDN_KEY), filter, searchCtls);

        //we got to the right page, return this page
        if(currentPage == pageNumber) {
            return results;
        }

        // loop through this page, because we cannot get a proper cookie otherwise
        // WARN: this could be a problem of performance
        while (results.hasMore()) results.next();

        // examine the paged results control response
        Control[] controls = ctx.getResponseControls();
        if (controls != null) {
            for (Control control : controls) {
                if (control instanceof PagedResultsResponseControl) {
                    cookie = ((PagedResultsResponseControl) control).getCookie();
                } 
            }
        }

        // pass the cookie back to the server for the next page
        ctx.setRequestControls(new Control[]{new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });

        //increment page
        currentPage++;
    } while (cookie != null);


    ctx.close();

    //if we get here, means it is an empty set(consumed by the inner loop)
    return results;
}

看来我需要遍历所有页面才能获得所需的页面。此外,我需要遍历页面上的所有条目,才能获得下一页。

有没有更好的方法?我担心性能问题。

【问题讨论】:

    标签: ldap paging jndi


    【解决方案1】:

    有一种叫做“虚拟列表视图”的控件。它由几个 LDAP 服务器支持。不确定实现是否仍在 JNDI 中。如果没有,您可以考虑自己实现它。您必须将它与服务器端排序一起使用。

    另请参阅 https://datatracker.ietf.org/doc/html/draft-ietf-ldapext-ldapv3-vlv-04http://www.cs.rit.edu/usr/local/pub/jeh/rit/java/lib/doc/ldapcontrols/com/sun/jndi/ldap/ctl/VirtualListViewControl.html

    【讨论】:

      【解决方案2】:

      你是对的。 API 不会凝固。您需要重新设计您应该交付的 API。

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        相关资源
        最近更新 更多