From 1b73b599b23921b3412d60ed231de1ef8ac90dbb Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Sat, 8 Feb 2020 14:16:15 -0500 Subject: Add headers to asynchronous get in HttpClient Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: I61ef2fcfde5eb361652d2d6e3f55324af7ca7b4e --- .../policy/common/endpoints/http/client/HttpClient.java | 8 +++++--- .../endpoints/http/client/internal/JerseyClient.java | 17 ++++++++++++----- .../endpoints/http/server/test/HttpClientTest.java | 9 +++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java index ebed1d7e..9e4e412f 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClient.java @@ -53,19 +53,21 @@ public interface HttpClient extends Startable { * Asynchronous GET request. * * @param callback callback to be invoked, asynchronously, when the request completes - * @param path context uri path. + * @param path context uri path + * @param headers request headers * * @return future that can be used to cancel the request or await the response */ - Future get(InvocationCallback callback, String path); + Future get(InvocationCallback callback, String path, Map headers); /** * Asynchronous GET request. * * @param callback callback to be invoked, asynchronously, when the request completes + * @param headers request headers * @return future that can be used to cancel the request or await the response */ - Future get(InvocationCallback callback); + Future get(InvocationCallback callback, Map headers); /** * PUT request. 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 1a822ff2..38ec6829 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 @@ -26,6 +26,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.util.Collections; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.Future; @@ -171,17 +172,23 @@ public class JerseyClient implements HttpClient { } @Override - public Future get(InvocationCallback callback, String path) { + public Future get(InvocationCallback callback, String path, Map headers) { + Map headers2 = (headers != null ? headers : Collections.emptyMap()); + if (!StringUtils.isBlank(path)) { - return this.client.target(this.baseUrl).path(path).request().async().get(callback); + return getBuilder(path, headers2).async().get(callback); } else { - return this.client.target(this.baseUrl).request().async().get(callback); + return get(callback, headers2); } } @Override - public Future get(InvocationCallback callback) { - return this.client.target(this.baseUrl).request().async().get(callback); + public Future get(InvocationCallback callback, Map headers) { + Builder builder = this.client.target(this.baseUrl).request(); + if (headers != null) { + headers.forEach(builder::header); + } + return builder.async().get(callback); } @Override diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java index d3f94cd0..5d609a65 100644 --- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java +++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/HttpClientTest.java @@ -31,6 +31,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Properties; +import java.util.TreeMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import javax.ws.rs.client.Entity; @@ -204,7 +205,7 @@ public class HttpClientTest { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); MyCallback callback = new MyCallback(); - final Response response = client.get(callback, HELLO).get(); + final Response response = client.get(callback, HELLO, new TreeMap<>()).get(); verifyCallback("testHttpGetNoAuthClientAsync", callback, response); @@ -316,7 +317,7 @@ public class HttpClientTest { final HttpClient client = getAuthHttpClient(); MyCallback callback = new MyCallback(); - final Response response = client.get(callback, HELLO).get(); + final Response response = client.get(callback, HELLO, null).get(); verifyCallback("testHttpAsyncAuthClient", callback, response); @@ -496,13 +497,13 @@ public class HttpClientTest { // try it asynchronously, too MyCallback callback = new MyCallback(); - response = clientPap.get(callback).get(); + response = clientPap.get(callback, null).get(); verifyCallback("testHttpAuthClientProps", callback, response); assertEquals(200, response.getStatus()); // try it asynchronously, with empty path callback = new MyCallback(); - response = clientPap.get(callback, "").get(); + response = clientPap.get(callback, "", null).get(); verifyCallback("testHttpAuthClientProps - empty path", callback, response); assertEquals(200, response.getStatus()); } -- cgit 1.2.3-korg