From 9f15e1987dc9fc6bb8196c415b817b3f1cf6bd5f Mon Sep 17 00:00:00 2001 From: Jorge Hernandez Date: Tue, 15 Jan 2019 13:28:16 -0600 Subject: Add post and delete http client methods Additional clean up for sonars have also been added for the classes that have been modified Issue-ID: POLICY-1367 Change-Id: Ie97d9057273e89850420a7c1b5b2d275709bdfd0 Signed-off-by: Jorge Hernandez --- .../common/endpoints/http/client/HttpClient.java | 156 +++++++++++++++++---- .../endpoints/http/client/HttpClientFactory.java | 17 ++- .../http/client/IndexedHttpClientFactory.java | 2 +- .../http/client/internal/JerseyClient.java | 32 ++++- 4 files changed, 164 insertions(+), 43 deletions(-) (limited to 'policy-endpoints/src/main') 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 72847b18..2e3b9afb 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 @@ -1,8 +1,8 @@ /*- * ============LICENSE_START======================================================= - * policy-endpoints + * ONAP * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -27,36 +27,136 @@ import javax.ws.rs.core.Response; import org.onap.policy.common.capabilities.Startable; +/** + * Http Client interface. + */ public interface HttpClient extends Startable { - public static final HttpClientFactory factory = new IndexedHttpClientFactory(); - - public Response get(String path); - - public Response get(); - - public Response put(String path, Entity entity, Map headers); - - public static T getBody(Response response, Class entityType) { + /** + * Factory. + */ + HttpClientFactory factory = new IndexedHttpClientFactory(); + + /** + * GET request. + * + * @param path context uri path. + * @return response + */ + Response get(String path); + + /** + * GET request. + * + * @return response + */ + Response get(); + + /** + * PUT request. + * + * @param path context uri path + * @param entity body + * @param headers headers + * + * @return response. + */ + Response put(String path, Entity entity, Map headers); + + /** + * POST request. + * + * @param path context uri path + * @param entity body + * @param headers headers + * + * @return response. + */ + Response post(String path, Entity entity, Map headers); + + /** + * DELETE request. + * + * @param path context uri path + * @param headers headers + * + * @return response. + */ + Response delete(String path, Map headers); + + /** + * Retrieve the body from the HTTP transaction. + * + * @param response response. + * @param entityType body type. + * @param body class. + * + * @return response. + */ + static T getBody(Response response, Class entityType) { return response.readEntity(entityType); } - public String getName(); - - public boolean isHttps(); - - public boolean isSelfSignedCerts(); - - public String getHostname(); - - public int getPort(); - - public String getBasePath(); - - public String getUserName(); - - public String getPassword(); - - public String getBaseUrl(); + /** + * Get the client name. + * @return name + */ + String getName(); + + /** + * HTTPS support. + * + * @return if the client uses https + */ + boolean isHttps(); + + /** + * Self-signed certificates. + * + * @return if the self-signed certificates are allowed + */ + boolean isSelfSignedCerts(); + + /** + * Get the host name. + * + * @return host name + */ + String getHostname(); + + /** + * Get the port. + * + * @return port + */ + int getPort(); + + /** + * Get the base path. + * + * @return base path + */ + String getBasePath(); + + /** + * Get the user name. + * + * @return the user name + */ + String getUserName(); + + /** + * Get the password. + * + * @return the password + */ + String getPassword(); + + /** + * Get the base URL. + * + * @return the base URL + */ + String getBaseUrl(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java index 133dfaea..f482eb01 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/HttpClientFactory.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * policy-endpoints + * ONAP * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd. @@ -35,13 +35,13 @@ public interface HttpClientFactory { /** * Build and http client with the following parameters. */ - public HttpClient build(BusTopicParams busTopicParams) + HttpClient build(BusTopicParams busTopicParams) throws KeyManagementException, NoSuchAlgorithmException; /** * Build http client from properties. */ - public List build(Properties properties) throws KeyManagementException, NoSuchAlgorithmException; + List build(Properties properties) throws KeyManagementException, NoSuchAlgorithmException; /** * Get http client. @@ -49,21 +49,24 @@ public interface HttpClientFactory { * @param name the name * @return the http client */ - public HttpClient get(String name); + HttpClient get(String name); /** * List of http clients. * * @return http clients */ - public List inventory(); + List inventory(); /** * Destroy by name. * * @param name name */ - public void destroy(String name); + void destroy(String name); - public void destroy(); + /** + * destroy the factory. + */ + void destroy(); } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java index d4d4a281..9aef09e4 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/client/IndexedHttpClientFactory.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * ONAP Policy Engine - Common Modules + * ONAP * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ 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 94635615..b55a7bb2 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 @@ -1,8 +1,8 @@ /* * ============LICENSE_START======================================================= - * policy-endpoints + * ONAP * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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"); @@ -46,6 +46,9 @@ import org.onap.policy.common.endpoints.http.client.HttpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Http Client implementation using a Jersey Client. + */ public class JerseyClient implements HttpClient { /** @@ -164,11 +167,17 @@ public class JerseyClient implements HttpClient { @Override public Response put(String path, Entity entity, Map headers) { - Builder builder = this.client.target(this.baseUrl).path(path).request(); - for (Entry header : headers.entrySet()) { - builder.header(header.getKey(), header.getValue()); - } - return builder.put(entity); + return getBuilder(path, headers).put(entity); + } + + @Override + public Response post(String path, Entity entity, Map headers) { + return getBuilder(path, headers).post(entity); + } + + @Override + public Response delete(String path, Map headers) { + return getBuilder(path, headers).delete(); } @Override @@ -274,4 +283,13 @@ public class JerseyClient implements HttpClient { return builder.toString(); } + private Builder getBuilder(String path, Map headers) { + Builder builder = this.client.target(this.baseUrl).path(path).request(); + for (Entry header : headers.entrySet()) { + builder.header(header.getKey(), header.getValue()); + } + return builder; + } + + } -- cgit 1.2.3-korg