From 658471f126a11954b20249cbd9b0ecd97f14091e Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Fri, 31 Jan 2020 09:42:04 -0500 Subject: Add async support to HttpClient Issue-ID: POLICY-1625 Signed-off-by: Jim Hahn Change-Id: I0123b98e89b734efd264dd62080bb23573a7a3e6 --- .../common/endpoints/http/client/HttpClient.java | 63 ++++++++- .../http/client/internal/JerseyClient.java | 35 ++++- .../endpoints/http/server/test/HttpClientTest.java | 146 ++++++++++++++++++++- 3 files changed, 236 insertions(+), 8 deletions(-) (limited to 'policy-endpoints/src') 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 2fe46fb3..ebed1d7e 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,14 +21,16 @@ package org.onap.policy.common.endpoints.http.client; import java.util.Map; - +import java.util.concurrent.Future; import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; import javax.ws.rs.core.Response; import org.onap.policy.common.capabilities.Startable; /** - * Http Client interface. + * Http Client interface. Supports both synchronous and asynchronous operations. + * */ public interface HttpClient extends Startable { @@ -47,6 +49,24 @@ public interface HttpClient extends Startable { */ Response get(); + /** + * Asynchronous GET request. + * + * @param callback callback to be invoked, asynchronously, when the request completes + * @param path context uri path. + * + * @return future that can be used to cancel the request or await the response + */ + Future get(InvocationCallback callback, String path); + + /** + * Asynchronous GET request. + * + * @param callback callback to be invoked, asynchronously, when the request completes + * @return future that can be used to cancel the request or await the response + */ + Future get(InvocationCallback callback); + /** * PUT request. * @@ -58,6 +78,19 @@ public interface HttpClient extends Startable { */ Response put(String path, Entity entity, Map headers); + /** + * Asynchronous PUT request. + * + * @param callback callback to be invoked, asynchronously, when the request completes + * @param path context uri path + * @param entity body + * @param headers headers + * + * @return future that can be used to cancel the request or await the response + */ + Future put(InvocationCallback callback, String path, Entity entity, + Map headers); + /** * POST request. * @@ -69,6 +102,19 @@ public interface HttpClient extends Startable { */ Response post(String path, Entity entity, Map headers); + /** + * Asynchronous POST request. + * + * @param callback callback to be invoked, asynchronously, when the request completes + * @param path context uri path + * @param entity body + * @param headers headers + * + * @return future that can be used to cancel the request or await the response + */ + Future post(InvocationCallback callback, String path, Entity entity, + Map headers); + /** * DELETE request. * @@ -79,6 +125,17 @@ public interface HttpClient extends Startable { */ Response delete(String path, Map headers); + /** + * Asynchronous DELETE request. + * + * @param callback callback to be invoked, asynchronously, when the request completes + * @param path context uri path + * @param headers headers + * + * @return future that can be used to cancel the request or await the response + */ + Future delete(InvocationCallback callback, String path, Map headers); + /** * Retrieve the body from the HTTP transaction. * 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 8a717712..1a822ff2 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,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ @@ -28,11 +28,13 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; 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.core.Response; import org.apache.commons.lang3.StringUtils; import org.glassfish.jersey.client.ClientProperties; @@ -168,21 +170,52 @@ public class JerseyClient implements HttpClient { return this.client.target(this.baseUrl).request().get(); } + @Override + public Future get(InvocationCallback callback, String path) { + if (!StringUtils.isBlank(path)) { + return this.client.target(this.baseUrl).path(path).request().async().get(callback); + } else { + return this.client.target(this.baseUrl).request().async().get(callback); + } + } + + @Override + public Future get(InvocationCallback callback) { + return this.client.target(this.baseUrl).request().async().get(callback); + } + @Override public Response put(String path, Entity entity, Map headers) { return getBuilder(path, headers).put(entity); } + @Override + public Future put(InvocationCallback callback, String path, Entity entity, + Map headers) { + return getBuilder(path, headers).async().put(entity, callback); + } + @Override public Response post(String path, Entity entity, Map headers) { return getBuilder(path, headers).post(entity); } + @Override + public Future post(InvocationCallback callback, String path, Entity entity, + Map headers) { + return getBuilder(path, headers).async().post(entity, callback); + } + @Override public Response delete(String path, Map headers) { return getBuilder(path, headers).delete(); } + @Override + public Future delete(InvocationCallback callback, String path, Map headers) { + return getBuilder(path, headers).async().delete(callback); + } + @Override public boolean start() { return alive; 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 2aaf1367..d3f94cd0 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 @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,15 +23,21 @@ package org.onap.policy.common.endpoints.http.server.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import lombok.Getter; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -193,6 +199,28 @@ public class HttpClientTest { assertEquals(HELLO, body); } + @Test + public void testHttpGetNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + MyCallback callback = new MyCallback(); + final Response response = client.get(callback, HELLO).get(); + + verifyCallback("testHttpGetNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals(HELLO, body); + } + + private void verifyCallback(String testName, MyCallback callback, final Response response) + throws InterruptedException { + assertTrue(testName, callback.await()); + assertNull(testName, callback.getThrowable()); + assertSame(testName, response, callback.getResponse()); + } + @Test public void testHttpPutNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); @@ -205,6 +233,22 @@ public class HttpClientTest { assertEquals(PUT_HELLO, body); } + @Test + public void testHttpPutNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, 6666); + + Entity entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); + MyCallback callback = new MyCallback(); + final Response response = client.put(callback, HELLO, entity, Collections.emptyMap()).get(); + + verifyCallback("testHttpPutNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals(PUT_HELLO, body); + } + @Test public void testHttpPostNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, @@ -218,6 +262,23 @@ public class HttpClientTest { assertEquals("POST:hello:{myParameter=myValue}", body); } + @Test + public void testHttpPostNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + + Entity entity = Entity.entity(new MyEntity(MY_VALUE), MediaType.APPLICATION_JSON); + MyCallback callback = new MyCallback(); + final Response response = client.post(callback, HELLO, entity, Collections.emptyMap()).get(); + + verifyCallback("testHttpPostNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals("POST:hello:{myParameter=myValue}", body); + } + @Test public void testHttpDeletetNoAuthClient() throws Exception { final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, @@ -230,6 +291,41 @@ public class HttpClientTest { assertEquals("DELETE:hello", body); } + @Test + public void testHttpDeletetNoAuthClientAsync() throws Exception { + final HttpClient client = getNoAuthHttpClient(TEST_HTTP_NO_AUTH_CLIENT, false, + 6666); + + MyCallback callback = new MyCallback(); + final Response response = client.delete(callback, HELLO, Collections.emptyMap()).get(); + + verifyCallback("testHttpDeletetNoAuthClientAsync", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals("DELETE:hello", body); + } + + /** + * Perform one asynchronous test with auth client; don't need to test every method. + * @throws Exception if an error occurs + */ + @Test + public void testHttpAsyncAuthClient() throws Exception { + final HttpClient client = getAuthHttpClient(); + + MyCallback callback = new MyCallback(); + final Response response = client.get(callback, HELLO).get(); + + verifyCallback("testHttpAsyncAuthClient", callback, response); + + final String body = HttpClient.getBody(response, String.class); + + assertEquals(200, response.getStatus()); + assertEquals(HELLO, body); + } + @Test public void testHttpGetAuthClient() throws Exception { final HttpClient client = getAuthHttpClient(); @@ -382,16 +478,33 @@ public class HttpClientTest { server.waitedStart(10000); } + Response response; final HttpClient clientPap = HttpClientFactoryInstance.getClientFactory().get("PAP"); - final Response response = clientPap.get(); + response = clientPap.get(); assertEquals(200, response.getStatus()); final HttpClient clientPdp = HttpClientFactoryInstance.getClientFactory().get("PDP"); - final Response response2 = clientPdp.get("test"); - assertEquals(500, response2.getStatus()); + response = clientPdp.get("test"); + assertEquals(500, response.getStatus()); assertFalse(MyJacksonProvider.hasWrittenSome()); assertFalse(MyGsonProvider.hasWrittenSome()); + + // try with empty path + response = clientPap.get(""); + assertEquals(200, response.getStatus()); + + // try it asynchronously, too + MyCallback callback = new MyCallback(); + response = clientPap.get(callback).get(); + verifyCallback("testHttpAuthClientProps", callback, response); + assertEquals(200, response.getStatus()); + + // try it asynchronously, with empty path + callback = new MyCallback(); + response = clientPap.get(callback, "").get(); + verifyCallback("testHttpAuthClientProps - empty path", callback, response); + assertEquals(200, response.getStatus()); } @Test @@ -497,4 +610,29 @@ public class HttpClientTest { } + class MyCallback implements InvocationCallback { + @Getter + private Response response; + + @Getter + private Throwable throwable; + + private CountDownLatch latch = new CountDownLatch(1); + + @Override + public void completed(Response response) { + this.response = response; + latch.countDown(); + } + + @Override + public void failed(Throwable throwable) { + this.throwable = throwable; + latch.countDown(); + } + + public boolean await() throws InterruptedException { + return latch.await(5, TimeUnit.SECONDS); + } + } } -- cgit 1.2.3-korg