zoukankan      html  css  js  c++  java
  • LDAP的SizeLimitExceededException

    LDAP.search()当查询的数据较多时,数据条目大于LDAP服务器设置的最多数据时,就会出现SizeLimitExceededException。

    解决方法之一是分页查询,控制每次查询的数目。

    	public void getAllPerson() throws NamingException, IOException {
    		SearchControls schCtrls = new SearchControls();
    		// 返回属性
    		String[] returnAttrs = { "userPrincipalName", "distinguishedName" };
    		schCtrls.setReturningAttributes(returnAttrs);
    
    		schCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
    		int pageSize = 100;
    		byte[] cookie = null;
    
    		ContextSource contextSource = ldapTemplate.getContextSource();
    		DirContext ctx = contextSource.getReadWriteContext();
    		LdapContext lCtx = (LdapContext) ctx;
    		lCtx.setRequestControls(new Control[] { new PagedResultsControl(
    				pageSize, Control.CRITICAL) });
    		int totalResults = 0;
    		do {
    			AndFilter andF = new AndFilter();
    			andF.and(new EqualsFilter("objectclass", "person")).and(
    					new LikeFilter("userPrincipalName", "*"));
    
    			NamingEnumeration<SearchResult> results = lCtx.search("",
    					andF.toString(), schCtrls);
    			while (results != null && results.hasMoreElements()) {
    				SearchResult sr = results.next();
    				Attributes attrs = sr.getAttributes();
    				System.out.println(attrs.get("userPrincipalName").get());
    				System.out.println(attrs.get("distinguishedName").get());
    				totalResults++;
    			}
    			cookie = parseControls(lCtx.getResponseControls());
    			lCtx.setRequestControls(new Control[] { new PagedResultsControl(
    					pageSize, cookie, Control.CRITICAL) });
    		} while ((cookie != null) && (cookie.length != 0));
    		lCtx.close();
    		System.out.println("Total" + totalResults);
    	}
    
    	private static byte[] parseControls(Control[] controls)
    			throws NamingException {
    		byte[] cookie = null;
    		if (controls != null) {
    			for (int i = 0; i < controls.length; i++) {
    				if (controls[i] instanceof PagedResultsResponseControl) {
    					PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
    					cookie = prrc.getCookie();
    					System.out.println(">>Next Page \n");
    				}
    			}
    		}
    		return (cookie == null) ? new byte[0] : cookie;
    	}


    其中的cookie是告诉服务器目前已获得的条数,便于下页获取。

  • 相关阅读:
    treeview十八般武艺,js选择和绑定权限树
    开源WebOS
    公交车路线查询系统后台数据库设计
    网页信息抓取
    一步一步打造WebIM(3)——性能测试
    WebBrowser介绍——Javascript与C++互操作
    .NET文档生成工具ADB[更新至2.3]
    一步一步打造WebIM(4)——Comet的特殊之处
    在SQL Server中对视图进行增删改
    开源企业即时通讯和在线客服
  • 原文地址:https://www.cnblogs.com/whuqin/p/4982047.html
Copyright © 2011-2022 走看看