From 21e01d4ae2d5583112b23290a6d52f70951e70cd Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Thu, 4 Apr 2024 10:37:43 +0200 Subject: Constructor inject the jakarte ee ClientBuilder - do not create the ClientBuilder inside the classes but rather constructor inject it - this way instrumentation of the client could be possible when tracing is enabled Issue-ID: AAI-3817 Change-Id: I4c6d1faf8da98283e8bbae66bd0b3207786a4ce5 Signed-off-by: Fiete Ostkamp --- pom.xml | 5 ++--- .../org/onap/aai/restclient/client/RestClient.java | 5 +++-- .../aai/restclient/rest/RestClientBuilder.java | 7 +++++-- .../aai/restclient/client/RestfulClientTest.java | 6 ++++-- .../aai/restclient/rest/RestClientBuilderTest.java | 24 +++++++++++++--------- version.properties | 2 +- 6 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 7688640..fde2b09 100644 --- a/pom.xml +++ b/pom.xml @@ -25,16 +25,15 @@ org.onap.aai.aai-common aai-parent - 1.9.4 + 1.11.0 org.onap.aai rest-client - 1.9.0-SNAPSHOT + 1.11.0-SNAPSHOT aai-rest-client - 1.9.3 google_checks.xml ${project.build.directory}/code-coverage 0.80 diff --git a/src/main/java/org/onap/aai/restclient/client/RestClient.java b/src/main/java/org/onap/aai/restclient/client/RestClient.java index 9639c9d..0bdb177 100644 --- a/src/main/java/org/onap/aai/restclient/client/RestClient.java +++ b/src/main/java/org/onap/aai/restclient/client/RestClient.java @@ -33,6 +33,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; 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.WebTarget; @@ -98,8 +99,8 @@ public class RestClient { /** * Creates a new instance of the {@link RestClient}. */ - public RestClient() { - clientBuilder = new RestClientBuilder(); + public RestClient(ClientBuilder builder) { + clientBuilder = new RestClientBuilder(builder); } /** diff --git a/src/main/java/org/onap/aai/restclient/rest/RestClientBuilder.java b/src/main/java/org/onap/aai/restclient/rest/RestClientBuilder.java index 38a8558..bfd44c6 100644 --- a/src/main/java/org/onap/aai/restclient/rest/RestClientBuilder.java +++ b/src/main/java/org/onap/aai/restclient/rest/RestClientBuilder.java @@ -56,6 +56,8 @@ public class RestClientBuilder { private static final String KEYSTORE_TYPE = "PKCS12"; private static final String TRUST_STORE_PROPERTY = "javax.net.ssl.trustStore"; + private final ClientBuilder builder; + private boolean validateServerHostname; private boolean validateServerCertChain; private String clientCertFileName; @@ -71,7 +73,8 @@ public class RestClientBuilder { /** * Rest Client Builder. */ - public RestClientBuilder() { + public RestClientBuilder(ClientBuilder builder) { + this.builder = builder; validateServerHostname = DEFAULT_VALIDATE_SERVER_HOST; validateServerCertChain = DEFAULT_VALIDATE_CERT_CHAIN; clientCertFileName = DEFAULT_CLIENT_CERT_FILENAME; @@ -239,7 +242,7 @@ public class RestClientBuilder { protected Client getClient(boolean useSsl) throws Exception { // Finally, create and initialize our client... - ClientBuilder builder = ClientBuilder.newBuilder(); + if (useSsl) { setupSecureSocketLayerClientConfig(builder); } diff --git a/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java b/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java index 9fe09a1..30cee4e 100644 --- a/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java +++ b/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue; import javax.ws.rs.ProcessingException; import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; @@ -45,6 +46,7 @@ public class RestfulClientTest { private static final String TEST_URL = "http://localhost:9000/aai/v7"; private final MultivaluedMap emptyMap = new MultivaluedHashMap<>(); + private final ClientBuilder clientBuilder = ClientBuilder.newBuilder(); private RestClientBuilder mockClientBuilder; private Client mockedClient; @@ -84,7 +86,7 @@ public class RestfulClientTest { @Test public void validateConstructors() { - RestClient restClient = new RestClient(); + RestClient restClient = new RestClient(clientBuilder); assertNotNull(restClient); restClient = new RestClient(mockClientBuilder); assertNotNull(restClient); @@ -333,7 +335,7 @@ public class RestfulClientTest { @Test public void testGetClient() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC); restClientBuilder.setTruststoreFilename("truststore"); assertTrue(restClientBuilder.getClient() instanceof Client); diff --git a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java index 3878813..490e84a 100644 --- a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java +++ b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java @@ -28,6 +28,8 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + import org.junit.Before; import org.junit.Test; import org.onap.aai.restclient.enums.RestAuthenticationMode; @@ -37,6 +39,8 @@ import org.onap.aai.restclient.enums.RestAuthenticationMode; */ public class RestClientBuilderTest { + private final ClientBuilder clientBuilder = ClientBuilder.newBuilder(); + /** * Test case initialization * @@ -53,7 +57,7 @@ public class RestClientBuilderTest { @Test public void validateAccesors() { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); // test defaults assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST); @@ -99,7 +103,7 @@ public class RestClientBuilderTest { @Test public void validateNoAuthClientCreation() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH); restClientBuilder.setConnectTimeoutInMs(12345); @@ -113,7 +117,7 @@ public class RestClientBuilderTest { @Test public void validateUnknownModeCreateNoAuthClient() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE); restClientBuilder.setConnectTimeoutInMs(12345); @@ -126,7 +130,7 @@ public class RestClientBuilderTest { @Test public void validateBasicAuthSslClient() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC); restClientBuilder.setConnectTimeoutInMs(12345); @@ -143,7 +147,7 @@ public class RestClientBuilderTest { @Test(expected = IllegalArgumentException.class) public void validateSslCertClient_noHostOrCertChainValidation() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT); restClientBuilder.setConnectTimeoutInMs(12345); @@ -157,7 +161,7 @@ public class RestClientBuilderTest { @Test(expected = IllegalArgumentException.class) public void validateSslCertClient_hostOnlyValidation() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT); restClientBuilder.setConnectTimeoutInMs(12345); @@ -172,7 +176,7 @@ public class RestClientBuilderTest { @Test public void validateSslCertClient_certChainOnlyValidation() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT); restClientBuilder.setConnectTimeoutInMs(12345); @@ -191,7 +195,7 @@ public class RestClientBuilderTest { @Test public void validateSslCertClient_withHostAndCertChainValidation() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT); restClientBuilder.setConnectTimeoutInMs(12345); @@ -210,7 +214,7 @@ public class RestClientBuilderTest { @Test(expected = IllegalArgumentException.class) public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT); restClientBuilder.setConnectTimeoutInMs(12345); @@ -230,7 +234,7 @@ public class RestClientBuilderTest { @Test public void validateSslProtocolConfiguration() throws Exception { - RestClientBuilder restClientBuilder = new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(clientBuilder); assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol()); restClientBuilder.setSslProtocol("TLSv1.2"); diff --git a/version.properties b/version.properties index 0986d1f..f6bcc5a 100644 --- a/version.properties +++ b/version.properties @@ -3,7 +3,7 @@ # because they are used in Jenkins, whose plug-in doesn't support major=1 -minor=9 +minor=11 patch=0 base_version=${major}.${minor}.${patch} -- cgit 1.2.3-korg