Uplift httpcomponent dependencies 71/140171/3
authorakenihan <adam.kenihan@est.tech>
Tue, 21 Jan 2025 16:55:10 +0000 (16:55 +0000)
committerakenihan <adam.kenihan@est.tech>
Tue, 18 Feb 2025 15:55:48 +0000 (15:55 +0000)
Issue-ID: POLICY-5189
Change-Id: Ie54502cfbd0a7c33d3aa66dd6b011643e7af2675
Signed-off-by: akenihan <adam.kenihan@est.tech>
models-interactions/model-impl/rest/pom.xml
models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/HttpDeleteWithBody.java [deleted file]
models-interactions/model-impl/rest/src/main/java/org/onap/policy/rest/RestManager.java
models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/HttpDeleteWithBodyTest.java [deleted file]
models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java

index c280266..33919d7 100644 (file)
@@ -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=========================================================
   -->
 
             <artifactId>jakarta.ws.rs-api</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.httpcomponents</groupId>
-            <artifactId>httpclient</artifactId>
+            <groupId>org.apache.httpcomponents.client5</groupId>
+            <artifactId>httpclient5</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.httpcomponents</groupId>
-            <artifactId>httpcore</artifactId>
+            <groupId>org.apache.httpcomponents.core5</groupId>
+            <artifactId>httpcore5</artifactId>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
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 (file)
index 992209d..0000000
+++ /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;
-    }
-}
index 855dc92..48a02f0 100644 (file)
@@ -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<Integer, String> delete(String url, String username, String password, Map<String, String> 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<Integer, String> sendRequest(HttpRequestBase request) {
+    private Pair<Integer, String> 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<String, String> headers) {
+    private void addHeaders(HttpUriRequestBase request, String username, String password, Map<String, String> headers) {
         String authHeader = makeAuthHeader(username, password);
         if (headers != null) {
             for (Entry<String, String> 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 (file)
index 5f21633..0000000
+++ /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
index 78e5177..02ae7ec 100644 (file)
@@ -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<Integer, String> result = mgr.delete(getUri, null, null, null, null, "");
+        assertEquals((Integer) 405, result.getLeft());
+
+        Map<String, String> hm = new HashMap<String, String>();
+        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)