You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/03/17 11:40:25 UTC
[camel] 07/19: CAMEL-18995: camel-undertow - Upgrade to HttpComponents 5.x
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a commit to branch CAMEL-18995/upgrade-httpcomponents-5
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5ec7f6eb17c9a11051a7638c5a53f5c5c892662b
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Fri Mar 17 12:25:45 2023 +0100
CAMEL-18995: camel-undertow - Upgrade to HttpComponents 5.x
---
components/camel-undertow/pom.xml | 6 ---
.../component/undertow/MultiPartFormTest.java | 26 +++++++------
.../UndertowComponentMuteExceptionTest.java | 43 ++++++++++-----------
.../undertow/UndertowMethodRestricTest.java | 39 +++++++++----------
.../undertow/UndertowMuteExceptionTest.java | 44 ++++++++++-----------
.../undertow/UndertowSwitchingStatus204Test.java | 45 ++++++++++++----------
.../undertow/UndertowTransferExceptionTest.java | 25 ++++++------
7 files changed, 108 insertions(+), 120 deletions(-)
diff --git a/components/camel-undertow/pom.xml b/components/camel-undertow/pom.xml
index 4005eadd8c1..bed907f351d 100644
--- a/components/camel-undertow/pom.xml
+++ b/components/camel-undertow/pom.xml
@@ -137,12 +137,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpmime</artifactId>
- <version>${httpclient4-version}</version>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-client</artifactId>
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/MultiPartFormTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/MultiPartFormTest.java
index fcb9593b279..751370d6ddd 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/MultiPartFormTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/MultiPartFormTest.java
@@ -24,11 +24,12 @@ import jakarta.activation.DataHandler;
import org.apache.camel.attachment.AttachmentMessage;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.util.IOHelper;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.mime.MultipartEntityBuilder;
-import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.HttpEntity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -47,20 +48,21 @@ public class MultiPartFormTest extends BaseUndertowTest {
@Test
public void testSendMultiPartForm() throws Exception {
- org.apache.http.client.HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://localhost:" + getPort() + "/test");
post.setEntity(createMultipartRequestEntity());
- HttpResponse response = client.execute(post);
- int status = response.getStatusLine().getStatusCode();
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(post)) {
+ int status = response.getCode();
- assertEquals(200, status, "Get a wrong response status");
- String result = IOHelper.loadText(response.getEntity().getContent()).trim();
+ assertEquals(200, status, "Get a wrong response status");
+ String result = IOHelper.loadText(response.getEntity().getContent()).trim();
- assertEquals("A binary file of some kind", result, "Get a wrong result");
+ assertEquals("A binary file of some kind", result, "Get a wrong result");
+ }
}
@Test
- public void testSendMultiPartFormFromCamelHttpComponnent() {
+ public void testSendMultiPartFormFromCamelHttpComponent() {
String result
= template.requestBody("http://localhost:" + getPort() + "/test", createMultipartRequestEntity(), String.class);
assertEquals("A binary file of some kind", result, "Get a wrong result");
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowComponentMuteExceptionTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowComponentMuteExceptionTest.java
index adf88c63b62..e499a48df92 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowComponentMuteExceptionTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowComponentMuteExceptionTest.java
@@ -17,11 +17,11 @@
package org.apache.camel.component.undertow;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -31,36 +31,33 @@ public class UndertowComponentMuteExceptionTest extends BaseUndertowTest {
@Test
public void muteExceptionTest() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
-
HttpGet get = new HttpGet("http://localhost:" + getPort() + "/test/mute");
get.addHeader("Accept", "application/text");
- HttpResponse response = client.execute(get);
-
- String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
- assertNotNull(responseString);
- assertEquals("", responseString);
- assertEquals(500, response.getStatusLine().getStatusCode());
-
- client.close();
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(get)) {
+
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertNotNull(responseString);
+ assertEquals("", responseString);
+ assertEquals(500, response.getCode());
+ }
}
@Test
public void muteExceptionWithTransferExceptionTest() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("http://localhost:" + getPort() + "/test/muteWithTransfer");
get.addHeader("Accept", "application/text");
- HttpResponse response = client.execute(get);
-
- String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
- assertNotNull(responseString);
- assertEquals("", responseString);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(get)) {
- assertEquals(500, response.getStatusLine().getStatusCode());
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertNotNull(responseString);
+ assertEquals("", responseString);
- client.close();
+ assertEquals(500, response.getCode());
+ }
}
@Override
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMethodRestricTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMethodRestricTest.java
index b2aceae041d..bccbd4ae01d 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMethodRestricTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMethodRestricTest.java
@@ -18,13 +18,13 @@ package org.apache.camel.component.undertow;
import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -41,31 +41,28 @@ public class UndertowMethodRestricTest extends BaseUndertowTest {
@Test
public void testProperHttpMethod() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
-
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity("This is a test"));
- HttpResponse response = client.execute(httpPost);
-
- assertEquals(200, response.getStatusLine().getStatusCode());
- String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
- assertEquals("This is a test response", responseString);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(httpPost)) {
- client.close();
+ assertEquals(200, response.getCode());
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertEquals("This is a test response", responseString);
+ }
}
@Test
public void testImproperHttpMethod() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
-
HttpGet httpGet = new HttpGet(url);
- HttpResponse response = client.execute(httpGet);
- int status = response.getStatusLine().getStatusCode();
- assertEquals(405, status, "Get a wrong response status");
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(httpGet)) {
+ int status = response.getCode();
- client.close();
+ assertEquals(405, status, "Get a wrong response status");
+ }
}
@Override
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMuteExceptionTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMuteExceptionTest.java
index e244fa70026..3911365c13e 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMuteExceptionTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowMuteExceptionTest.java
@@ -17,11 +17,11 @@
package org.apache.camel.component.undertow;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -31,36 +31,32 @@ public class UndertowMuteExceptionTest extends BaseUndertowTest {
@Test
public void muteExceptionTest() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
-
HttpGet get = new HttpGet("http://localhost:" + getPort() + "/test/mute");
get.addHeader("Accept", "application/text");
- HttpResponse response = client.execute(get);
-
- String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
- assertNotNull(responseString);
- assertEquals("", responseString);
- assertEquals(500, response.getStatusLine().getStatusCode());
-
- client.close();
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(get)) {
+
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertNotNull(responseString);
+ assertEquals("", responseString);
+ assertEquals(500, response.getCode());
+ }
}
@Test
public void muteExceptionWithTransferExceptionTest() throws Exception {
- CloseableHttpClient client = HttpClients.createDefault();
-
HttpGet get = new HttpGet("http://localhost:" + getPort() + "/test/muteWithTransfer");
get.addHeader("Accept", "application/text");
- HttpResponse response = client.execute(get);
-
- String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
- assertNotNull(responseString);
- assertEquals("", responseString);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(get)) {
- assertEquals(500, response.getStatusLine().getStatusCode());
+ String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
+ assertNotNull(responseString);
+ assertEquals("", responseString);
- client.close();
+ assertEquals(500, response.getCode());
+ }
}
@Override
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSwitchingStatus204Test.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSwitchingStatus204Test.java
index 9d7617de4cf..65ff92b4cbb 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSwitchingStatus204Test.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowSwitchingStatus204Test.java
@@ -19,12 +19,12 @@ package org.apache.camel.component.undertow;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -35,12 +35,13 @@ public class UndertowSwitchingStatus204Test extends BaseUndertowTest {
@Test
public void testSwitchNoBodyTo204ViaHttpEmptyBody() throws Exception {
- HttpUriRequest request = new HttpGet("http://localhost:" + getPort() + "/foo");
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpResponse httpResponse = httpClient.execute(request);
+ HttpGet request = new HttpGet("http://localhost:" + getPort() + "/foo");
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse httpResponse = httpClient.execute(request)) {
- assertEquals(204, httpResponse.getStatusLine().getStatusCode());
- assertNull(httpResponse.getEntity());
+ assertEquals(204, httpResponse.getCode());
+ assertNull(httpResponse.getEntity());
+ }
}
@Test
@@ -66,12 +67,13 @@ public class UndertowSwitchingStatus204Test extends BaseUndertowTest {
@Test
public void testNoSwitchingHasBodyViaHttpNoContent() throws Exception {
HttpUriRequest request = new HttpGet("http://localhost:" + getPort() + "/bar");
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpResponse httpResponse = httpClient.execute(request);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse httpResponse = httpClient.execute(request)) {
- assertEquals(200, httpResponse.getStatusLine().getStatusCode());
- assertNotNull(httpResponse.getEntity());
- assertEquals("No Content", EntityUtils.toString(httpResponse.getEntity()));
+ assertEquals(200, httpResponse.getCode());
+ assertNotNull(httpResponse.getEntity());
+ assertEquals("No Content", EntityUtils.toString(httpResponse.getEntity()));
+ }
}
@Test
@@ -97,12 +99,13 @@ public class UndertowSwitchingStatus204Test extends BaseUndertowTest {
@Test
public void testNoSwitchingHasCodeViaHttpNoContent() throws Exception {
HttpUriRequest request = new HttpGet("http://localhost:" + getPort() + "/foobar");
- HttpClient httpClient = HttpClientBuilder.create().build();
- HttpResponse httpResponse = httpClient.execute(request);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse httpResponse = httpClient.execute(request)) {
- assertEquals(200, httpResponse.getStatusLine().getStatusCode());
- assertNotNull(httpResponse.getEntity());
- assertEquals("", EntityUtils.toString(httpResponse.getEntity()));
+ assertEquals(200, httpResponse.getCode());
+ assertNotNull(httpResponse.getEntity());
+ assertEquals("", EntityUtils.toString(httpResponse.getEntity()));
+ }
}
@Test
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowTransferExceptionTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowTransferExceptionTest.java
index 97dda423b86..4ce054564d0 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowTransferExceptionTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/UndertowTransferExceptionTest.java
@@ -20,10 +20,10 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -33,19 +33,18 @@ public class UndertowTransferExceptionTest extends BaseUndertowTest {
@Test
public void getSerializedExceptionTest() throws IOException, ClassNotFoundException {
- CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("http://localhost:" + getPort() + "/test/transfer");
get.addHeader("Accept", "application/x-java-serialized-object");
- HttpResponse response = client.execute(get);
+ try (CloseableHttpClient httpClient = HttpClients.createDefault();
+ CloseableHttpResponse response = httpClient.execute(get)) {
- ObjectInputStream in = new ObjectInputStream(response.getEntity().getContent());
- IllegalArgumentException e = (IllegalArgumentException) in.readObject();
- assertNotNull(e);
- assertEquals(500, response.getStatusLine().getStatusCode());
- assertEquals("Camel cannot do this", e.getMessage());
-
- client.close();
+ ObjectInputStream in = new ObjectInputStream(response.getEntity().getContent());
+ IllegalArgumentException e = (IllegalArgumentException) in.readObject();
+ assertNotNull(e);
+ assertEquals(500, response.getCode());
+ assertEquals("Camel cannot do this", e.getMessage());
+ }
}
@Override