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.client.console.rest;
20  
21  import com.fasterxml.jackson.databind.json.JsonMapper;
22  import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
23  import java.util.List;
24  import javax.ws.rs.core.MediaType;
25  import javax.ws.rs.core.Response;
26  import org.apache.cxf.jaxrs.client.WebClient;
27  import org.apache.syncope.client.console.SyncopeWebApplication;
28  import org.apache.syncope.client.ui.commons.rest.RestClient;
29  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
30  import org.apache.syncope.common.lib.AMSession;
31  import org.apache.syncope.common.lib.SyncopeClientException;
32  import org.apache.syncope.common.lib.types.ClientExceptionType;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  public abstract class AMSessionRestClient implements RestClient {
37  
38      private static final long serialVersionUID = 17371816842780L;
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(AMSessionRestClient.class);
41  
42      protected static final List<?> JAX_RS_PROVIDERS =
43              List.of(new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build()));
44  
45      protected final List<NetworkService> instances;
46  
47      protected AMSessionRestClient(final List<NetworkService> instances) {
48          this.instances = instances;
49      }
50  
51      protected abstract String getActuatorEndpoint();
52  
53      public abstract List<AMSession> list();
54  
55      public void delete(final String key) {
56          SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Unknown);
57  
58          try {
59              Response response = WebClient.create(
60                      getActuatorEndpoint(),
61                      SyncopeWebApplication.get().getAnonymousUser(),
62                      SyncopeWebApplication.get().getAnonymousKey(),
63                      null).
64                      path(key).
65                      accept(MediaType.APPLICATION_JSON_TYPE).type(MediaType.APPLICATION_JSON_TYPE).delete();
66              if (response.getStatus() != Response.Status.OK.getStatusCode()
67                      && response.getStatus() != Response.Status.NO_CONTENT.getStatusCode()) {
68  
69                  LOG.error("Unexpected response when deleting SSO Session {} from {}: {}",
70                          key, getActuatorEndpoint(), response.getStatus());
71                  sce.getElements().add("Unexpected response code: " + response.getStatus());
72              }
73          } catch (Exception e) {
74              LOG.error("Could not delete SSO Session {} from {}",
75                      key, getActuatorEndpoint(), e);
76              sce.getElements().add("Unexpected error: " + e.getMessage());
77          }
78  
79          if (!sce.getElements().isEmpty()) {
80              throw sce;
81          }
82      }
83  }