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.persistence.jpa.content;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.fasterxml.jackson.databind.json.JsonMapper;
23  import java.io.InputStream;
24  import java.util.Iterator;
25  import java.util.Map;
26  import java.util.Optional;
27  import java.util.concurrent.Callable;
28  import javax.sql.DataSource;
29  import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
30  import org.apache.syncope.core.persistence.api.content.ConfParamLoader;
31  import org.apache.syncope.core.spring.ApplicationContextProvider;
32  import org.apache.syncope.core.spring.security.AuthContextUtils;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.transaction.annotation.Transactional;
36  
37  /**
38   * Initialize Keymaster with default content if no data is present already.
39   */
40  public class KeymasterConfParamLoader implements ConfParamLoader {
41  
42      protected static final Logger LOG = LoggerFactory.getLogger(KeymasterConfParamLoader.class);
43  
44      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
45  
46      protected final ConfParamOps confParamOps;
47  
48      public KeymasterConfParamLoader(final ConfParamOps confParamOps) {
49          this.confParamOps = confParamOps;
50      }
51  
52      @Override
53      public int getOrder() {
54          return 450;
55      }
56  
57      @Override
58      public void load(final String domain, final DataSource datasource) {
59          AuthContextUtils.callAsAdmin(domain, new Callable<Void>() {
60  
61              @Transactional
62              @Override
63              public Void call() throws Exception {
64                  boolean existingData;
65                  try {
66                      existingData = !confParamOps.list(domain).isEmpty();
67                  } catch (Exception e) {
68                      LOG.error("[{}] Could not access Keymaster", domain, e);
69                      existingData = true;
70                  }
71  
72                  if (existingData) {
73                      LOG.info("[{}] Data found in Keymaster, leaving untouched", domain);
74                  } else {
75                      LOG.info("[{}] Empty Keymaster found, loading default content", domain);
76  
77                      try (InputStream contentJSON = ApplicationContextProvider.getBeanFactory().
78                              getBean(domain + "KeymasterConfParamsJSON", InputStream.class)) {
79  
80                          JsonNode content = MAPPER.readTree(contentJSON);
81                          for (Iterator<Map.Entry<String, JsonNode>> itor = content.fields(); itor.hasNext();) {
82                              Map.Entry<String, JsonNode> param = itor.next();
83                              Optional.ofNullable(MAPPER.treeToValue(param.getValue(), Object.class)).
84                                      ifPresent(value -> confParamOps.set(domain, param.getKey(), value));
85                          }
86                      } catch (Exception e) {
87                          LOG.error("[{}] While loading default Keymaster content", domain, e);
88                      }
89                  }
90  
91                  return null;
92              }
93          });
94      }
95  }