From 370900a774537b2299b14286144a34b2931e97ec Mon Sep 17 00:00:00 2001 From: akenihan Date: Tue, 21 Jan 2025 16:55:10 +0000 Subject: [PATCH] Uplift httpcomponent dependencies Issue-ID: POLICY-5189 Change-Id: Ie54502cfbd0a7c33d3aa66dd6b011643e7af2675 Signed-off-by: akenihan --- models-interactions/model-impl/rest/pom.xml | 12 +-- .../org/onap/policy/rest/HttpDeleteWithBody.java | 49 ------------ .../java/org/onap/policy/rest/RestManager.java | 86 ++++++++++++++-------- .../onap/policy/rest/HttpDeleteWithBodyTest.java | 38 ---------- .../test/java/org/onap/policy/rest/RestTest.java | 23 ++++++ 5 files changed, 84 insertions(+), 124 deletions(-) delete mode 100644 models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java delete mode 100644 models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java diff --git a/models-interactions/model-impl/rest/pom.xml b/models-interactions/model-impl/rest/pom.xml index c28026687..33919d781 100644 --- a/models-interactions/model-impl/rest/pom.xml +++ b/models-interactions/model-impl/rest/pom.xml @@ -3,7 +3,7 @@ rest ================================================================================ Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. - Modifications Copyright (C) 2019, 2023-2024 Nordix Foundation. + Modifications Copyright (C) 2019, 2023-2025 Nordix Foundation. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + + SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= --> @@ -58,12 +60,12 @@ jakarta.ws.rs-api - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 - org.apache.httpcomponents - httpcore + org.apache.httpcomponents.core5 + httpcore5 org.apache.commons diff --git a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java deleted file mode 100644 index 992209de4..000000000 --- a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * rest - * ================================================================================ - * Copyright (C) 2018 Amdocs. All rights reserved. - * Modifications Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.rest; - -import java.net.URI; -import lombok.NoArgsConstructor; -import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; - -/** - * Allows for HTTP DELETE requests to contain a body, which the HttpDelete - * class does not support. - */ -@NoArgsConstructor -public class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { - public static final String METHOD_NAME = "DELETE"; - - public HttpDeleteWithBody(final String uri) { - setURI(URI.create(uri)); - } - - public HttpDeleteWithBody(final URI uri) { - setURI(uri); - } - - @Override - public String getMethod() { - return METHOD_NAME; - } -} diff --git a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java index 855dc92f6..48a02f07a 100644 --- a/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java +++ b/models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java @@ -3,7 +3,7 @@ * rest * ================================================================================ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2023-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -25,20 +27,28 @@ import jakarta.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Map.Entry; +import javax.net.ssl.SSLContext; import org.apache.commons.lang3.tuple.Pair; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; +import org.apache.hc.client5.http.classic.methods.HttpDelete; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPatch; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpPut; +import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.ssl.NoopHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.core5.http.HttpResponse; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.message.StatusLine; +import org.apache.hc.core5.ssl.SSLContexts; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -65,8 +75,8 @@ public class RestManager { addHeaders(put, username, password, headers); put.addHeader(CONTENT_TYPE, contentType); try { - var input = new StringEntity(body); - input.setContentType(contentType); + var contentType1 = ContentType.create(contentType); + var input = new StringEntity(body, contentType1); put.setEntity(input); } catch (Exception e) { logger.error("put threw: ", e); @@ -92,8 +102,8 @@ public class RestManager { addHeaders(post, username, password, headers); post.addHeader(CONTENT_TYPE, contentType); try { - var input = new StringEntity(body); - input.setContentType(contentType); + var contentType1 = ContentType.create(contentType); + var input = new StringEntity(body, contentType1); post.setEntity(input); } catch (Exception e) { logger.error("post threw: ", e); @@ -131,13 +141,13 @@ public class RestManager { */ public Pair delete(String url, String username, String password, Map headers, String contentType, String body) { - var delete = new HttpDeleteWithBody(url); + var delete = new HttpDelete(url); addHeaders(delete, username, password, headers); if (body != null && !body.isEmpty()) { delete.addHeader(CONTENT_TYPE, contentType); try { - var input = new StringEntity(body); - input.setContentType(contentType); + ContentType contentType1 = ContentType.create(contentType); + var input = new StringEntity(body, contentType1); delete.setEntity(input); } catch (Exception e) { logger.error("delete threw: ", e); @@ -179,8 +189,8 @@ public class RestManager { addHeaders(patch, username, password, headers); patch.addHeader(CONTENT_TYPE, contentType); try { - var input = new StringEntity(body); - input.setContentType(contentType); + ContentType contentType1 = ContentType.create(contentType); + var input = new StringEntity(body, contentType1); patch.setEntity(input); } catch (Exception e) { logger.error("patch threw: ", e); @@ -195,27 +205,39 @@ public class RestManager { * @param request http request to send * @return the response status code and the body */ - private Pair sendRequest(HttpRequestBase request) { + private Pair sendRequest(HttpUriRequestBase request) { if (logger.isDebugEnabled()) { - logger.debug("***** sendRequest to url {}:", request.getURI()); + logger.debug("***** sendRequest to url {}:", request.getRequestUri()); } + var sslContext = SSLContexts.createDefault(); + PoolingHttpClientConnectionManager connectionManager = + PoolingHttpClientConnectionManagerBuilder.create() + .setSSLSocketFactory( + SSLConnectionSocketFactoryBuilder.create() + .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) + .setSslContext(sslContext) + .build() + ) + .build(); try (CloseableHttpClient client = - HttpClientBuilder.create().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) { + HttpClientBuilder.create().setConnectionManager(connectionManager).build()) { HttpResponse response = client.execute(request); if (response != null) { - var returnBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - logger.debug("HTTP Response Status Code: {}", response.getStatusLine().getStatusCode()); + ClassicHttpResponse classicHttpResponse = (ClassicHttpResponse) response; + var returnBody = EntityUtils.toString(classicHttpResponse.getEntity(), "UTF-8"); + + logger.debug("HTTP Response Status Code: {}", new StatusLine(response).getStatusCode()); logger.debug("HTTP Response Body:"); logger.debug(returnBody); - return Pair.of(response.getStatusLine().getStatusCode(), returnBody); + return Pair.of(new StatusLine(response).getStatusCode(), returnBody); } else { - logger.error("Response from {} is null", request.getURI()); + logger.error("Response from {} is null", request.getRequestUri()); return null; } } catch (Exception e) { - logger.error("Request failed to {}", request.getURI(), e); + logger.error("Request failed to {}", request.getRequestUri(), e); return null; } } @@ -228,7 +250,7 @@ public class RestManager { * @param password the password * @param headers any headers */ - private void addHeaders(HttpRequestBase request, String username, String password, Map headers) { + private void addHeaders(HttpUriRequestBase request, String username, String password, Map headers) { String authHeader = makeAuthHeader(username, password); if (headers != null) { for (Entry entry : headers.entrySet()) { diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java deleted file mode 100644 index 5f21633e3..000000000 --- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * rest - * ================================================================================ - * Copyright (C) 2018 Amdocs. All rights reserved. - * Modifications Copyright (C) 2019, 2024 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.rest; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class HttpDeleteWithBodyTest { - - private static final String NO_URI = "BlahBlah"; - - @Test - void testGetMethod() { - HttpDeleteWithBody deleteWithBody = new HttpDeleteWithBody(NO_URI); - assertEquals("DELETE", deleteWithBody.getMethod()); - assertEquals(NO_URI, deleteWithBody.getURI().toString()); - } -} \ No newline at end of file diff --git a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java index 78e5177b7..02ae7ece3 100644 --- a/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java +++ b/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java @@ -16,6 +16,8 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -43,6 +45,8 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.util.HashMap; +import java.util.Map; import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -342,6 +346,25 @@ public class RestTest { } + @Test + void testBody() { + RestManager mgr = new RestManager(); + + Pair result = mgr.delete(getUri, null, null, null, null, ""); + assertEquals((Integer) 405, result.getLeft()); + + Map hm = new HashMap(); + hm.put("header", "header"); + result = mgr.delete(getUri, null, null, hm, null, ""); + assertEquals((Integer) 405, result.getLeft()); + + result = mgr.delete(deleteUri, "user", null, null, MediaType.TEXT_PLAIN, "body"); + checkResult(result, "DELETE: " + EXPECT_STRING); + + result = mgr.put(deleteUri, "user", null, null, "content", "body"); + assertEquals((Integer) 400, result.getLeft()); + } + @GET @Path("/GetHello/{name}") @Produces(MediaType.TEXT_PLAIN) -- 2.16.6