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是告诉服务器目前已获得的条数,便于下页获取。

 

转载于:https://www.cnblogs.com/whuqin/archive/2012/04/11/4982047.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值