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.keymaster.internal;
20  
21  import com.fasterxml.jackson.core.JsonProcessingException;
22  import com.fasterxml.jackson.databind.JsonNode;
23  import com.fasterxml.jackson.databind.json.JsonMapper;
24  import java.util.Map;
25  import java.util.Optional;
26  import java.util.TreeMap;
27  import org.apache.syncope.core.persistence.api.dao.ConfParamDAO;
28  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
29  import org.apache.syncope.core.persistence.api.entity.ConfParam;
30  import org.apache.syncope.core.persistence.api.entity.SelfKeymasterEntityFactory;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  public class InternalConfParamHelper {
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(InternalConfParamHelper.class);
38  
39      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
40  
41      protected final ConfParamDAO confParamDAO;
42  
43      protected final SelfKeymasterEntityFactory entityFactory;
44  
45      public InternalConfParamHelper(final ConfParamDAO confParamDAO, final SelfKeymasterEntityFactory entityFactory) {
46          this.confParamDAO = confParamDAO;
47          this.entityFactory = entityFactory;
48      }
49  
50      @Transactional(readOnly = true)
51      public Map<String, Object> list() {
52          Map<String, Object> params = new TreeMap<>();
53          confParamDAO.findAll().forEach(param -> {
54              try {
55                  params.put(param.getKey(), MAPPER.treeToValue(param.getValue(), Object.class));
56              } catch (JsonProcessingException e) {
57                  LOG.error("While processing {}'s value", param.getKey(), e);
58              }
59          });
60          return params;
61      }
62  
63      @Transactional(readOnly = true)
64      public JsonNode get(final String key) {
65          ConfParam param = confParamDAO.find(key);
66  
67          return Optional.ofNullable(param).map(ConfParam::getValue).orElse(null);
68      }
69  
70      @Transactional
71      public void set(final String key, final JsonNode value) {
72          if (value == null) {
73              throw new NotFoundException("No value provided for " + key);
74          }
75  
76          ConfParam param = confParamDAO.find(key);
77          if (param == null) {
78              param = entityFactory.newConfParam();
79              param.setKey(key);
80          }
81          param.setValue(value);
82          confParamDAO.save(param);
83      }
84  
85      @Transactional
86      public void remove(final String key) {
87          confParamDAO.delete(key);
88      }
89  }