From 1d3fa7879103448d036dd410e393c2120b89182a Mon Sep 17 00:00:00 2001 From: yachtintheband Date: Thu, 7 Dec 2023 16:57:59 +0100 Subject: [PATCH] feat(client/java): upgrade Apache HttpClient to 5 related to camunda/camunda-bpm-platform#3945 Co-authored-by: Daniel Kelemen --- clients/java/client/pom.xml | 14 +---- .../camunda/bpm/client/rule/EngineRule.java | 32 +++++----- .../bpm/client/impl/EngineClientLogger.java | 2 +- .../bpm/client/impl/RequestExecutor.java | 60 +++++++++---------- .../interceptor/auth/BasicAuthProvider.java | 2 +- .../impl/RequestInterceptorHandler.java | 13 ++-- .../impl/mapper/ByteArrayValueMapper.java | 6 +- .../variable/impl/mapper/FileValueMapper.java | 2 +- 8 files changed, 62 insertions(+), 69 deletions(-) diff --git a/clients/java/client/pom.xml b/clients/java/client/pom.xml index e38776e1eeb..5b032ffe5e7 100644 --- a/clients/java/client/pom.xml +++ b/clients/java/client/pom.xml @@ -14,8 +14,7 @@ - 4.5.13 - + 5.3 ${project.build.directory}/camunda-tomcat ${engine.runtime}/server/apache-tomcat-${version.tomcat} ${tomcat.connector.http.port} @@ -63,8 +62,8 @@ - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 ${version.httpclient} @@ -99,13 +98,6 @@ test - - org.apache.httpcomponents - httpmime - ${version.httpclient} - test - - org.skyscreamer jsonassert diff --git a/clients/java/client/src/it/java/org/camunda/bpm/client/rule/EngineRule.java b/clients/java/client/src/it/java/org/camunda/bpm/client/rule/EngineRule.java index 94ac5f108fe..46406d1b1c6 100644 --- a/clients/java/client/src/it/java/org/camunda/bpm/client/rule/EngineRule.java +++ b/clients/java/client/src/it/java/org/camunda/bpm/client/rule/EngineRule.java @@ -34,20 +34,20 @@ import java.util.Set; import java.util.function.Supplier; -import org.apache.http.HttpEntity; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.entity.mime.MultipartEntityBuilder; -import org.apache.http.impl.client.AbstractResponseHandler; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.client5.http.ClientProtocolException; +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.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpUriRequest; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder; +import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.camunda.bpm.client.dto.HistoricProcessInstanceDto; import org.camunda.bpm.client.dto.IncidentDto; import org.camunda.bpm.client.dto.ProcessDefinitionDto; @@ -398,8 +398,8 @@ protected T executeRequest(HttpUriRequest httpRequest, Class responseDtoC } } - protected ResponseHandler handleResponse(final Class responseDtoClass) { - return new AbstractResponseHandler() { + protected HttpClientResponseHandler handleResponse(final Class responseDtoClass) { + return new AbstractHttpClientResponseHandler<>() { @Override public T handleEntity(HttpEntity responseEntity) { T deserializedResponse = null; diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/impl/EngineClientLogger.java b/clients/java/client/src/main/java/org/camunda/bpm/client/impl/EngineClientLogger.java index 0975c3d4085..02e8297ac77 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/impl/EngineClientLogger.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/impl/EngineClientLogger.java @@ -16,7 +16,7 @@ */ package org.camunda.bpm.client.impl; -import org.apache.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequest; import org.camunda.bpm.client.exception.RestException; import java.io.IOException; diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/impl/RequestExecutor.java b/clients/java/client/src/main/java/org/camunda/bpm/client/impl/RequestExecutor.java index d1da5c2ca52..79370607e98 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/impl/RequestExecutor.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/impl/RequestExecutor.java @@ -18,23 +18,24 @@ import java.io.IOException; import java.io.InputStream; - -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; -import org.apache.http.client.HttpClient; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.client.methods.RequestBuilder; -import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.impl.client.AbstractResponseHandler; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicHeader; -import org.apache.http.util.EntityUtils; -import org.camunda.bpm.client.exception.EngineException; +import java.net.URI; + +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.ClassicHttpRequest; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.HttpHeaders; +import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; +import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; +import org.apache.hc.core5.http.message.BasicHeader; +import org.apache.hc.core5.http.message.StatusLine; import org.camunda.bpm.client.exception.RestException; import org.camunda.bpm.client.interceptor.impl.RequestInterceptorHandler; import org.camunda.commons.utils.IoUtil; @@ -65,25 +66,25 @@ protected RequestExecutor(RequestInterceptorHandler requestInterceptorHandler, O protected T postRequest(String resourceUrl, RequestDto requestDto, Class responseClass) { ByteArrayEntity serializedRequest = serializeRequest(requestDto); - HttpUriRequest httpRequest = RequestBuilder.post(resourceUrl) + ClassicHttpRequest httpRequest = ClassicRequestBuilder.post(URI.create(resourceUrl).normalize()) .addHeader(HEADER_USER_AGENT) .addHeader(HEADER_CONTENT_TYPE_JSON) .setEntity(serializedRequest) .build(); - + return executeRequest(httpRequest, responseClass); } protected byte[] getRequest(String resourceUrl) { - HttpUriRequest httpRequest = RequestBuilder.get(resourceUrl) + ClassicHttpRequest httpRequest = ClassicRequestBuilder.get(URI.create(resourceUrl).normalize()) .addHeader(HEADER_USER_AGENT) .addHeader(HEADER_CONTENT_TYPE_JSON) .build(); - + return executeRequest(httpRequest, byte[].class); } - protected T executeRequest(HttpUriRequest httpRequest, Class responseClass) { + protected T executeRequest(ClassicHttpRequest httpRequest, Class responseClass) { try { return httpClient.execute(httpRequest, handleResponse(responseClass)); @@ -96,9 +97,8 @@ protected T executeRequest(HttpUriRequest httpRequest, Class responseClas } } - protected ResponseHandler handleResponse(final Class responseClass) { - return new AbstractResponseHandler() { - @SuppressWarnings("unchecked") + protected HttpClientResponseHandler handleResponse(final Class responseClass) { + return new AbstractHttpClientResponseHandler<>() { public T handleEntity(HttpEntity responseEntity) throws IOException { T response = null; if (responseClass.isAssignableFrom(byte[].class)) { @@ -125,8 +125,8 @@ public T handleEntity(HttpEntity responseEntity) throws IOException { } @Override - public T handleResponse(HttpResponse response) throws IOException { - final StatusLine statusLine = response.getStatusLine(); + public T handleResponse(ClassicHttpResponse response) throws IOException { + final StatusLine statusLine = new StatusLine(response); final HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { try { @@ -168,7 +168,7 @@ protected T deserializeResponse(HttpEntity httpEntity, Class responseClas } protected ByteArrayEntity serializeRequest(RequestDto dto) { - byte[] serializedRequest = null; + byte[] serializedRequest; try { serializedRequest = objectMapper.writeValueAsBytes(dto); @@ -178,7 +178,7 @@ protected ByteArrayEntity serializeRequest(RequestDto dto) { ByteArrayEntity byteArrayEntity = null; if (serializedRequest != null) { - byteArrayEntity = new ByteArrayEntity(serializedRequest); + byteArrayEntity = new ByteArrayEntity(serializedRequest, ContentType.APPLICATION_JSON); } return byteArrayEntity; @@ -187,7 +187,7 @@ protected ByteArrayEntity serializeRequest(RequestDto dto) { protected void initHttpClient(RequestInterceptorHandler requestInterceptorHandler) { HttpClientBuilder httpClientBuilder = HttpClients.custom() .useSystemProperties() - .addInterceptorLast(requestInterceptorHandler); + .addRequestInterceptorLast(requestInterceptorHandler); this.httpClient = httpClientBuilder.build(); } diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/auth/BasicAuthProvider.java b/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/auth/BasicAuthProvider.java index 94b3f7e95d8..73c838cba66 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/auth/BasicAuthProvider.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/auth/BasicAuthProvider.java @@ -23,7 +23,7 @@ import java.nio.charset.Charset; import java.util.Base64; -import static org.apache.http.HttpHeaders.AUTHORIZATION; +import static org.apache.hc.core5.http.HttpHeaders.AUTHORIZATION; /** *

