排序控制

原文: https://docs.oracle.com/javase/tutorial/jndi/newstuff/sort.html

当客户端希望服务器发送已排序的搜索结果时,使用排序控件。服务器端排序在客户端需要根据某些条件对结果进行排序但无法单独执行排序过程的情况下非常有用。排序控制在 RFC 2891 中指定。下面的类提供了支持排序控制所需的功能。

排序键是一个有序的键列表,服务器根据该列表对结果进行排序。

如何使用排序控制?

下面的示例说明了执行请求基于属性“cn”的服务器端排序的搜索的客户端之间的客户端 - 服务器交互。

  1. 客户端发送询问

    1. // Activate sorting
    2. String sortKey = "cn";
    3. ctx.setRequestControls(new Control[] {
    4. new SortControl(sortKey, Control.CRITICAL) });
    5. // Perform a search
    6. NamingEnumeration results =
    7. ctx.search("", "(objectclass=*)", new SearchControls());

    的搜索请求

  2. 服务器使用基于“cn”属性及其对应的匹配规则排序的条目进行响应。

    1. // Iterate over sorted search results
    2. while (results != null && results.hasMore()) {
    3. // Display an entry
    4. SearchResult entry = (SearchResult)results.next();
    5. System.out.println(entry.getName());
    6. // Handle the entry's response controls (if any)
    7. if (entry instanceof HasControls) {
    8. // ((HasControls)entry).getControls();
    9. }
    10. }
    11. // Examine the sort control response
    12. Control[] controls = ctx.getResponseControls();
    13. if (controls != null) {
    14. for (int i = 0; i < controls.length; i++) {
    15. if (controls[i] instanceof SortResponseControl) {
    16. SortResponseControl src = (SortResponseControl)controls[i];
    17. if (! src.isSorted()) {
    18. throw src.getException();
    19. }
    20. } else {
    21. // Handle other response controls (if any)
    22. }
    23. }
    24. }

可以找到完整的 JNDI 示例 here


注意: Oracle Directory Server 和 Windows Active Directory 服务器都支持排序控件。