Skip to content

Commit

Permalink
feat(client/java): upgrade Apache HttpClient to 5
Browse files Browse the repository at this point in the history
related to #3945

Co-authored-by: Daniel Kelemen <[email protected]>
  • Loading branch information
yachtintheband and danielkelemen committed Dec 15, 2023
1 parent 2581f58 commit 1d3fa78
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 69 deletions.
14 changes: 3 additions & 11 deletions clients/java/client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
</parent>

<properties>
<version.httpclient>4.5.13</version.httpclient>

<version.httpclient>5.3</version.httpclient>
<engine.runtime>${project.build.directory}/camunda-tomcat</engine.runtime>
<tomcat.runtime>${engine.runtime}/server/apache-tomcat-${version.tomcat}</tomcat.runtime>
<http.port>${tomcat.connector.http.port}</http.port>
Expand Down Expand Up @@ -63,8 +62,8 @@
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${version.httpclient}</version>
</dependency>

Expand Down Expand Up @@ -99,13 +98,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${version.httpclient}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -398,8 +398,8 @@ protected <T> T executeRequest(HttpUriRequest httpRequest, Class<T> responseDtoC
}
}

protected <T> ResponseHandler<T> handleResponse(final Class<T> responseDtoClass) {
return new AbstractResponseHandler<T>() {
protected <T> HttpClientResponseHandler<T> handleResponse(final Class<T> responseDtoClass) {
return new AbstractHttpClientResponseHandler<>() {
@Override
public T handleEntity(HttpEntity responseEntity) {
T deserializedResponse = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,25 +66,25 @@ protected RequestExecutor(RequestInterceptorHandler requestInterceptorHandler, O

protected <T> T postRequest(String resourceUrl, RequestDto requestDto, Class<T> 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> T executeRequest(HttpUriRequest httpRequest, Class<T> responseClass) {
protected <T> T executeRequest(ClassicHttpRequest httpRequest, Class<T> responseClass) {
try {
return httpClient.execute(httpRequest, handleResponse(responseClass));

Expand All @@ -96,9 +97,8 @@ protected <T> T executeRequest(HttpUriRequest httpRequest, Class<T> responseClas
}
}

protected <T> ResponseHandler<T> handleResponse(final Class<T> responseClass) {
return new AbstractResponseHandler<T>() {
@SuppressWarnings("unchecked")
protected <T> HttpClientResponseHandler<T> handleResponse(final Class<T> responseClass) {
return new AbstractHttpClientResponseHandler<>() {
public T handleEntity(HttpEntity responseEntity) throws IOException {
T response = null;
if (responseClass.isAssignableFrom(byte[].class)) {
Expand All @@ -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 {
Expand Down Expand Up @@ -168,7 +168,7 @@ protected <T> T deserializeResponse(HttpEntity httpEntity, Class<T> responseClas
}

protected ByteArrayEntity serializeRequest(RequestDto dto) {
byte[] serializedRequest = null;
byte[] serializedRequest;

try {
serializedRequest = objectMapper.writeValueAsBytes(dto);
Expand All @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>Provides HTTP Basic Authentication by using the request interceptor api</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +44,7 @@ public RequestInterceptorHandler(List<ClientRequestInterceptor> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +38,7 @@ public ByteArrayValueMapper() {
}

public BytesValue convertToTypedValue(UntypedValueImpl untypedValue) {
byte[] byteArr = null;
byte[] byteArr;

Object value = untypedValue.getValue();
if (value instanceof byte[]) {
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1d3fa78

Please sign in to comment.