This is an automated email from the ASF dual-hosted git repository.
vavrtom pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git
The following commit(s) were added to refs/heads/main by this push:
new 8bcf2cdbf4 QPID-8755: [Broker-J] Replace Apache HttpClient with Java
HTTP client in test modules (#432)
8bcf2cdbf4 is described below
commit 8bcf2cdbf4e17bebb580f93cf5b18970f34d869b
Author: Daniil Kirilyuk <[email protected]>
AuthorDate: Wed Sep 9 15:09:09 2026 +0200
QPID-8755: [Broker-J] Replace Apache HttpClient with Java HTTP client in
test modules (#432)
Co-authored-by: vavrtom <[email protected]>
---
joramtests/pom.xml | 9 -
.../qpid/joramtests/admin/JavaBrokerAdmin.java | 131 +++++++-------
perftests/pom.xml | 5 -
perftests/src/main/assembly/NOTICE | 10 --
.../dependency-verification/DEPENDENCIES_REFERENCE | 12 --
.../qpid/disttest/jms/QpidRestAPIQueueCreator.java | 193 +++++++++------------
pom.xml | 11 --
tck/pom.xml | 4 -
.../apache/qpid/tck/ManageQpidJMSResources.java | 130 +++++++-------
9 files changed, 221 insertions(+), 284 deletions(-)
diff --git a/joramtests/pom.xml b/joramtests/pom.xml
index 486eaf275b..afe8213921 100644
--- a/joramtests/pom.xml
+++ b/joramtests/pom.xml
@@ -62,15 +62,6 @@
<artifactId>joram-jms-tests</artifactId>
<version>${joram-jms-tests-version}</version>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5-fluent</artifactId>
- </dependency>
-
</dependencies>
<build>
diff --git
a/joramtests/src/main/java/org/apache/qpid/joramtests/admin/JavaBrokerAdmin.java
b/joramtests/src/main/java/org/apache/qpid/joramtests/admin/JavaBrokerAdmin.java
index 8c7059b9ef..40600b163f 100644
---
a/joramtests/src/main/java/org/apache/qpid/joramtests/admin/JavaBrokerAdmin.java
+++
b/joramtests/src/main/java/org/apache/qpid/joramtests/admin/JavaBrokerAdmin.java
@@ -21,8 +21,17 @@
package org.apache.qpid.joramtests.admin;
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_OK;
+
import java.io.IOException;
+import java.net.URI;
import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import java.util.Hashtable;
import javax.jms.ConnectionFactory;
@@ -34,33 +43,20 @@ import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
-import org.apache.hc.client5.http.auth.AuthScope;
-import org.apache.hc.client5.http.auth.CredentialsProvider;
-import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
-import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
-import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
-import org.apache.hc.client5.http.impl.auth.BasicScheme;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
-import org.apache.hc.client5.http.protocol.HttpClientContext;
-import org.apache.hc.core5.http.ClassicHttpRequest;
-import org.apache.hc.core5.http.ContentType;
-import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.client5.http.classic.methods.HttpDelete;
-import org.apache.hc.client5.http.classic.methods.HttpPut;
-import org.apache.hc.client5.http.impl.classic.HttpClients;
-import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.io.entity.StringEntity;
import org.objectweb.jtests.jms.admin.Admin;
public class JavaBrokerAdmin implements Admin
{
+ private static final String APPLICATION_JSON = "application/json;
charset=UTF-8";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final String CONTENT_TYPE = "Content-Type";
+
private final String _virtualhostnode;
private final String _virtualhost;
- private final HttpHost _management;
- private final CredentialsProvider _credentialsProvider;
- private final HttpClientContext _httpClientContext;
+ private final URI _management;
+ private final HttpClient _httpClient;
+ private final String _authorization;
private final InitialContext _context;
private final String _queueApiUrl;
@@ -71,18 +67,22 @@ public class JavaBrokerAdmin implements Admin
final Hashtable<String, String> env = new Hashtable<>();
_context = new InitialContext(env);
- final String managementUser =
System.getProperty("joramtests.manangement-user", "guest");
+ final String managementUser =
System.getProperty("joramtests.manangement-username",
+ System.getProperty("joramtests.manangement-user", "guest"));
final String managementPassword =
System.getProperty("joramtests.manangement-password", "guest");
_virtualhostnode =
System.getProperty("joramtests.broker-virtualhostnode", "default");
_virtualhost = System.getProperty("joramtests.broker-virtualhost",
"default");
- _management =
HttpHost.create(System.getProperty("joramtests.manangement-url",
"http://localhost:8080"));
+ _management = new URI(System.getProperty("joramtests.manangement-url",
"http://localhost:8080"));
+ _httpClient = HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_1_1)
+ .followRedirects(HttpClient.Redirect.NEVER)
+ .proxy(HttpClient.Builder.NO_PROXY)
+ .build();
+ _authorization = getAuthorization(managementUser, managementPassword);
_queueApiUrl = System.getProperty("joramtests.manangement-api-queue",
"/api/latest/queue/%s/%s/%s");
_topicApiUrl = System.getProperty("joramtests.manangement-api-topic",
"/api/latest/exchange/%s/%s/%s");
-
- _credentialsProvider = getCredentialsProvider(managementUser,
managementPassword);
- _httpClientContext = getHttpClientContext(_management);
}
@@ -193,9 +193,7 @@ public class JavaBrokerAdmin implements Admin
if (!clazz.isInstance(object))
{
throw new IllegalArgumentException(String.format("'%s' has
unexpected type. It is a '%s', but expected a '%s'",
- name,
-
object.getClass().getName(),
-
clazz.getName()));
+ name, object.getClass().getName(), clazz.getName()));
}
}
catch (NamingException e)
@@ -207,43 +205,57 @@ public class JavaBrokerAdmin implements Admin
private void managementCreateQueue(final String name)
{
- final HttpPut put = new HttpPut(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
- final StringEntity input = new StringEntity("{}",
ContentType.APPLICATION_JSON, "UTF_8", false);
- put.setEntity(input);
- executeManagement(put);
+ final String path = String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .header(CONTENT_TYPE, APPLICATION_JSON)
+ .PUT(HttpRequest.BodyPublishers.ofString("{}",
StandardCharsets.UTF_8))
+ .build();
+ executeManagement(request);
}
private void managementCreateTopic(final String name)
{
- final HttpPut put = new HttpPut(String.format(_topicApiUrl,
_virtualhostnode, _virtualhost, name));
- final StringEntity input = new StringEntity("{\"type\" : \"fanout\"}",
ContentType.APPLICATION_JSON, "UTF_8", false);
- put.setEntity(input);
- executeManagement(put);
+ final String path = String.format(_topicApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .header(CONTENT_TYPE, APPLICATION_JSON)
+ .PUT(HttpRequest.BodyPublishers.ofString("{\"type\" :
\"fanout\"}", StandardCharsets.UTF_8))
+ .build();
+ executeManagement(request);
}
private void managementDeleteQueue(final String name)
{
- final HttpDelete delete = new HttpDelete(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
- executeManagement(delete);
+ final String path = String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .DELETE()
+ .build();
+ executeManagement(request);
}
private void managementDeleteTopic(final String name)
{
- final HttpDelete delete = new HttpDelete(String.format(_topicApiUrl,
_virtualhostnode, _virtualhost, name));
- executeManagement(delete);
+ final String path = String.format(_topicApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .DELETE()
+ .build();
+ executeManagement(request);
}
- private void executeManagement(final ClassicHttpRequest httpRequest)
+ private HttpRequest.Builder newManagementRequest(final String path)
{
- try (final CloseableHttpClient httpClient =
HttpClients.custom().setDefaultCredentialsProvider(_credentialsProvider).build();
- final CloseableHttpResponse response =
httpClient.execute(_management, httpRequest, _httpClientContext, reply ->
(CloseableHttpResponse) reply))
+ return
HttpRequest.newBuilder(_management.resolve(path)).header(AUTHORIZATION,
_authorization);
+ }
+
+ private void executeManagement(final HttpRequest httpRequest)
+ {
+ try
{
- final int status = response.getCode();
- final ProtocolVersion version = response.getVersion();
- final String reason = response.getReasonPhrase();
- if (status != 200 && status != 201)
+ final HttpResponse<Void> response = _httpClient.send(httpRequest,
HttpResponse.BodyHandlers.discarding());
+ final int status = response.statusCode();
+ final HttpClient.Version version = response.version();
+ if (status != HTTP_OK && status != HTTP_CREATED)
{
- final String msg = String.format("Failed: HTTP error code: %d,
Version: %s, Reason: %s", status, version, reason);
+ final String msg = String.format("Failed: HTTP error code: %d,
Version: %s", status, version);
throw new RuntimeException(msg);
}
}
@@ -251,21 +263,18 @@ public class JavaBrokerAdmin implements Admin
{
throw new RuntimeException(e);
}
+ catch (InterruptedException e)
+ {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
}
- private HttpClientContext getHttpClientContext(final HttpHost management)
- {
- final BasicAuthCache authCache = new BasicAuthCache();
- authCache.put(management, new BasicScheme());
- final HttpClientContext localContext = HttpClientContext.create();
- localContext.setAuthCache(authCache);
- return localContext;
- }
-
- private CredentialsProvider getCredentialsProvider(final String
managementUser, final String managementPassword)
+ private static String getAuthorization(final String managementUser, final
String managementPassword)
{
- final BasicCredentialsProvider credentialsProvider = new
BasicCredentialsProvider();
- credentialsProvider.setCredentials(new AuthScope("localhost", 8080),
new UsernamePasswordCredentials(managementUser,
managementPassword.toCharArray()));
- return credentialsProvider;
+ final String credentials = managementUser + ":" + managementPassword;
+ final String encodedCredentials = Base64.getEncoder()
+ .encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
+ return "Basic " + encodedCredentials;
}
}
diff --git a/perftests/pom.xml b/perftests/pom.xml
index f5cb2bf340..06826d6285 100644
--- a/perftests/pom.xml
+++ b/perftests/pom.xml
@@ -94,11 +94,6 @@
<scope>provided</scope>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- </dependency>
-
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-ratelimiter</artifactId>
diff --git a/perftests/src/main/assembly/NOTICE
b/perftests/src/main/assembly/NOTICE
index 8903892b05..32d578cbee 100644
--- a/perftests/src/main/assembly/NOTICE
+++ b/perftests/src/main/assembly/NOTICE
@@ -41,13 +41,3 @@ Copyright (c) 2004-2017 QOS.ch
Logback
Copyright (c) 1999-2017, QOS.ch
-
-###############################################
-
-Apache HttpCore
-Copyright (c) 2005-2017 The Apache Software Foundation
-
-###############################################
-
-Apache HttpClient
-Copyright (c) 1999-2017 The Apache Software Foundation
diff --git
a/perftests/src/main/assembly/dependency-verification/DEPENDENCIES_REFERENCE
b/perftests/src/main/assembly/dependency-verification/DEPENDENCIES_REFERENCE
index 5973ab0b87..d12875864b 100644
--- a/perftests/src/main/assembly/dependency-verification/DEPENDENCIES_REFERENCE
+++ b/perftests/src/main/assembly/dependency-verification/DEPENDENCIES_REFERENCE
@@ -65,18 +65,6 @@ From: 'QOS.ch' (http://www.qos.ch)
License: MIT (https://opensource.org/license/mit)
-From: 'The Apache Software Foundation' (https://www.apache.org/)
-
- - Apache HttpClient
(https://hc.apache.org/httpcomponents-client-5.6.x/5.6.2/httpclient5/)
org.apache.httpcomponents.client5:httpclient5:jar:5.6.2
- License: Apache License, Version 2.0
(https://www.apache.org/licenses/LICENSE-2.0.txt)
-
- - Apache HttpComponents Core HTTP/1.1
(https://hc.apache.org/httpcomponents-core-5.4.x/5.4.3/httpcore5/)
org.apache.httpcomponents.core5:httpcore5:jar:5.4.3
- License: Apache License, Version 2.0
(https://www.apache.org/licenses/LICENSE-2.0.txt)
-
- - Apache HttpComponents Core HTTP/2
(https://hc.apache.org/httpcomponents-core-5.4.x/5.4.3/httpcore5-h2/)
org.apache.httpcomponents.core5:httpcore5-h2:jar:5.4.3
- License: Apache License, Version 2.0
(https://www.apache.org/licenses/LICENSE-2.0.txt)
-
-
diff --git
a/perftests/src/main/java/org/apache/qpid/disttest/jms/QpidRestAPIQueueCreator.java
b/perftests/src/main/java/org/apache/qpid/disttest/jms/QpidRestAPIQueueCreator.java
index e5510bbbf0..a6ac7a3cc3 100644
---
a/perftests/src/main/java/org/apache/qpid/disttest/jms/QpidRestAPIQueueCreator.java
+++
b/perftests/src/main/java/org/apache/qpid/disttest/jms/QpidRestAPIQueueCreator.java
@@ -19,12 +19,19 @@
*/
package org.apache.qpid.disttest.jms;
-import java.io.ByteArrayOutputStream;
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_OK;
+
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -37,28 +44,6 @@ import javax.jms.MessageConsumer;
import javax.jms.QueueBrowser;
import javax.jms.Session;
-import org.apache.hc.client5.http.auth.AuthScope;
-import org.apache.hc.client5.http.auth.CredentialsProvider;
-import org.apache.hc.client5.http.auth.CredentialsStore;
-import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
-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.HttpPut;
-import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
-import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
-import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
-import org.apache.hc.client5.http.impl.auth.BasicScheme;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.classic.HttpClients;
-import org.apache.hc.client5.http.protocol.HttpClientContext;
-import org.apache.hc.core5.http.ClassicHttpResponse;
-import org.apache.hc.core5.http.ContentType;
-import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.io.HttpClientResponseHandler;
-import org.apache.hc.core5.http.io.entity.StringEntity;
-import org.apache.qpid.disttest.json.ObjectMapperFactory;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -66,23 +51,28 @@ import tools.jackson.databind.ObjectMapper;
import org.apache.qpid.disttest.DistributedTestException;
import org.apache.qpid.disttest.controller.config.QueueConfig;
+import org.apache.qpid.disttest.json.ObjectMapperFactory;
/**
* Assumes Basic-Auth is enabled
*/
public class QpidRestAPIQueueCreator implements QueueCreator
{
+ private static final String APPLICATION_JSON = "application/json;
charset=UTF-8";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final String CONTENT_TYPE = "Content-Type";
private static final Logger LOGGER =
LoggerFactory.getLogger(QpidRestAPIQueueCreator.class);
private static final int DRAIN_POLL_TIMEOUT =
Integer.getInteger(QUEUE_CREATOR_DRAIN_POLL_TIMEOUT, 500);
+ private static final ObjectMapper OBJECT_MAPPER = new
ObjectMapperFactory().createObjectMapper();
- private final HttpHost _management;
+ private final URI _management;
+ private final HttpClient _httpClient;
+ private final String _authorization;
private final String _virtualhostnode;
private final String _virtualhost;
private final String _queueApiUrl;
private final String _brokerApiUrl;
- private final CredentialsProvider _credentialsProvider;
-
public QpidRestAPIQueueCreator() throws URISyntaxException
{
final String managementUser =
System.getProperty("perftests.manangement-user", "guest");
@@ -91,35 +81,35 @@ public class QpidRestAPIQueueCreator implements QueueCreator
_virtualhostnode =
System.getProperty("perftests.broker-virtualhostnode", "default");
_virtualhost = System.getProperty("perftests.broker-virtualhost",
"default");
- _management =
HttpHost.create(System.getProperty("perftests.manangement-url",
"http://localhost:8080"));
+ _management = new URI(System.getProperty("perftests.manangement-url",
"http://localhost:8080"));
+ _httpClient = HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_1_1)
+ .followRedirects(HttpClient.Redirect.NEVER)
+ .proxy(HttpClient.Builder.NO_PROXY)
+ .build();
+ _authorization = getAuthorization(managementUser, managementPassword);
_queueApiUrl = System.getProperty("perftests.manangement-api-queue",
"/api/latest/queue/%s/%s/%s");
_brokerApiUrl = System.getProperty("perftests.manangement-api-broker",
"/api/latest/broker");
-
- _credentialsProvider = getCredentialsProvider(managementUser,
managementPassword);
}
@Override
- public void createQueues(Connection connection, Session session,
List<QueueConfig> configs)
+ public void createQueues(final Connection connection, final Session
session, final List<QueueConfig> configs)
{
- HttpClientContext context = getHttpClientContext(_management);
-
- for (QueueConfig queueConfig : configs)
+ for (final QueueConfig queueConfig : configs)
{
final String queueName = queueConfig.getName();
- managementCreateQueue(queueName, context);
+ managementCreateQueue(queueName);
}
}
@Override
- public void deleteQueues(Connection connection, Session session,
List<QueueConfig> configs)
+ public void deleteQueues(final Connection connection, final Session
session, final List<QueueConfig> configs)
{
- HttpClientContext context = getHttpClientContext(_management);
-
- for (QueueConfig queueConfig : configs)
+ for (final QueueConfig queueConfig : configs)
{
final String queueName = queueConfig.getName();
drainQueue(connection, queueName);
- managementDeleteQueue(queueName, context);
+ managementDeleteQueue(queueName);
}
}
@@ -158,10 +148,8 @@ public class QpidRestAPIQueueCreator implements
QueueCreator
@Override
public String getProviderVersion(final Connection connection)
{
- HttpClientContext context = getHttpClientContext(_management);
-
- final Map<String, Object> stringObjectMap =
managementQueryBroker(context);
- return stringObjectMap == null ||
stringObjectMap.get("productVersion") == null ? null :
String.valueOf(stringObjectMap.get("productVersion"));
+ final Map<String, Object> stringObjectMap = managementQueryBroker();
+ return stringObjectMap.get("productVersion") == null ? null :
String.valueOf(stringObjectMap.get("productVersion"));
}
private void drainQueue(Connection connection, String queueName)
@@ -248,20 +236,22 @@ public class QpidRestAPIQueueCreator implements
QueueCreator
}
@SuppressWarnings("unchecked")
- private Map<String, Object> managementQueryBroker(final HttpClientContext
context)
+ private Map<String, Object> managementQueryBroker()
{
- HttpGet get = new HttpGet(_brokerApiUrl);
- Object obj = executeManagement(get, context);
+ final HttpRequest request =
newManagementRequest(_brokerApiUrl).GET().build();
+ Object obj = executeManagement(request);
if (obj == null)
{
- throw new IllegalStateException(String.format("Unexpected null
response from management query '%s'", get));
+ final String error = String.format("Unexpected null response from
management query '%s'", request);
+ throw new IllegalStateException(error);
}
else if (obj instanceof Collection)
{
- Iterator itr = ((Collection) obj).iterator();
+ final Iterator<?> itr = ((Collection<?>) obj).iterator();
if (!itr.hasNext())
{
- throw new IllegalStateException(String.format("Unexpected
empty list response from management query '%s'", get));
+ final String error = String.format("Unexpected empty list
response from management query '%s'", request);
+ throw new IllegalStateException(error);
}
obj = itr.next();
}
@@ -272,88 +262,77 @@ public class QpidRestAPIQueueCreator implements
QueueCreator
}
else
{
- throw new IllegalStateException(String.format("Unexpected response
'%s' from management query '%s'", obj, get));
+ final String error = String.format("Unexpected response '%s' from
management query '%s'", obj, request);
+ throw new IllegalStateException(error);
}
}
- private void managementCreateQueue(final String name, final
HttpClientContext context)
+ private void managementCreateQueue(final String name)
+ {
+ final String path = String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .header(CONTENT_TYPE, APPLICATION_JSON)
+ .PUT(HttpRequest.BodyPublishers.ofString("{}",
StandardCharsets.UTF_8))
+ .build();
+ executeManagement(request);
+ }
+
+ private void managementDeleteQueue(final String name)
{
- HttpPut put = new HttpPut(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
- StringEntity input = new StringEntity("{}",
ContentType.APPLICATION_JSON, "UTF_8", false);
- put.setEntity(input);
- executeManagement(put, context);
+ final String path = String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .DELETE()
+ .build();
+ executeManagement(request);
}
- private void managementDeleteQueue(final String name, final
HttpClientContext context)
+ private HttpRequest.Builder newManagementRequest(final String path)
{
- HttpDelete delete = new HttpDelete(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
- executeManagement(delete, context);
+ return
HttpRequest.newBuilder(_management.resolve(path)).header(AUTHORIZATION,
_authorization);
}
- private Object executeManagement(final HttpUriRequest httpRequest, final
HttpClientContext context)
+ private Object executeManagement(final HttpRequest httpRequest)
{
- try (final CloseableHttpClient httpClient = HttpClients.custom()
- .setDefaultCredentialsProvider(_credentialsProvider)
- .build())
+ try
{
- return httpClient.execute(_management, httpRequest, context, new
ResponseHandler());
+ final HttpResponse.BodyHandler<byte[]> bodyHandler =
HttpResponse.BodyHandlers.ofByteArray();
+ final HttpResponse<byte[]> response =
_httpClient.send(httpRequest, bodyHandler);
+ return handleResponse(response);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
+ catch (InterruptedException e)
+ {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
}
- private HttpClientContext getHttpClientContext(final HttpHost management)
- {
- final HttpClientContext localContext = HttpClientContext.create();
- final BasicAuthCache authCache = new BasicAuthCache();
- final BasicScheme basicScheme = new BasicScheme();
- basicScheme.initPreemptive(_credentialsProvider.getCredentials(new
AuthScope(management), localContext));
- authCache.put(management, basicScheme);
- localContext.setAuthCache(authCache);
- return localContext;
- }
-
- private CredentialsProvider getCredentialsProvider(final String
managementUser, final String managementPassword)
+ private static String getAuthorization(final String managementUser, final
String managementPassword)
{
- final URI managementURI =
URI.create(System.getProperty("perftests.manangement-url"));
- final String hostname = managementURI.getHost();
- final int port = managementURI.getPort();
- final AuthScope authScope = new AuthScope(hostname, port);
- final CredentialsStore credentialsProvider = new
BasicCredentialsProvider();
- credentialsProvider.setCredentials(authScope, new
UsernamePasswordCredentials(managementUser, managementPassword.toCharArray()));
- return credentialsProvider;
+ final String credentials = managementUser + ":" + managementPassword;
+ final String encodedCredentials = Base64.getEncoder()
+ .encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
+ return "Basic " + encodedCredentials;
}
- static class ResponseHandler implements HttpClientResponseHandler<Object>
+ private Object handleResponse(final HttpResponse<byte[]> response) throws
IOException
{
- private static final ObjectMapper OBJECT_MAPPER = new
ObjectMapperFactory().createObjectMapper();
-
- @Override
- public Object handleResponse(ClassicHttpResponse response) throws
IOException
+ final int status = response.statusCode();
+ final HttpClient.Version version = response.version();
+ if (status != HTTP_OK && status != HTTP_CREATED)
{
- final int status = response.getCode();
- final ProtocolVersion version = response.getVersion();
- final String reason = response.getReasonPhrase();
- if (status != 200 && status != 201)
- {
- final String msg = String.format("Failed: HTTP error code: %d,
Version: %s, Reason: %s", status, version, reason);
- throw new RuntimeException(msg);
- }
+ final String msg = String.format("Failed: HTTP error code: %d,
Version: %s", status, version);
+ throw new RuntimeException(msg);
+ }
- if (response.getEntity() != null)
- {
- try (ByteArrayOutputStream bos = new ByteArrayOutputStream())
- {
- response.getEntity().writeTo(bos);
- if (bos.size() > 0)
- {
- return OBJECT_MAPPER.readValue(bos.toByteArray(),
Object.class);
- }
- }
- }
- return null;
+ final byte[] body = response.body();
+ if (body.length > 0)
+ {
+ return OBJECT_MAPPER.readValue(body, Object.class);
}
+ return null;
}
}
diff --git a/pom.xml b/pom.xml
index d682ed9d2d..11834c8192 100644
--- a/pom.xml
+++ b/pom.xml
@@ -135,7 +135,6 @@
<netty-version>4.2.16.Final</netty-version>
<hamcrest-version>3.0</hamcrest-version>
<maven-resolver-version>2.0.22</maven-resolver-version>
- <httpclient-version>5.6.2</httpclient-version>
<qpid-jms-client-version>1.16.0</qpid-jms-client-version>
<qpid-jms-client-amqp-0-x-version>6.4.0</qpid-jms-client-amqp-0-x-version>
<nashorn-version>15.7</nashorn-version>
@@ -717,16 +716,6 @@
<artifactId>jfreechart</artifactId>
<version>${jfreechart-version}</version>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- <version>${httpclient-version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5-fluent</artifactId>
- <version>${httpclient-version}</version>
- </dependency>
<dependency>
<groupId>org.dojotoolkit</groupId>
<artifactId>dojo</artifactId>
diff --git a/tck/pom.xml b/tck/pom.xml
index 8e3c4ae9b9..73c0ba41e7 100644
--- a/tck/pom.xml
+++ b/tck/pom.xml
@@ -60,10 +60,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.httpcomponents.client5</groupId>
- <artifactId>httpclient5</artifactId>
- </dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
diff --git a/tck/src/main/java/org/apache/qpid/tck/ManageQpidJMSResources.java
b/tck/src/main/java/org/apache/qpid/tck/ManageQpidJMSResources.java
index c7e45aef77..34ec4e6154 100644
--- a/tck/src/main/java/org/apache/qpid/tck/ManageQpidJMSResources.java
+++ b/tck/src/main/java/org/apache/qpid/tck/ManageQpidJMSResources.java
@@ -21,9 +21,18 @@
package org.apache.qpid.tck;
+import static java.net.HttpURLConnection.HTTP_CREATED;
+import static java.net.HttpURLConnection.HTTP_OK;
+
import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
import java.net.URISyntaxException;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import java.util.List;
import java.util.Map;
@@ -35,29 +44,14 @@ import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
-import org.apache.hc.client5.http.auth.AuthScope;
-import org.apache.hc.client5.http.auth.CredentialsProvider;
-import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
-import org.apache.hc.client5.http.classic.methods.HttpDelete;
-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.HttpUriRequest;
-import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
-import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
-import org.apache.hc.client5.http.impl.auth.BasicScheme;
-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.client5.http.protocol.HttpClientContext;
-import org.apache.hc.core5.http.ContentType;
-import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.io.entity.StringEntity;
-
/**
* Used pre/post-integration-test to create/delete JMS resources required for
the TCK run.
*/
public class ManageQpidJMSResources
{
+ private static final String APPLICATION_JSON = "application/json;
charset=UTF-8";
+ private static final String AUTHORIZATION = "Authorization";
+ private static final String CONTENT_TYPE = "Content-Type";
private static final Logger LOGGER =
LoggerFactory.getLogger(ManageQpidJMSResources.class);
private static final TypeReference<List<Map<String, Object>>>
VALUE_TYPE_REF =
@@ -68,13 +62,13 @@ public class ManageQpidJMSResources
private final String _virtualhostnode;
private final String _virtualhost;
- private final HttpHost _management;
+ private final URI _management;
+ private final HttpClient _httpClient;
+ private final String _authorization;
private final String _queueApiUrl;
private final String _queueApiClearQueueUrl;
private final String _topicApiUrl;
private final ObjectMapper _objectMapper;
- private final CredentialsProvider _credentialsProvider;
- private final HttpClientContext _httpClientContext;
private enum NodeType
{
@@ -107,13 +101,16 @@ public class ManageQpidJMSResources
_virtualhostnode = System.getProperty("tck.broker-virtualhostnode",
"default");
_virtualhost = System.getProperty("tck.broker-virtualhost", "default");
- _management = HttpHost.create(System.getProperty("tck.management-url",
"http://localhost:8080"));
+ _management = new URI(System.getProperty("tck.management-url",
"http://localhost:8080"));
+ _httpClient = HttpClient.newBuilder()
+ .version(HttpClient.Version.HTTP_1_1)
+ .followRedirects(HttpClient.Redirect.NEVER)
+ .proxy(HttpClient.Builder.NO_PROXY)
+ .build();
+ _authorization = getAuthorization(managementUser, managementPassword);
_queueApiUrl = System.getProperty("tck.management-api-queue",
"/api/latest/queue/%s/%s/%s");
_queueApiClearQueueUrl =
System.getProperty("tck.management-api-queue-clear",
"/api/latest/queue/%s/%s/%s/clearQueue");
_topicApiUrl = System.getProperty("tck.management-api-topic",
"/api/latest/exchange/%s/%s/%s");
-
- _credentialsProvider = getCredentialsProvider(managementUser,
managementPassword);
- _httpClientContext = getHttpClientContext(_management);
}
private void createResources() throws IOException
@@ -187,80 +184,83 @@ public class ManageQpidJMSResources
private void managementCreateQueue(final String name, final Map<String,
Object> arguments) throws IOException
{
- HttpPut put = new HttpPut(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
-
- management(put, arguments);
+ management("PUT", String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name), arguments);
}
private void managementClearQueue(final String name) throws IOException
{
- HttpPost post = new HttpPost(String.format(_queueApiClearQueueUrl,
_virtualhostnode, _virtualhost, name));
-
- management(post, Map.of());
+ final String path = String.format(_queueApiClearQueueUrl,
_virtualhostnode, _virtualhost, name);
+ management("POST", path, Map.of());
}
private void managementCreateExchange(final String name, final Map<String,
Object> arguments) throws IOException
{
- HttpPut put = new HttpPut(String.format(_topicApiUrl,
_virtualhostnode, _virtualhost, name));
-
- management(put, arguments);
+ final String path = String.format(_topicApiUrl, _virtualhostnode,
_virtualhost, name);
+ management("PUT", path, arguments);
}
+
private void managementDeleteQueue(final String name)
{
- HttpDelete delete = new HttpDelete(String.format(_queueApiUrl,
_virtualhostnode, _virtualhost, name));
- executeManagement(delete);
+ final String path = String.format(_queueApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .DELETE()
+ .build();
+ executeManagement(request);
}
private void managementDeleteExchange(final String name)
{
- HttpDelete delete = new HttpDelete(String.format(_topicApiUrl,
_virtualhostnode, _virtualhost, name));
- executeManagement(delete);
+ final String path = String.format(_topicApiUrl, _virtualhostnode,
_virtualhost, name);
+ final HttpRequest request = newManagementRequest(path)
+ .DELETE()
+ .build();
+ executeManagement(request);
}
- private void management(final HttpUriRequest request, final Object obj)
throws IOException
+ private void management(final String method, final String path, final
Object obj) throws IOException
{
- StringEntity input = new
StringEntity(_objectMapper.writeValueAsString(obj),
ContentType.APPLICATION_JSON, "UTF_8", false);
- request.setEntity(input);
+ final String body = _objectMapper.writeValueAsString(obj);
+ final HttpRequest.BodyPublisher bodyPublisher =
HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8);
+ final HttpRequest request = newManagementRequest(path)
+ .header(CONTENT_TYPE, APPLICATION_JSON)
+ .method(method, bodyPublisher)
+ .build();
- int statusCode = executeManagement(request);
- if (statusCode != 200 && statusCode != 201)
+ final int statusCode = executeManagement(request);
+ if (statusCode != HTTP_OK && statusCode != HTTP_CREATED)
{
throw new RuntimeException(String.format("Failed : HTTP error code
: %d", statusCode));
}
}
- private int executeManagement(final HttpUriRequest httpRequest)
+ private HttpRequest.Builder newManagementRequest(final String path)
{
+ return
HttpRequest.newBuilder(_management.resolve(path)).header(AUTHORIZATION,
_authorization);
+ }
- try(CloseableHttpClient httpClient = HttpClients.custom()
-
.setDefaultCredentialsProvider(_credentialsProvider)
- .build())
+ private int executeManagement(final HttpRequest httpRequest)
+ {
+ try
{
- try (CloseableHttpResponse response =
httpClient.execute(_management, httpRequest, _httpClientContext, reply ->
(CloseableHttpResponse) reply))
- {
- return response.getCode();
- }
+ final HttpResponse<Void> response = _httpClient.send(httpRequest,
HttpResponse.BodyHandlers.discarding());
+ return response.statusCode();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
+ catch (InterruptedException e)
+ {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ }
}
- private HttpClientContext getHttpClientContext(final HttpHost management)
- {
- final BasicAuthCache authCache = new BasicAuthCache();
- authCache.put(management, new BasicScheme());
- HttpClientContext localContext = HttpClientContext.create();
- localContext.setAuthCache(authCache);
- return localContext;
- }
-
- private CredentialsProvider getCredentialsProvider(final String
managementUser, final String managementPassword)
+ private static String getAuthorization(final String managementUser, final
String managementPassword)
{
- final BasicCredentialsProvider credentialsProvider = new
BasicCredentialsProvider();
- credentialsProvider.setCredentials(new AuthScope("localhost", 8080),
new UsernamePasswordCredentials(managementUser,
managementPassword.toCharArray()));
- return credentialsProvider;
+ final String credentials = managementUser + ":" + managementPassword;
+ final String encodedCredentials = Base64.getEncoder()
+ .encodeToString(credentials.getBytes(StandardCharsets.UTF_8));
+ return "Basic " + encodedCredentials;
}
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]