diff options
Diffstat (limited to 'policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java')
-rw-r--r-- | policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java | 127 |
1 files changed, 31 insertions, 96 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java index f4f0705c..130b6c15 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/internal/JerseyClient.java @@ -2,9 +2,9 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,14 @@ package org.onap.policy.common.endpoints.http.client.internal; +import com.google.re2j.Pattern; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.Invocation.Builder; +import jakarta.ws.rs.client.InvocationCallback; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; @@ -30,19 +38,13 @@ import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Future; import javax.net.ssl.SSLContext; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation.Builder; -import javax.ws.rs.client.InvocationCallback; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.Response; +import lombok.Getter; +import lombok.ToString; import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.gson.annotation.GsonJsonIgnore; import org.onap.policy.common.utils.network.NetworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,12 +52,15 @@ import org.slf4j.LoggerFactory; /** * Http Client implementation using a Jersey Client. */ +@Getter +@ToString public class JerseyClient implements HttpClient { + private static final Pattern COMMA_PAT = Pattern.compile(","); /** * Logger. */ - private static Logger logger = LoggerFactory.getLogger(JerseyClient.class); + private static final Logger logger = LoggerFactory.getLogger(JerseyClient.class); protected static final String JERSEY_DEFAULT_SERIALIZATION_PROVIDER = "org.onap.policy.common.gson.GsonMessageBodyHandler"; @@ -77,9 +82,14 @@ public class JerseyClient implements HttpClient { /** * Constructor. * - * <p>name the name https is it https or not selfSignedCerts are there self signed certs - * hostname the hostname port port being used basePath base context userName user - * password password + * <p>name - the name + * https - is it https or not + * selfSignedCerts - are there self-signed certs + * hostname - the hostname + * port - port being used + * basePath - base context + * userName - user credentials + * password - password credentials * * @param busTopicParams Input parameters object * @throws KeyManagementException key exception @@ -112,7 +122,7 @@ public class JerseyClient implements HttpClient { this.client = detmClient(); if (!StringUtils.isBlank(this.userName) && !StringUtils.isBlank(this.password)) { - HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(userName, password); + var authFeature = HttpAuthenticationFeature.basic(userName, password); this.client.register(authFeature); } @@ -127,13 +137,13 @@ public class JerseyClient implements HttpClient { private Client detmClient() throws NoSuchAlgorithmException, KeyManagementException { if (this.https) { ClientBuilder clientBuilder; - SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); + var sslContext = SSLContext.getInstance("TLSv1.2"); if (this.selfSignedCerts) { sslContext.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom()); - // This falls under self signed certs which is used for non-production testing environments where + // This falls under self-signed certs which is used for non-production testing environments where // the hostname in the cert is unlikely to be crafted properly. We always return true for the - // hostname verifier. This causes a sonar vuln but we ignore it as it could cause problems in some + // hostname verifier. This causes a sonar vuln, but we ignore it as it could cause problems in some // testing environments. clientBuilder = ClientBuilder.newBuilder().sslContext(sslContext).hostnameVerifier( @@ -158,7 +168,7 @@ public class JerseyClient implements HttpClient { private void registerSerProviders(String serializationProvider) throws ClassNotFoundException { String providers = (StringUtils.isBlank(serializationProvider) ? JERSEY_DEFAULT_SERIALIZATION_PROVIDER : serializationProvider); - for (String prov : providers.split(",")) { + for (String prov : COMMA_PAT.split(providers)) { this.client.register(Class.forName(prov)); } } @@ -195,7 +205,7 @@ public class JerseyClient implements HttpClient { @Override public Future<Response> get(InvocationCallback<Response> callback, Map<String, Object> headers) { - Builder builder = getWebTarget().request(); + var builder = getWebTarget().request(); if (headers != null) { headers.forEach(builder::header); } @@ -262,83 +272,8 @@ public class JerseyClient implements HttpClient { return this.alive; } - @Override - public String getName() { - return name; - } - - @Override - public boolean isHttps() { - return https; - } - - @Override - public boolean isSelfSignedCerts() { - return selfSignedCerts; - } - - @Override - public String getHostname() { - return hostname; - } - - @Override - public int getPort() { - return port; - } - - @Override - public String getBasePath() { - return basePath; - } - - @Override - public String getUserName() { - return userName; - } - - @GsonJsonIgnore - @Override - public String getPassword() { - return password; - } - - @Override - public String getBaseUrl() { - return baseUrl; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("JerseyClient [name="); - builder.append(name); - builder.append(", https="); - builder.append(https); - builder.append(", selfSignedCerts="); - builder.append(selfSignedCerts); - builder.append(", hostname="); - builder.append(hostname); - builder.append(", port="); - builder.append(port); - builder.append(", basePath="); - builder.append(basePath); - builder.append(", userName="); - builder.append(userName); - builder.append(", password="); - builder.append(password); - builder.append(", client="); - builder.append(client); - builder.append(", baseUrl="); - builder.append(baseUrl); - builder.append(", alive="); - builder.append(alive); - builder.append("]"); - return builder.toString(); - } - private Builder getBuilder(String path, Map<String, Object> headers) { - Builder builder = getWebTarget().path(path).request(); + var builder = getWebTarget().path(path).request(); for (Entry<String, Object> header : headers.entrySet()) { builder.header(header.getKey(), header.getValue()); } |