1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.lib;
20
21 import com.fasterxml.jackson.databind.JsonNode;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Base64;
25 import javax.ws.rs.core.HttpHeaders;
26 import javax.ws.rs.core.MediaType;
27 import org.apache.commons.lang3.StringUtils;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.apache.cxf.configuration.jsse.TLSClientParameters;
30 import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
31 import org.apache.cxf.jaxrs.client.WebClient;
32 import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
33 import org.apache.syncope.common.lib.info.NumbersInfo;
34 import org.apache.syncope.common.lib.info.PlatformInfo;
35 import org.apache.syncope.common.lib.info.SystemInfo;
36 import org.apache.syncope.common.rest.api.RESTHeaders;
37
38 public class SyncopeAnonymousClient extends SyncopeClient {
39
40 protected final AnonymousAuthenticationHandler anonymousAuthHandler;
41
42 public SyncopeAnonymousClient(
43 final MediaType mediaType,
44 final JAXRSClientFactoryBean restClientFactory,
45 final RestClientExceptionMapper exceptionMapper,
46 final AnonymousAuthenticationHandler anonymousAuthHandler,
47 final boolean useCompression,
48 final HTTPClientPolicy httpClientPolicy,
49 final TLSClientParameters tlsClientParameters) {
50
51 super(
52 mediaType,
53 restClientFactory,
54 exceptionMapper,
55 anonymousAuthHandler,
56 useCompression,
57 httpClientPolicy,
58 tlsClientParameters);
59 this.anonymousAuthHandler = anonymousAuthHandler;
60 }
61
62 protected JsonNode info() throws IOException {
63 WebClient webClient = WebClient.create(
64 restClientFactory.getAddress().replace("/rest", "/actuator/info")).
65 accept(MediaType.APPLICATION_JSON_TYPE).
66 header(RESTHeaders.DOMAIN, getDomain()).
67 header(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(
68 (anonymousAuthHandler.getUsername() + ":" + anonymousAuthHandler.getPassword()).getBytes()));
69
70 return MAPPER.readTree((InputStream) webClient.get().getEntity());
71 }
72
73 public Pair<String, String> gitAndBuildInfo() {
74 try {
75 JsonNode info = info();
76 return Pair.of(
77 info.has("git") ? info.get("git").get("commit").get("id").asText() : StringUtils.EMPTY,
78 info.get("build").get("version").asText());
79 } catch (IOException e) {
80 throw new RuntimeException("While getting build and git Info", e);
81 }
82 }
83
84 public PlatformInfo platform() {
85 try {
86 return MAPPER.treeToValue(info().get("platform"), PlatformInfo.class);
87 } catch (IOException e) {
88 throw new RuntimeException("While getting Platform Info", e);
89 }
90 }
91
92 public SystemInfo system() {
93 try {
94 return MAPPER.treeToValue(info().get("system"), SystemInfo.class);
95 } catch (IOException e) {
96 throw new RuntimeException("While getting System Info", e);
97 }
98 }
99
100 public NumbersInfo numbers() {
101 try {
102 return MAPPER.treeToValue(info().get("numbers"), NumbersInfo.class);
103 } catch (IOException e) {
104 throw new RuntimeException("While getting Numbers Info", e);
105 }
106 }
107 }