我一直在寻找使用 Apache Directory API 执行分页搜索的信息,但我没有找到任何示例或任何有关如何使用正确的 PagedResults 控件构建 SearchRequest 然后执行搜索的信息。
你们有什么建议吗?或者知道哪里可以找到此类信息?
或者,也许你应该推荐我使用其他一些 API,比如 unboundid sdk
预先感谢并致以亲切的问候。
今天,我努力使 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)