Provides HTTP Basic Authentication by using the request interceptor api

diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/impl/RequestInterceptorHandler.java b/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/impl/RequestInterceptorHandler.java index 49a5eb807af..9abaa669a12 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/impl/RequestInterceptorHandler.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/interceptor/impl/RequestInterceptorHandler.java @@ -16,11 +16,12 @@ */ package org.camunda.bpm.client.interceptor.impl; -import org.apache.http.HttpException; -import org.apache.http.HttpRequest; -import org.apache.http.HttpRequestInterceptor; -import org.apache.http.message.BasicHeader; -import org.apache.http.protocol.HttpContext; +import org.apache.hc.core5.http.EntityDetails; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpRequest; +import org.apache.hc.core5.http.HttpRequestInterceptor; +import org.apache.hc.core5.http.message.BasicHeader; +import org.apache.hc.core5.http.protocol.HttpContext; import org.camunda.bpm.client.impl.EngineClientLogger; import org.camunda.bpm.client.impl.ExternalTaskClientLogger; import org.camunda.bpm.client.interceptor.ClientRequestInterceptor; @@ -43,7 +44,7 @@ public RequestInterceptorHandler(List interceptors) { } @Override - public void process(HttpRequest httpRequest, HttpContext context) throws HttpException, IOException { + public void process(HttpRequest httpRequest, EntityDetails details, HttpContext context) throws HttpException, IOException { ClientRequestContextImpl interceptedRequest = new ClientRequestContextImpl(); interceptors.forEach((ClientRequestInterceptor requestInterceptor) -> { try { diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/ByteArrayValueMapper.java b/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/ByteArrayValueMapper.java index 7a6b08edb11..e470791d3ac 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/ByteArrayValueMapper.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/ByteArrayValueMapper.java @@ -18,7 +18,7 @@ import java.io.InputStream; -import org.apache.commons.codec.binary.Base64; +import org.apache.hc.client5.http.utils.Base64; import org.camunda.bpm.client.variable.impl.TypedValueField; import org.camunda.bpm.engine.variable.Variables; import org.camunda.bpm.engine.variable.impl.value.UntypedValueImpl; @@ -38,7 +38,7 @@ public ByteArrayValueMapper() { } public BytesValue convertToTypedValue(UntypedValueImpl untypedValue) { - byte[] byteArr = null; + byte[] byteArr; Object value = untypedValue.getValue(); if (value instanceof byte[]) { @@ -63,7 +63,7 @@ public BytesValue readValue(TypedValueField typedValueField) { } public void writeValue(BytesValue byteValue, TypedValueField typedValueField) { - byte[] bytes = (byte[]) byteValue.getValue(); + byte[] bytes = byteValue.getValue(); if (bytes != null) { typedValueField.setValue(Base64.encodeBase64String(bytes)); diff --git a/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/FileValueMapper.java b/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/FileValueMapper.java index 0b95c9f875b..67c61a93039 100644 --- a/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/FileValueMapper.java +++ b/clients/java/client/src/main/java/org/camunda/bpm/client/variable/impl/mapper/FileValueMapper.java @@ -24,7 +24,7 @@ import java.util.HashMap; import java.util.Map; -import org.apache.commons.codec.binary.Base64; +import org.apache.hc.client5.http.utils.Base64; import org.camunda.bpm.client.impl.EngineClient; import org.camunda.bpm.client.variable.impl.AbstractTypedValueMapper; import org.camunda.bpm.client.variable.impl.TypedValueField;