26c62b597e52ab8158dbb2580c43cdcf11682eca
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-sb-rest-client / src / main / java / org / o / ran / oam / nf / oam / adopter / pm / sb / rest / client / http / TokenHandler.java
1 package org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.http;
2
3 import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
4
5 import com.google.gson.Gson;
6 import java.nio.charset.StandardCharsets;
7 import java.util.Base64;
8 import java.util.concurrent.ExecutionException;
9 import lombok.AccessLevel;
10 import lombok.NoArgsConstructor;
11 import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
12 import org.apache.hc.client5.http.async.methods.SimpleHttpRequests;
13 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
14 import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
15 import org.apache.hc.core5.http.HttpHeaders;
16 import org.apache.hc.core5.http.HttpStatus;
17 import org.apache.hc.core5.http.message.StatusLine;
18 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.TokenGenerationException;
19 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter;
20 import org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.pojos.TokenResponse;
21
22 @NoArgsConstructor(access = AccessLevel.PRIVATE)
23 public final class TokenHandler {
24
25     public static final Gson GSON = new Gson();
26     public static final String HTTPS = "https://";
27
28     /**
29      * Request Token under defined endpoint.
30      */
31     public static synchronized String returnToken(final CloseableHttpAsyncClient client, final String tokenEndpoint,
32             final Adapter adapter) throws ExecutionException, InterruptedException {
33         final String host = HTTPS + adapter.getHostIpAddress();
34         final SimpleHttpRequest request = SimpleHttpRequests.post(host + tokenEndpoint);
35         final String basicAuth = Base64.getEncoder().encodeToString(
36                 (adapter.getUsername() + ":" + adapter.getPassword()).getBytes(StandardCharsets.UTF_8));
37         request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth);
38         request.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE);
39         request.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON_VALUE);
40
41         final SimpleHttpResponse response = client.execute(request, null).get();
42         final String statusLine = new StatusLine(response).toString();
43         if (response.getCode() != HttpStatus.SC_OK) {
44             throw new TokenGenerationException("Failed to obtain a token for host " + host + ": " + statusLine);
45         }
46         final String output = response.getBody().getBodyText();
47         if (output.isEmpty()) {
48             throw new TokenGenerationException(
49                     "Failed to obtain a token for host " + host + ", empty output: " + statusLine);
50         }
51         return GSON.fromJson(output, TokenResponse.class).getToken();
52     }
53 }