summaryrefslogtreecommitdiffstats
path: root/common/src/main/java
diff options
context:
space:
mode:
authorBenjamin, Max (mb388a) <mb388a@att.com>2021-01-28 13:12:40 -0500
committerBenjamin, Max (mb388a) <mb388a@att.com>2021-02-01 17:45:32 -0500
commitdd89e148932ba0acbfc6ceb4b229b001b6b8dd33 (patch)
treee377fce20812d390a07eab5d12665fe3e1903a1f /common/src/main/java
parentb8b0464aa8a05a345115264084b08229f35c8413 (diff)
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) <mb388a@att.com> Change-Id: I9c433cdaed33a7db6182af259421676c088a1fae
Diffstat (limited to 'common/src/main/java')
-rw-r--r--common/src/main/java/org/onap/so/client/HttpClient.java9
-rw-r--r--common/src/main/java/org/onap/so/client/RestClient.java32
-rw-r--r--common/src/main/java/org/onap/so/client/RestProperties.java7
-rw-r--r--common/src/main/java/org/onap/so/client/RestPropertiesLoader.java4
-rw-r--r--common/src/main/java/org/onap/so/client/RestRequest.java12
-rw-r--r--common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java11
-rw-r--r--common/src/main/java/org/onap/so/client/dmaap/rest/DMaaPRestClient.java11
-rw-r--r--common/src/main/java/org/onap/so/client/grm/GRMRestClient.java7
-rw-r--r--common/src/main/java/org/onap/so/client/policy/PolicyRestClient.java13
9 files changed, 64 insertions, 42 deletions
diff --git a/common/src/main/java/org/onap/so/client/HttpClient.java b/common/src/main/java/org/onap/so/client/HttpClient.java
index eaeb0ab6c6..0420ab2373 100644
--- a/common/src/main/java/org/onap/so/client/HttpClient.java
+++ b/common/src/main/java/org/onap/so/client/HttpClient.java
@@ -20,10 +20,11 @@
package org.onap.so.client;
+import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.net.URL;
-import java.util.Map;
import java.util.Optional;
-import static org.apache.commons.lang3.StringUtils.*;
+import javax.ws.rs.core.MultivaluedMap;
+import org.javatuples.Pair;
import org.onap.logging.filter.base.ONAPComponentsList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,7 +50,7 @@ public class HttpClient extends RestClient {
}
@Override
- protected void initializeHeaderMap(Map<String, String> headerMap) {}
+ protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {}
@Override
protected Optional<ResponseExceptionMapper> addResponseExceptionMapper() {
@@ -79,7 +80,7 @@ public class HttpClient extends RestClient {
public void addAdditionalHeader(String name, String value) {
try {
if (isNotBlank(name) && isNotBlank(value)) {
- headerMap.put(name, value);
+ headerMap.add("ALL", Pair.with(name, value));
} else {
log.warn("Not adding " + name + " to headers.");
}
diff --git a/common/src/main/java/org/onap/so/client/RestClient.java b/common/src/main/java/org/onap/so/client/RestClient.java
index f201c7c906..d1b4c2b900 100644
--- a/common/src/main/java/org/onap/so/client/RestClient.java
+++ b/common/src/main/java/org/onap/so/client/RestClient.java
@@ -29,10 +29,7 @@ import java.security.GeneralSecurityException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
@@ -43,9 +40,12 @@ import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
+import org.javatuples.Pair;
import org.onap.logging.filter.base.MDCSetup;
import org.onap.logging.filter.base.ONAPComponentsList;
import org.onap.logging.filter.base.PayloadLoggingClientFilter;
@@ -67,7 +67,7 @@ public abstract class RestClient {
private static final int MAX_PAYLOAD_SIZE = 1024 * 1024;
private WebTarget webTarget;
- protected final Map<String, String> headerMap;
+ protected final MultivaluedMap<String, Pair<String, String>> headerMap;
protected final Logger logger = LoggerFactory.getLogger(RestClient.class);
protected URL host;
protected Optional<URI> path;
@@ -80,7 +80,7 @@ public abstract class RestClient {
protected RestClient(RestProperties props, Optional<URI> path) {
- headerMap = new HashMap<>();
+ headerMap = new MultivaluedHashMap<>();
try {
host = props.getEndpoint();
} catch (MalformedURLException e) {
@@ -99,7 +99,7 @@ public abstract class RestClient {
}
protected RestClient(URL host, String contentType) {
- headerMap = new HashMap<>();
+ headerMap = new MultivaluedHashMap<>();
this.path = Optional.empty();
this.host = host;
this.contentType = contentType;
@@ -107,7 +107,7 @@ public abstract class RestClient {
}
protected RestClient(URL host, String acceptType, String contentType) {
- headerMap = new HashMap<>();
+ headerMap = new MultivaluedHashMap<>();
this.path = Optional.empty();
this.host = host;
this.accept = acceptType;
@@ -133,15 +133,23 @@ public abstract class RestClient {
return MAX_PAYLOAD_SIZE;
}
- protected Builder getBuilder() {
+ protected Builder getBuilder(String method) {
if (webTarget == null) {
initializeClient(getClient());
}
Builder builder = webTarget.request();
initializeHeaderMap(headerMap);
- for (Entry<String, String> entry : headerMap.entrySet()) {
- builder.header(entry.getKey(), entry.getValue());
+ if (headerMap.containsKey("ALL")) {
+ for (Pair<String, String> pair : headerMap.get("ALL")) {
+ builder.header(pair.getValue0(), pair.getValue1());
+ }
+ }
+
+ if (headerMap.containsKey(method)) {
+ for (Pair<String, String> pair : headerMap.get(method)) {
+ builder.header(pair.getValue0(), pair.getValue1());
+ }
}
return builder;
}
@@ -150,7 +158,7 @@ public abstract class RestClient {
return this.webTarget;
}
- protected abstract void initializeHeaderMap(Map<String, String> headerMap);
+ protected abstract void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap);
protected Optional<ResponseExceptionMapper> addResponseExceptionMapper() {
return Optional.of(new ResponseExceptionMapperImpl());
@@ -170,7 +178,7 @@ public abstract class RestClient {
try {
byte[] decryptedAuth = CryptoUtils.decrypt(auth, key).getBytes();
String authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(decryptedAuth);
- headerMap.put("Authorization", authHeaderValue);
+ headerMap.add("ALL", Pair.with("Authorization", authHeaderValue));
} catch (GeneralSecurityException e) {
logger.error(e.getMessage(), e);
}
diff --git a/common/src/main/java/org/onap/so/client/RestProperties.java b/common/src/main/java/org/onap/so/client/RestProperties.java
index 79e006bf9a..d1cf77b047 100644
--- a/common/src/main/java/org/onap/so/client/RestProperties.java
+++ b/common/src/main/java/org/onap/so/client/RestProperties.java
@@ -22,6 +22,9 @@ package org.onap.so.client;
import java.net.MalformedURLException;
import java.net.URL;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import org.javatuples.Pair;
public interface RestProperties {
@@ -66,4 +69,8 @@ public interface RestProperties {
public default CacheProperties getCacheProperties() {
return new CacheProperties() {};
}
+
+ public default MultivaluedMap<String, Pair<String, String>> additionalHeaders() {
+ return new MultivaluedHashMap<>();
+ }
}
diff --git a/common/src/main/java/org/onap/so/client/RestPropertiesLoader.java b/common/src/main/java/org/onap/so/client/RestPropertiesLoader.java
index efb50cf04b..952c8de7de 100644
--- a/common/src/main/java/org/onap/so/client/RestPropertiesLoader.java
+++ b/common/src/main/java/org/onap/so/client/RestPropertiesLoader.java
@@ -62,9 +62,9 @@ public class RestPropertiesLoader {
if (clazz.isAssignableFrom(item.getClass())) {
try {
if (forceNewInstance) {
- result = (T) item.getClass().newInstance();
+ return (T) item.getClass().newInstance();
} else {
- result = (T) item;
+ return (T) item;
}
} catch (InstantiationException | IllegalAccessException e) {
/*
diff --git a/common/src/main/java/org/onap/so/client/RestRequest.java b/common/src/main/java/org/onap/so/client/RestRequest.java
index 9e6e818171..0a1be758c9 100644
--- a/common/src/main/java/org/onap/so/client/RestRequest.java
+++ b/common/src/main/java/org/onap/so/client/RestRequest.java
@@ -49,23 +49,23 @@ public class RestRequest implements CheckedSupplier<Response> {
public Response get() throws Exception {
final Response response;
if ("GET".equals(method)) {
- response = this.client.getBuilder().accept(this.client.getAccept()).get();
+ response = this.client.getBuilder(method).accept(this.client.getAccept()).get();
} else if ("POST".equals(method)) {
- response = this.client.getBuilder().accept(this.client.getAccept())
+ response = this.client.getBuilder(method).accept(this.client.getAccept())
.post(Entity.entity(entity, this.client.getContentType()));
} else if ("PATCH".equals(method)) {
- response = this.client.getBuilder().header("X-HTTP-Method-Override", "PATCH")
+ response = this.client.getBuilder(method).header("X-HTTP-Method-Override", "PATCH")
.accept(this.client.getAccept()).post(Entity.entity(entity, this.client.getMergeContentType()));
} else if ("DELETE".equals(method)) {
if (entity == null) {
- response = this.client.getBuilder().accept(this.client.getAccept()).delete();
+ response = this.client.getBuilder(method).accept(this.client.getAccept()).delete();
} else {
- response = this.client.getBuilder().accept(this.client.getAccept())
+ response = this.client.getBuilder(method).accept(this.client.getAccept())
.build(HttpMethod.DELETE, Entity.entity(entity, this.client.getContentType())).invoke();
}
} else if ("PUT".equals(method)) {
- response = this.client.getBuilder().accept(this.client.getAccept())
+ response = this.client.getBuilder(method).accept(this.client.getAccept())
.put(Entity.entity(entity, this.client.getContentType()));
} else {
response = Response.serverError().entity(method + " not valid").build();
diff --git a/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java b/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java
index 88899a7097..3bac3dc210 100644
--- a/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java
+++ b/common/src/main/java/org/onap/so/client/adapter/rest/AdapterRestClient.java
@@ -22,14 +22,15 @@ package org.onap.so.client.adapter.rest;
import java.net.URI;
import java.security.GeneralSecurityException;
-import java.util.Map;
import java.util.Optional;
+import javax.ws.rs.core.MultivaluedMap;
import org.apache.commons.codec.binary.Base64;
+import org.javatuples.Pair;
+import org.onap.logging.filter.base.ONAPComponents;
import org.onap.so.client.RestClient;
import org.onap.so.client.policy.CommonObjectMapperProvider;
import org.onap.so.client.policy.JettisonStyleMapperProvider;
import org.onap.so.utils.CryptoUtils;
-import org.onap.logging.filter.base.ONAPComponents;
public class AdapterRestClient extends RestClient {
@@ -51,9 +52,9 @@ public class AdapterRestClient extends RestClient {
}
@Override
- protected void initializeHeaderMap(Map<String, String> headerMap) {
- headerMap.put("Authorization",
- this.getBasicAuth(adapterRestProperties.getAuth(), adapterRestProperties.getKey()));
+ protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {
+ headerMap.add("ALL", Pair.with("Authorization",
+ this.getBasicAuth(adapterRestProperties.getAuth(), adapterRestProperties.getKey())));
}
@Override
diff --git a/common/src/main/java/org/onap/so/client/dmaap/rest/DMaaPRestClient.java b/common/src/main/java/org/onap/so/client/dmaap/rest/DMaaPRestClient.java
index e0c8d4ba80..85417e09d4 100644
--- a/common/src/main/java/org/onap/so/client/dmaap/rest/DMaaPRestClient.java
+++ b/common/src/main/java/org/onap/so/client/dmaap/rest/DMaaPRestClient.java
@@ -21,8 +21,10 @@
package org.onap.so.client.dmaap.rest;
import java.net.URL;
-import java.util.Map;
import java.util.UUID;
+import javax.ws.rs.core.MultivaluedMap;
+import org.javatuples.Pair;
+import org.onap.logging.filter.base.ONAPComponents;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.onap.so.client.RestClient;
import org.onap.logging.filter.base.ONAPComponents;
@@ -46,14 +48,15 @@ public class DMaaPRestClient extends RestClient {
}
@Override
- protected void initializeHeaderMap(Map<String, String> headerMap) {
+ protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {
if (auth != null && !auth.isEmpty() && key != null && !key.isEmpty()) {
addBasicAuthHeader(auth, key);
}
String onapRequestId = UUID.randomUUID().toString();
- headerMap.put(ONAPLogConstants.Headers.REQUEST_ID, onapRequestId);
+ headerMap.add("ALL", Pair.with(ONAPLogConstants.Headers.REQUEST_ID, onapRequestId));
if (MDC.get(ONAPLogConstants.MDCs.REQUEST_ID) != null) {
- headerMap.put(ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
+ headerMap.add("ALL",
+ Pair.with(ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.REQUEST_ID)));
}
}
}
diff --git a/common/src/main/java/org/onap/so/client/grm/GRMRestClient.java b/common/src/main/java/org/onap/so/client/grm/GRMRestClient.java
index 147f688ea8..cf73a57535 100644
--- a/common/src/main/java/org/onap/so/client/grm/GRMRestClient.java
+++ b/common/src/main/java/org/onap/so/client/grm/GRMRestClient.java
@@ -22,10 +22,11 @@ package org.onap.so.client.grm;
import java.net.URI;
-import java.util.Map;
import java.util.Optional;
-import org.onap.so.client.RestClient;
+import javax.ws.rs.core.MultivaluedMap;
+import org.javatuples.Pair;
import org.onap.logging.filter.base.ONAPComponents;
+import org.onap.so.client.RestClient;
public class GRMRestClient extends RestClient {
@@ -42,7 +43,7 @@ public class GRMRestClient extends RestClient {
}
@Override
- protected void initializeHeaderMap(Map<String, String> headerMap) {
+ protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {
String auth = properties.getAuth();
String key = properties.getKey();
diff --git a/common/src/main/java/org/onap/so/client/policy/PolicyRestClient.java b/common/src/main/java/org/onap/so/client/policy/PolicyRestClient.java
index 33ece07e3d..f657c17c68 100644
--- a/common/src/main/java/org/onap/so/client/policy/PolicyRestClient.java
+++ b/common/src/main/java/org/onap/so/client/policy/PolicyRestClient.java
@@ -21,12 +21,13 @@
package org.onap.so.client.policy;
-import java.util.Map;
import java.util.Optional;
+import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriBuilder;
+import org.javatuples.Pair;
+import org.onap.logging.filter.base.ONAPComponents;
import org.onap.so.client.RestClient;
import org.onap.so.client.policy.entities.PolicyServiceType;
-import org.onap.logging.filter.base.ONAPComponents;
public class PolicyRestClient extends RestClient {
@@ -43,10 +44,10 @@ public class PolicyRestClient extends RestClient {
}
@Override
- protected void initializeHeaderMap(Map<String, String> headerMap) {
- headerMap.put("ClientAuth", properties.getClientAuth());
- headerMap.put("Authorization", properties.getAuth());
- headerMap.put("Environment", properties.getEnvironment());
+ protected void initializeHeaderMap(MultivaluedMap<String, Pair<String, String>> headerMap) {
+ headerMap.add("ALL", Pair.with("ClientAuth", properties.getClientAuth()));
+ headerMap.add("ALL", Pair.with("Authorization", properties.getAuth()));
+ headerMap.add("ALL", Pair.with("Environment", properties.getEnvironment()));
}
}