排序控制
原文: https://docs.oracle.com/javase/tutorial/jndi/newstuff/sort.html
当客户端希望服务器发送已排序的搜索结果时,使用排序控件。服务器端排序在客户端需要根据某些条件对结果进行排序但无法单独执行排序过程的情况下非常有用。排序控制在 RFC 2891 中指定。下面的类提供了支持排序控制所需的功能。
排序键是一个有序的键列表,服务器根据该列表对结果进行排序。
如何使用排序控制?
下面的示例说明了执行请求基于属性“cn”的服务器端排序的搜索的客户端之间的客户端 - 服务器交互。
客户端发送询问
// 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 Directory Server 和 Windows Active Directory 服务器都支持排序控件。