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.commons.lang3.tuple.Pair;
27  import org.apache.cxf.jaxrs.client.WebClient;
28  import org.apache.syncope.client.console.SyncopeWebApplication;
29  import org.apache.syncope.client.lib.WebClientBuilder;
30  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public final class SRAStatisticsRestClient {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(SRAStatisticsRestClient.class);
37  
38      private static final List<?> JAX_RS_PROVIDERS =
39              List.of(new JacksonJsonProvider(JsonMapper.builder().findAndAddModules().build()));
40  
41      protected String getActuatorEndpoint(final List<NetworkService> instances) {
42          return instances.get(0).getAddress() + "actuator/metrics/spring.cloud.gateway.requests";
43      }
44  
45      public SRAStatistics get(final List<NetworkService> instances, final List<Pair<String, String>> selected) {
46          try {
47              WebClient client = WebClientBuilder.build(getActuatorEndpoint(instances),
48                      SyncopeWebApplication.get().getAnonymousUser(),
49                      SyncopeWebApplication.get().getAnonymousKey(),
50                      JAX_RS_PROVIDERS).accept(MediaType.APPLICATION_JSON_TYPE);
51  
52              if (!selected.isEmpty()) {
53                  client.query("tag", selected.stream().map(s -> s.getKey() + ":" + s.getValue()).toArray());
54              }
55  
56              Response response = client.get();
57              if (response.getStatus() == Response.Status.OK.getStatusCode()) {
58                  return response.readEntity(SRAStatistics.class);
59              }
60  
61              LOG.error("Unexpected response for SRA statistics from {}: {}",
62                      getActuatorEndpoint(instances), response.getStatus());
63          } catch (Exception e) {
64              LOG.error("Could not fetch SRA statistics from {}", getActuatorEndpoint(instances), e);
65          }
66  
67          return new SRAStatistics();
68      }
69  }