Sort Control
当 Client 端希望服务器发送排序的搜索结果时,将使用排序控件。在 Client 端需要根据某些标准对结果进行排序但自身不具备执行排序过程的能力的情况下,服务器端排序很有用。排序控件在RFC 2891中指定。下面的类提供支持排序控制所需的功能。
SortKey 是服务器的排序键列表,服务器根据该列表对结果进行排序。
如何使用排序控制?
以下示例说明了执行搜索的 Client 端之间的 Client 端-服务器交互,该搜索请求基于属性“ cn”进行服务器端排序。
- Client 发送搜索请求
// Activate sorting
String sortKey = "cn";
ctx.setRequestControls(new Control[] {
new SortControl(sortKey, Control.CRITICAL) });
// Perform a search
NamingEnumeration results =
ctx.search("", "(objectclass=*)", new SearchControls());
- 服务器以基于“ cn”属性及其对应的匹配规则排序的条目作为响应。
// Iterate over sorted search results
while (results != null && results.hasMore()) {
// Display an entry
SearchResult entry = (SearchResult)results.next();
System.out.println(entry.getName());
// Handle the entry's response controls (if any)
if (entry instanceof HasControls) {
// ((HasControls)entry).getControls();
}
}
// Examine the sort control response
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof SortResponseControl) {
SortResponseControl src = (SortResponseControl)controls[i];
if (! src.isSorted()) {
throw src.getException();
}
} else {
// Handle other response controls (if any)
}
}
}
完整的 JNDI 示例可以找到here。
注意: Oracle 目录服务器和 Windows Active Directory 服务器都支持排序控件。