X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=ves-nf-oam-adopter%2Fves-nf-oam-adopter-pm-sb-rest-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fpm%2Fsb%2Frest%2Fclient%2Fhttp%2FTokenHandler.java;fp=ves-nf-oam-adopter%2Fves-nf-oam-adopter-pm-sb-rest-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fo%2Fran%2Foam%2Fnf%2Foam%2Fadopter%2Fpm%2Fsb%2Frest%2Fclient%2Fhttp%2FTokenHandler.java;h=26c62b597e52ab8158dbb2580c43cdcf11682eca;hb=5868c88405a5057923095e84b225adc23feef84b;hp=0000000000000000000000000000000000000000;hpb=3d81c2eabbdd3e5187a824fc5d2dff4e58f62a68;p=oam%2Fnf-oam-adopter.git diff --git a/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 b/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 new file mode 100644 index 0000000..26c62b5 --- /dev/null +++ b/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 @@ -0,0 +1,53 @@ +package org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.http; + +import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE; + +import com.google.gson.Gson; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.concurrent.ExecutionException; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; +import org.apache.hc.client5.http.async.methods.SimpleHttpRequests; +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.message.StatusLine; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.TokenGenerationException; +import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.Adapter; +import org.o.ran.oam.nf.oam.adopter.pm.sb.rest.client.pojos.TokenResponse; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class TokenHandler { + + public static final Gson GSON = new Gson(); + public static final String HTTPS = "https://"; + + /** + * Request Token under defined endpoint. + */ + public static synchronized String returnToken(final CloseableHttpAsyncClient client, final String tokenEndpoint, + final Adapter adapter) throws ExecutionException, InterruptedException { + final String host = HTTPS + adapter.getHostIpAddress(); + final SimpleHttpRequest request = SimpleHttpRequests.post(host + tokenEndpoint); + final String basicAuth = Base64.getEncoder().encodeToString( + (adapter.getUsername() + ":" + adapter.getPassword()).getBytes(StandardCharsets.UTF_8)); + request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth); + request.addHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE); + request.addHeader(HttpHeaders.ACCEPT, APPLICATION_JSON_VALUE); + + final SimpleHttpResponse response = client.execute(request, null).get(); + final String statusLine = new StatusLine(response).toString(); + if (response.getCode() != HttpStatus.SC_OK) { + throw new TokenGenerationException("Failed to obtain a token for host " + host + ": " + statusLine); + } + final String output = response.getBody().getBodyText(); + if (output.isEmpty()) { + throw new TokenGenerationException( + "Failed to obtain a token for host " + host + ", empty output: " + statusLine); + } + return GSON.fromJson(output, TokenResponse.class).getToken(); + } +}