JNDI-LDAP分页 [英] JNDI-LDAP paging

查看:136
本文介绍了JNDI-LDAP分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在此处.问题是我需要公开一个看起来像这样的API:getUsers(pageSize, pageNumber),它与JNDI/LDAP进行分页的方式(与每次传递给搜索方法的cookie都不能很好地配合).代码如下:

I managed to get pagination working like described here. The problem is I need to expose an API which would look like this:getUsers(pageSize, pageNumber), which does not really go well with the way JNDI/LDAP does the paging(with a cookie you pass each time to the search method). The code looks like this:

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;
}

似乎我需要遍历所有页面才能获得所需的页面.而且,我需要遍历页面上的所有条目,才能获得下一页.

It seems I need to iterate through all the pages to get the required page. Moreover, I need to iterate through all the entries on a page, to be able to get the next page.

有更好的方法吗?我担心性能问题.

Is there a better way? I worry about performance issues.

推荐答案

有些东西叫做虚拟列表视图"控件.几个LDAP服务器都支持它.不知道该实现是否仍在JNDI中.如果没有,您可以考虑自己实施.您必须将其与服务器端排序一起使用.

There is something called "Virtual List View" control. It is supported by a couple of LDAP servers. Not sure if the implementation is still in the JNDI. If not, you may consider to implement it yourself. You have to use it together with server side sorting.

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

这篇关于JNDI-LDAP分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