【问题标题】:Descending sorting LDAP with Spring Boot使用 Spring Boot 降序排序 LDAP
【发布时间】:2019-07-03 03:29:02
【问题描述】:

是否可以使用 SortControlDirContextProcessor 降序而不是升序对 LDAP 目录中的记录进行排序?

我基于此: SpringLdap - LdapTemplateSortedSearchITest

public void testSearch_SortControl_ConvenienceMethod() {
    SortControlDirContextProcessor requestControl;

    // Prepare for first search
    requestControl = new SortControlDirContextProcessor("cn");
    tested.search(BASE, FILTER_STRING, searchControls, callbackHandler,
            requestControl);
    int resultCode = requestControl.getResultCode();
    boolean sorted = requestControl.isSorted();
    assertThat("Search result should have been sorted: " + resultCode, sorted).isTrue();
    List list = callbackHandler.getList();
    assertSortedList(list);
}

它有效但上升。如何设置降序排列?

【问题讨论】:

    标签: java sorting spring-boot ldap spring-ldap


    【解决方案1】:

    我相信它会有所帮助:

    公共 SortKey(字符串 attrID, 布尔升序, 字符串匹配RuleID)

    为属性创建排序键。条目将根据指定的属性按照指定的排序顺序并使用指定的匹配规则(如果提供)进行排序。

    参数: attrID - 用作排序键的属性的非空 ID。


    ascendingOrder - 如果为 true,则条目按升序排列。否则按降序排列。


    ma​​tchingRuleID - 用于对属性值进行排序的匹配规则的可能为空的 ID。如果未指定,则使用为排序键属性定义的排序匹配规则。

    来自文档:Java doc

    关于您的问题:Example code from java doc

    我发现了另一种替代方案,称为 unboundid ldap sdk link

    【讨论】:

    • 感谢您的回答。 @Echoinacup 你能解释一下我应该在哪里初始化排序键吗? SortControlDirContextProcessor 作为参数消耗字符串,而不是 SortKey。
    • 好的例子很好,但它似乎不适用于我所要求的 Spring Boot。
    【解决方案2】:

    我的解决方案是实现一个自定义 DirContextProcessor,它允许我通过使用 SortControl 的重载方法以所需方向(升序/降序)对多个属性进行排序 类,将 SortKey 的对象数组作为参数。

    实现必须扩展 AbstractFallbackRequestAndResponseControlDirContextProcessor 并覆盖 createRequestControl 方法。

    超类 AbstractFallbackRequestAndResponseControlDirContextProcessor 将负责控件的实际创建。它只需要来自子类的 2 条信息。

    1. 要实例化的控件的完全限定类名
    2. 构造函数参数的类型和值

    全限定类名在子类属性defaultRequestControl中提供,构造函数参数的类型和值在子类方法createRequestControl中提供。

    SortKey 对象的 ascendingOrder 属性中提供了任何特定属性的排序方向信息。

    public class SortMultipleControlDirContextProcessor extends AbstractFallbackRequestAndResponseControlDirContextProcessor{
    
        private SortKey[] sortKeys;
        private boolean sorted;
        private int resultCode;
    
        public SortMultipleControlDirContextProcessor(SortKey ... sortKeys){
    
            if(ArrayUtils.isEmpty(sortKeys)){
                throw new IllegalArgumentException("At least one key to sort on must be provided.");
            }
    
            this.sortKeys = sortKeys;
            this.sorted = false;
            this.resultCode = -1;
            this.defaultRequestControl = "javax.naming.ldap.SortControl";
            this.defaultResponseControl = "javax.naming.ldap.SortResponseControl";
            this.fallbackRequestControl = "com.sun.jndi.ldap.ctl.SortControl";
            this.fallbackResponseControl = "com.sun.jndi.ldap.ctl.SortResponseControl";
    
            loadControlClasses();
        }
    
        @Override
        public Control createRequestControl(){
            return super.createRequestControl(new Class[]{SortKey[].class,  boolean.class}, new Object[]{sortKeys, critical});
        }
    
        @Override
        protected void handleResponse(Object control) {
    
            Boolean result = (Boolean) invokeMethod("isSorted", responseControlClass, control);
            this.sorted = result;
    
            Integer code = (Integer) invokeMethod("getResultCode", responseControlClass, control);
            this.resultCode = code;
        }
    
        public SortKey[] getSortKeys(){
            return sortKeys;
        }
    
        public boolean isSorted(){
            return sorted;
        }
    
        public int getResultCode(){
            return resultCode;
        }
    }
    

    实现后,您可以使用该类对多个属性的结果进行任意方向的排序:

    // SortKey for sorting results on the cn attribute in descending order
    SortKey cnSortKey = new SortKey("cn", false, null);
    
    // Instantiate the control
    SortMultipleControlDirContextProcessor myCustomControl = new SortMultipleControlDirContextProcessor(cnSortKey);
    
    // Perform the search with the control
    List<User> users = ldapTemplate.search("", orFilter.encode(), searchControls, new UserAttributesMapper(), myCustomControl);
    

    【讨论】:

      猜你喜欢
      • 2018-01-15
      • 1970-01-01
      • 2017-07-09
      • 2013-01-10
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多