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.java.utils;
20  
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Optional;
26  import java.util.Set;
27  import java.util.concurrent.ConcurrentHashMap;
28  import java.util.stream.Stream;
29  import org.apache.commons.lang3.ArrayUtils;
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.syncope.common.lib.to.Item;
32  import org.apache.syncope.common.lib.to.Mapping;
33  import org.apache.syncope.common.lib.to.Provision;
34  import org.apache.syncope.common.lib.types.MappingPurpose;
35  import org.apache.syncope.core.persistence.api.entity.Implementation;
36  import org.apache.syncope.core.provisioning.api.data.ItemTransformer;
37  import org.apache.syncope.core.provisioning.api.data.JEXLItemTransformer;
38  import org.apache.syncope.core.provisioning.java.data.JEXLItemTransformerImpl;
39  import org.apache.syncope.core.spring.ApplicationContextProvider;
40  import org.apache.syncope.core.spring.implementation.ImplementationManager;
41  import org.identityconnectors.framework.common.objects.Name;
42  import org.identityconnectors.framework.common.objects.OperationOptions;
43  import org.identityconnectors.framework.common.objects.OperationOptionsBuilder;
44  import org.identityconnectors.framework.common.objects.OperationalAttributes;
45  import org.identityconnectors.framework.common.objects.Uid;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  import org.springframework.beans.factory.support.AbstractBeanDefinition;
49  
50  public final class MappingUtils {
51  
52      private static final Logger LOG = LoggerFactory.getLogger(MappingUtils.class);
53  
54      private static final Map<String, ItemTransformer> PER_CONTEXT_ITEM_TRANSFORMERS = new ConcurrentHashMap<>();
55  
56      public static Optional<Item> getConnObjectKeyItem(final Provision provision) {
57          Mapping mapping = null;
58          if (provision != null) {
59              mapping = provision.getMapping();
60          }
61  
62          return mapping == null
63                  ? Optional.empty()
64                  : mapping.getConnObjectKeyItem();
65      }
66  
67      public static Stream<Item> getPropagationItems(final Stream<Item> items) {
68          return items.filter(
69                  item -> item.getPurpose() == MappingPurpose.PROPAGATION || item.getPurpose() == MappingPurpose.BOTH);
70      }
71  
72      public static Stream<Item> getPullItems(final Stream<Item> items) {
73          return items.filter(
74                  item -> item.getPurpose() == MappingPurpose.PULL || item.getPurpose() == MappingPurpose.BOTH);
75      }
76  
77      public static List<ItemTransformer> getItemTransformers(
78              final Item item,
79              final List<Implementation> transformers) {
80  
81          List<ItemTransformer> result = new ArrayList<>();
82  
83          // First consider the JEXL transformation expressions
84          if (StringUtils.isNotBlank(item.getPropagationJEXLTransformer())
85                  || StringUtils.isNotBlank(item.getPullJEXLTransformer())) {
86  
87              JEXLItemTransformer jexlTransformer = (JEXLItemTransformer) ApplicationContextProvider.getBeanFactory().
88                      createBean(JEXLItemTransformerImpl.class, AbstractBeanDefinition.AUTOWIRE_BY_NAME, false);
89  
90              jexlTransformer.setPropagationJEXL(item.getPropagationJEXLTransformer());
91              jexlTransformer.setPullJEXL(item.getPullJEXLTransformer());
92              result.add(jexlTransformer);
93          }
94  
95          // Then other custom transformers
96          transformers.forEach(impl -> {
97              try {
98                  result.add(ImplementationManager.build(
99                          impl,
100                         () -> PER_CONTEXT_ITEM_TRANSFORMERS.get(impl.getKey()),
101                         instance -> PER_CONTEXT_ITEM_TRANSFORMERS.put(impl.getKey(), instance)));
102             } catch (Exception e) {
103                 LOG.error("While building {}", impl, e);
104             }
105         });
106 
107         return result;
108     }
109 
110     /**
111      * Build options for requesting all mapped connector attributes.
112      *
113      * @param items items
114      * @param moreAttrsToGet additional attributes to get
115      * @return options for requesting all mapped connector attributes
116      * @see OperationOptions
117      */
118     public static OperationOptions buildOperationOptions(
119             final Stream<Item> items,
120             final String... moreAttrsToGet) {
121 
122         OperationOptionsBuilder builder = new OperationOptionsBuilder();
123 
124         Set<String> attrsToGet = new HashSet<>();
125         attrsToGet.add(Name.NAME);
126         attrsToGet.add(Uid.NAME);
127         attrsToGet.add(OperationalAttributes.ENABLE_NAME);
128         if (!ArrayUtils.isEmpty(moreAttrsToGet)) {
129             attrsToGet.addAll(List.of(moreAttrsToGet));
130         }
131 
132         items.filter(item -> item.getPurpose() != MappingPurpose.NONE).
133                 forEach(item -> attrsToGet.add(item.getExtAttrName()));
134 
135         builder.setAttributesToGet(attrsToGet);
136         // -------------------------------------
137 
138         return builder.build();
139     }
140 
141     /**
142      * Private default constructor, for static-only classes.
143      */
144     private MappingUtils() {
145     }
146 }