From dd89e148932ba0acbfc6ceb4b229b001b6b8dd33 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Thu, 28 Jan 2021 13:12:40 -0500 Subject: allow RestClient to configure custom headers allow RestClient to configure custom headers always load the first implementation on the classpath Issue-ID: SO-3495 Signed-off-by: Benjamin, Max (mb388a) Change-Id: I9c433cdaed33a7db6182af259421676c088a1fae --- .../org/onap/aaiclient/client/aai/AAIClient.java | 13 ++++++------ .../aaiclient/client/aai/AAIDSLQueryClient.java | 9 ++++++-- .../onap/aaiclient/client/aai/AAIRestClient.java | 24 +++++++++++++++------- .../graphinventory/GraphInventoryClient.java | 14 ++++++------- .../graphinventory/GraphInventoryRestClient.java | 5 +++-- .../client/aai/AAIDSLQueryClientTest.java | 5 +++-- .../aaiclient/client/aai/AAIRestClientTest.java | 17 ++++++++------- 7 files changed, 54 insertions(+), 33 deletions(-) (limited to 'graph-inventory/aai-client/src') diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIClient.java index 1f747e6c8c..fc5ef86e56 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIClient.java @@ -21,10 +21,11 @@ package org.onap.aaiclient.client.aai; import java.net.URI; -import java.util.HashMap; -import java.util.Map; import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; +import org.javatuples.Pair; import org.onap.aaiclient.client.graphinventory.GraphInventoryClient; import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.onap.so.client.RestClient; @@ -38,19 +39,19 @@ public class AAIClient extends GraphInventoryClient { protected AAIVersion version; protected AAIClient() { - super(AAIProperties.class, new HashMap()); + super(AAIProperties.class, new MultivaluedHashMap<>()); } protected AAIClient(AAIVersion version) { - super(AAIProperties.class, new HashMap()); + super(AAIProperties.class, new MultivaluedHashMap<>()); this.version = version; } - protected AAIClient(Map additionalHeaders) { + protected AAIClient(MultivaluedMap> additionalHeaders) { super(AAIProperties.class, additionalHeaders); } - protected AAIClient(AAIVersion version, Map additionalHeaders) { + protected AAIClient(AAIVersion version, MultivaluedMap> additionalHeaders) { super(AAIProperties.class, additionalHeaders); this.version = version; } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java index 8b8707e533..d894a0fa0d 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java @@ -20,6 +20,8 @@ package org.onap.aaiclient.client.aai; +import javax.ws.rs.core.MultivaluedHashMap; +import org.javatuples.Pair; import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; @@ -33,11 +35,14 @@ public class AAIDSLQueryClient extends GraphInventoryQueryClient { public AAIDSLQueryClient() { - super(new AAIClient(ImmutableMap.of("X-DslApiVersion", "V2"))); + super(new AAIClient(new MultivaluedHashMap>( + ImmutableMap.of("ALL", Pair.with("X-DslApiVersion", "V2"))))); + } public AAIDSLQueryClient(AAIVersion version) { - super(new AAIClient(version, ImmutableMap.of("X-DslApiVersion", "V2"))); + super(new AAIClient(version, new MultivaluedHashMap>( + ImmutableMap.of("ALL", Pair.with("X-DslApiVersion", "V2"))))); } @Override diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIRestClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIRestClient.java index 0f69b0cc8f..df124efe9a 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIRestClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIRestClient.java @@ -21,8 +21,9 @@ package org.onap.aaiclient.client.aai; import java.net.URI; -import java.util.Map; import java.util.Optional; +import javax.ws.rs.core.MultivaluedMap; +import org.javatuples.Pair; import org.onap.aaiclient.client.graphinventory.GraphInventoryPatchConverter; import org.onap.aaiclient.client.graphinventory.GraphInventoryRestClient; import org.onap.logging.filter.base.ONAPComponents; @@ -31,9 +32,10 @@ import org.onap.so.client.ResponseExceptionMapper; public class AAIRestClient extends GraphInventoryRestClient { private final AAIProperties aaiProperties; - private final Map additionalHeaders; + private final MultivaluedMap> additionalHeaders; - protected AAIRestClient(AAIProperties props, URI uri, Map additionalHeaders) { + protected AAIRestClient(AAIProperties props, URI uri, + MultivaluedMap> additionalHeaders) { super(props, uri); this.aaiProperties = props; this.additionalHeaders = additionalHeaders; @@ -45,16 +47,24 @@ public class AAIRestClient extends GraphInventoryRestClient { } @Override - protected void initializeHeaderMap(Map headerMap) { - headerMap.put("X-FromAppId", aaiProperties.getSystemName()); - headerMap.put("X-TransactionId", requestId); - headerMap.putAll(additionalHeaders); + protected void initializeHeaderMap(MultivaluedMap> headerMap) { + headerMap.add("ALL", Pair.with("X-FromAppId", aaiProperties.getSystemName())); + headerMap.add("ALL", Pair.with("X-TransactionId", requestId)); + additionalHeaders.forEach((k, v) -> { + headerMap.addAll(k, v); + }); String auth = aaiProperties.getAuth(); String key = aaiProperties.getKey(); if (auth != null && !auth.isEmpty() && key != null && !key.isEmpty()) { addBasicAuthHeader(auth, key); } + + if (!aaiProperties.additionalHeaders().isEmpty()) { + aaiProperties.additionalHeaders().forEach((k, v) -> { + headerMap.addAll(k, v); + }); + } } @Override diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryClient.java index f8f977d117..af875fa274 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryClient.java @@ -21,22 +21,22 @@ package org.onap.aaiclient.client.graphinventory; import java.net.URI; -import java.util.Map; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import org.javatuples.Pair; import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryUri; import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri; import org.onap.so.client.RestClient; import org.onap.so.client.RestProperties; import org.onap.so.client.RestPropertiesLoader; -import com.google.common.collect.ImmutableMap; public abstract class GraphInventoryClient { private RestProperties props; - protected final Map additionalHeaders; + protected final MultivaluedMap> additionalHeaders; protected GraphInventoryClient(Class propertiesClass, - Map additionalHeaders) { - + MultivaluedMap> additionalHeaders) { RestProperties props = RestPropertiesLoader.getInstance().getNewImpl(propertiesClass); this.props = props; this.additionalHeaders = additionalHeaders; @@ -70,7 +70,7 @@ public abstract class GraphInventoryClient { public abstract String getGraphDBName(); - public Map getAdditionalHeaders() { - return ImmutableMap.copyOf(this.additionalHeaders); + public MultivaluedMap> getAdditionalHeaders() { + return new MultivaluedHashMap<>(this.additionalHeaders); } } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryRestClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryRestClient.java index c22f2f5f8e..6ccd1a2cab 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryRestClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryRestClient.java @@ -21,10 +21,11 @@ package org.onap.aaiclient.client.graphinventory; import java.net.URI; -import java.util.Map; import java.util.Optional; import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; +import org.javatuples.Pair; import org.onap.aaiclient.client.CacheControlFeature; import org.onap.aaiclient.client.FlushCache; import org.onap.logging.filter.base.ONAPComponentsList; @@ -65,7 +66,7 @@ public abstract class GraphInventoryRestClient extends RestClientSSL { public abstract ONAPComponentsList getTargetEntity(); @Override - protected abstract void initializeHeaderMap(Map headerMap); + protected abstract void initializeHeaderMap(MultivaluedMap> headerMap); @Override protected abstract Optional addResponseExceptionMapper(); diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIDSLQueryClientTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIDSLQueryClientTest.java index 36fc1db57c..68858de9a4 100644 --- a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIDSLQueryClientTest.java +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIDSLQueryClientTest.java @@ -1,7 +1,8 @@ package org.onap.aaiclient.client.aai; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.net.URISyntaxException; +import org.javatuples.Pair; import org.junit.Test; public class AAIDSLQueryClientTest { @@ -12,6 +13,6 @@ public class AAIDSLQueryClientTest { public void verifyHeadersTest() throws URISyntaxException { AAIDSLQueryClient client = new AAIDSLQueryClient(); - assertEquals("V2", client.getClient().getAdditionalHeaders().get("X-DslApiVersion")); + assertTrue(client.getClient().getAdditionalHeaders().get("ALL").contains(Pair.with("X-DslApiVersion", "V2"))); } } diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIRestClientTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIRestClientTest.java index d0f7847726..9b34095a23 100644 --- a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIRestClientTest.java +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIRestClientTest.java @@ -43,8 +43,9 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.util.HashMap; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.Response; +import org.javatuples.Pair; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -75,7 +76,7 @@ public class AAIRestClientTest { @Test public void failPatchOnComplexObject() throws URISyntaxException { - AAIRestClient client = new AAIRestClient(props, new URI(""), new HashMap()); + AAIRestClient client = new AAIRestClient(props, new URI(""), new MultivaluedHashMap<>()); this.thrown.expect(GraphInventoryPatchDepthExceededException.class); this.thrown.expectMessage(containsString("Object exceeds allowed depth for update action")); client.patch( @@ -84,7 +85,7 @@ public class AAIRestClientTest { @Test public void verifyPatchValidation() throws URISyntaxException { - AAIRestClient client = new AAIRestClient(props, new URI(""), new HashMap()); + AAIRestClient client = new AAIRestClient(props, new URI(""), new MultivaluedHashMap<>()); AAIRestClient spy = spy(client); GraphInventoryPatchConverter patchValidatorMock = mock(GraphInventoryPatchConverter.class); doReturn(patchValidatorMock).when(spy).getPatchConverter(); @@ -97,11 +98,13 @@ public class AAIRestClientTest { @Test public void verifyAdditionalHeadersTest() throws URISyntaxException { AAIRestClient client = new AAIRestClient(new DefaultAAIPropertiesImpl(wireMockRule.port()), new URI("/test"), - ImmutableMap.of("test", "value")); + new MultivaluedHashMap>( + ImmutableMap.of("ALL", Pair.with("test", "value"), "GET", Pair.with("get test", "value")))); wireMockRule.stubFor(get(urlPathEqualTo("/test")).willReturn(aResponse().withStatus(200))); client.get(); wireMockRule.verify(getRequestedFor(urlPathEqualTo("/test")).withHeader("X-FromAppId", equalTo("MSO")) - .withHeader("X-TransactionId", matching(".*")).withHeader("test", equalTo("value"))); + .withHeader("X-TransactionId", matching(".*")).withHeader("test", equalTo("value")) + .withHeader("get test", equalTo("value"))); } @@ -145,7 +148,7 @@ public class AAIRestClientTest { } }; - RestClient client = new AAIRestClient(props, new URI("/cached"), new HashMap()); + RestClient client = new AAIRestClient(props, new URI("/cached"), new MultivaluedHashMap<>()); Response response = client.get(); @@ -199,7 +202,7 @@ public class AAIRestClientTest { }; - RestClient client = new AAIRestClient(props, new URI("/cached/1"), new HashMap()); + RestClient client = new AAIRestClient(props, new URI("/cached/1"), new MultivaluedHashMap<>()); Response response = client.get(); -- cgit 1.2.3-korg