diff options
author | Jim Hahn <jrh3@att.com> | 2020-01-31 09:42:04 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2020-01-31 10:06:35 -0500 |
commit | 658471f126a11954b20249cbd9b0ecd97f14091e (patch) | |
tree | 8a9d782eadd84e511e310c0e1919ca835d8479ee /policy-endpoints/src/main/java/org/onap | |
parent | b2899115452784712443429704d5956075e95557 (diff) |
Add async support to HttpClient
Issue-ID: POLICY-1625
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: I0123b98e89b734efd264dd62080bb23573a7a3e6
Diffstat (limited to 'policy-endpoints/src/main/java/org/onap')
2 files changed, 94 insertions, 4 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 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 { @@ -48,6 +50,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<Response> get(InvocationCallback<Response> 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<Response> get(InvocationCallback<Response> callback); + + /** * PUT request. * * @param path context uri path @@ -59,6 +79,19 @@ public interface HttpClient extends Startable { Response put(String path, Entity<?> entity, Map<String, Object> 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<Response> put(InvocationCallback<Response> callback, String path, Entity<?> entity, + Map<String, Object> headers); + + /** * POST request. * * @param path context uri path @@ -70,6 +103,19 @@ public interface HttpClient extends Startable { Response post(String path, Entity<?> entity, Map<String, Object> 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<Response> post(InvocationCallback<Response> callback, String path, Entity<?> entity, + Map<String, Object> headers); + + /** * DELETE request. * * @param path context uri path @@ -80,6 +126,17 @@ public interface HttpClient extends Startable { Response delete(String path, Map<String, Object> 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<Response> delete(InvocationCallback<Response> callback, String path, Map<String, Object> headers); + + /** * Retrieve the body from the HTTP transaction. * * @param response response. 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; @@ -169,21 +171,52 @@ public class JerseyClient implements HttpClient { } @Override + public Future<Response> get(InvocationCallback<Response> 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<Response> get(InvocationCallback<Response> callback) { + return this.client.target(this.baseUrl).request().async().get(callback); + } + + @Override public Response put(String path, Entity<?> entity, Map<String, Object> headers) { return getBuilder(path, headers).put(entity); } @Override + public Future<Response> put(InvocationCallback<Response> callback, String path, Entity<?> entity, + Map<String, Object> headers) { + return getBuilder(path, headers).async().put(entity, callback); + } + + @Override public Response post(String path, Entity<?> entity, Map<String, Object> headers) { return getBuilder(path, headers).post(entity); } @Override + public Future<Response> post(InvocationCallback<Response> callback, String path, Entity<?> entity, + Map<String, Object> headers) { + return getBuilder(path, headers).async().post(entity, callback); + } + + @Override public Response delete(String path, Map<String, Object> headers) { return getBuilder(path, headers).delete(); } @Override + public Future<Response> delete(InvocationCallback<Response> callback, String path, Map<String, Object> headers) { + return getBuilder(path, headers).async().delete(callback); + } + + @Override public boolean start() { return alive; } |