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.common.keymaster.client.self;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Map;
27  import javax.ws.rs.core.Response;
28  import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
29  import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
30  import org.apache.syncope.common.keymaster.client.api.KeymasterException;
31  import org.apache.syncope.common.keymaster.rest.api.service.ConfParamService;
32  import org.apache.syncope.common.rest.api.RESTHeaders;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public class SelfKeymasterConfParamOps extends SelfKeymasterOps implements ConfParamOps {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(ConfParamOps.class);
39  
40      private static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
41  
42      public SelfKeymasterConfParamOps(final JAXRSClientFactoryBean clientFactory) {
43          super(clientFactory);
44      }
45  
46      @Override
47      public Map<String, Object> list(final String domain) {
48          return client(ConfParamService.class, Map.of(RESTHeaders.DOMAIN, domain)).list();
49      }
50  
51      @Override
52      public <T> T get(final String domain, final String key, final T defaultValue, final Class<T> reference) {
53          Response response = client(ConfParamService.class, Map.of(RESTHeaders.DOMAIN, domain)).get(key);
54          if (response.getStatus() != Response.Status.OK.getStatusCode()) {
55              return defaultValue;
56          }
57          try {
58              return MAPPER.readValue(response.readEntity(InputStream.class), reference);
59          } catch (IOException e) {
60              LOG.error("Could not deserialize response", e);
61              return defaultValue;
62          }
63      }
64  
65      @Override
66      public <T> void set(final String domain, final String key, final T value) {
67          if (value == null) {
68              remove(domain, key);
69          } else {
70              try {
71                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
72                  MAPPER.writeValue(baos, value);
73  
74                  client(ConfParamService.class, Map.of(RESTHeaders.DOMAIN, domain)).
75                          set(key, new ByteArrayInputStream(baos.toByteArray()));
76              } catch (IOException e) {
77                  throw new KeymasterException("Could not serialize " + value, e);
78              }
79          }
80      }
81  
82      @Override
83      public void remove(final String domain, final String key) {
84          client(ConfParamService.class, Map.of(RESTHeaders.DOMAIN, domain)).remove(key);
85      }
86  }