View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.jexl;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Optional;
25  import java.util.stream.Collectors;
26  import org.apache.commons.lang3.StringUtils;
27  import org.identityconnectors.framework.common.objects.Attribute;
28  import org.identityconnectors.framework.common.objects.ConnectorObject;
29  
30  /**
31   * Utility functions for usage with JEXL engine.
32   */
33  public class SyncopeJexlFunctions {
34  
35      /**
36       * Converts realm's full path into the equivalent DN.
37       *
38       * Example: {@code /a/b/c} becomes {@code ou=c,ou=b,ou=a}.
39       *
40       * @param fullPath realm's full path
41       * @param attr attribute name for DN
42       * @return DN equivalent of the provided full path
43       */
44      public String fullPath2Dn(final String fullPath, final String attr) {
45          return fullPath2Dn(fullPath, attr, StringUtils.EMPTY);
46      }
47  
48      /**
49       * Converts realm's full path into the equivalent DN.
50       *
51       * Example: {@code /a/b/c} becomes {@code ,ou=c,ou=b,ou=a}, when {@code prefix} is
52       * {@code ","}
53       *
54       * @param fullPath realm's full path
55       * @param attr attribute name for DN
56       * @param prefix result's prefix
57       * @return DN equivalent of the provided full path
58       */
59      public String fullPath2Dn(final String fullPath, final String attr, final String prefix) {
60          String[] fullPathSplitted = fullPath.split("/");
61          if (fullPathSplitted.length <= 1) {
62              return StringUtils.EMPTY;
63          }
64  
65          List<String> headless = Arrays.asList(fullPathSplitted).subList(1, fullPathSplitted.length);
66          Collections.reverse(headless);
67          return prefix + attr + "=" + headless.stream().collect(Collectors.joining("," + attr + "="));
68      }
69  
70      /**
71       * Extracts the values of the attribute with given name from the given connector object, or empty list if not found.
72       *
73       * @param connObj connector object
74       * @param name attribute name
75       * @return the values of the attribute with given name from the given connector object, or empty list if not found
76       */
77      public List<Object> connObjAttrValues(final ConnectorObject connObj, final String name) {
78          return Optional.ofNullable(connObj).
79                  flatMap(obj -> Optional.ofNullable(obj.getAttributeByName(name)).
80                  map(Attribute::getValue)).
81                  orElse(List.of());
82      }
83  }