Skip to content

Commit 364fff1

Browse files
committed
Upgrade httpcomponents from 4.5.14 to 5.5
1 parent 0e3630b commit 364fff1

File tree

7 files changed

+81
-156
lines changed

7 files changed

+81
-156
lines changed

pom.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
<spring.version>6.2.7</spring.version>
128128
<solr.version>9.8.1</solr.version>
129129
<pekko.version>1.1.3</pekko.version>
130-
<httpcomponents.version>4.5.14</httpcomponents.version>
130+
<httpcomponents.version>5.5</httpcomponents.version>
131131
<commons_ip2.version>2.10.0</commons_ip2.version>
132132
<metrics.version>3.2.6</metrics.version>
133133
<roda_community_url>https://roda-community.org</roda_community_url>
@@ -875,14 +875,9 @@
875875
</exclusions>
876876
</dependency>
877877
<dependency>
878-
<groupId>org.apache.httpcomponents</groupId>
879-
<artifactId>httpclient</artifactId>
880-
<version>${httpcomponents.version}</version>
881-
</dependency>
882-
<dependency>
883-
<groupId>org.apache.httpcomponents</groupId>
884-
<artifactId>httpmime</artifactId>
885-
<version>${httpcomponents.version}</version>
878+
<groupId>org.apache.httpcomponents.client5</groupId>
879+
<artifactId>httpclient5</artifactId>
880+
<version>5.5</version>
886881
</dependency>
887882
<!-- Templating system dependencies -->
888883
<dependency>

roda-common/roda-common-utils/pom.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@
3737
<artifactId>commons-io</artifactId>
3838
</dependency>
3939
<dependency>
40-
<groupId>org.apache.httpcomponents</groupId>
41-
<artifactId>httpclient</artifactId>
42-
</dependency>
43-
<dependency>
44-
<groupId>org.apache.httpcomponents</groupId>
45-
<artifactId>httpmime</artifactId>
40+
<groupId>org.apache.httpcomponents.client5</groupId>
41+
<artifactId>httpclient5</artifactId>
4642
</dependency>
4743
<dependency>
4844
<groupId>org.springframework</groupId>

roda-common/roda-common-utils/src/main/java/org/roda/core/util/RESTClientUtility.java

Lines changed: 69 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,32 @@
99

1010
import java.io.File;
1111
import java.io.FileInputStream;
12-
import java.io.FileNotFoundException;
1312
import java.io.IOException;
1413
import java.io.InputStream;
1514
import java.io.Serializable;
1615
import java.nio.CharBuffer;
16+
import java.nio.charset.Charset;
1717
import java.nio.charset.StandardCharsets;
1818
import java.nio.file.Files;
1919
import java.nio.file.Path;
2020

2121
import org.apache.commons.io.FilenameUtils;
2222
import org.apache.commons.lang3.ArrayUtils;
23-
import org.apache.http.HttpEntity;
24-
import org.apache.http.HttpResponse;
25-
import org.apache.http.client.methods.HttpPost;
26-
import org.apache.http.entity.ContentType;
27-
import org.apache.http.entity.StringEntity;
28-
import org.apache.http.entity.mime.HttpMultipartMode;
29-
import org.apache.http.entity.mime.MultipartEntityBuilder;
30-
import org.apache.http.impl.client.CloseableHttpClient;
31-
import org.apache.http.impl.client.HttpClientBuilder;
32-
import org.apache.http.util.EntityUtils;
23+
import org.apache.hc.client5.http.classic.methods.HttpPost;
24+
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
25+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
26+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
27+
import org.apache.hc.client5.http.impl.classic.HttpClients;
28+
import org.apache.hc.core5.http.ContentType;
29+
import org.apache.hc.core5.http.io.entity.EntityUtils;
30+
import org.apache.hc.core5.http.io.entity.StringEntity;
3331
import org.roda.core.data.common.RodaConstants;
3432
import org.roda.core.data.common.SecureString;
3533
import org.roda.core.data.exceptions.GenericException;
3634
import org.roda.core.data.exceptions.RODAException;
3735
import org.roda.core.data.utils.JsonUtils;
3836
import org.roda.core.data.v2.accessToken.AccessToken;
3937

40-
import com.fasterxml.jackson.databind.JsonNode;
41-
4238
/**
4339
* 20160824 hsilva: I think someone, outside RODA code base, is using this
4440
* method via maven dependency
@@ -52,168 +48,106 @@ private RESTClientUtility() {
5248
// do nothing
5349
}
5450

55-
public static <T extends Serializable> T sendPostRequest(T element, Class<T> elementClass, String url,
56-
String resource, String username, String password) throws RODAException {
57-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
58-
String basicAuthToken = new String(Base64.encode((username + ":" + password).getBytes()));
59-
HttpPost httpPost = new HttpPost(url + resource);
60-
httpPost.setHeader("Authorization", "Basic " + basicAuthToken);
61-
httpPost.addHeader("content-type", "application/json");
62-
httpPost.addHeader("Accept", "application/json");
51+
public static <T extends Serializable> T sendPostRequestWithoutBodyHttp5(Class<T> elementClass, String url,
52+
String resource, AccessToken accessToken) throws GenericException {
53+
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
54+
HttpPost httpPost = new HttpPost(url + resource);
55+
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
56+
httpPost.addHeader("content-type", "application/json");
57+
httpPost.addHeader("Accept", "application/json");
6358

64-
try {
65-
httpPost.setEntity(new StringEntity(JsonUtils.getJsonFromObject(element)));
66-
HttpResponse response;
67-
response = httpClient.execute(httpPost);
68-
HttpEntity responseEntity = response.getEntity();
59+
Result result = httpclient.execute(httpPost, response -> new Result(response.getCode(),
60+
EntityUtils.toString(response.getEntity(), Charset.defaultCharset())));
6961

70-
int responseStatusCode = response.getStatusLine().getStatusCode();
71-
if (responseStatusCode == 201) {
72-
return JsonUtils.getObjectFromJson(responseEntity.getContent(), elementClass);
73-
} else {
74-
throw new RODAException("POST request response status code: " + responseStatusCode);
75-
}
76-
} catch (IOException e) {
77-
throw new RODAException("Error sending POST request", e);
78-
}
79-
}
80-
81-
public static <T extends Serializable> T sendPostRequestWithoutBody(Class<T> elementClass, String url, String resource,
82-
AccessToken accessToken) throws GenericException {
83-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
84-
HttpPost httpPost = new HttpPost(url + resource);
85-
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
86-
httpPost.addHeader("content-type", "application/json");
87-
httpPost.addHeader("Accept", "application/json");
88-
89-
try {
90-
HttpResponse response;
91-
response = httpClient.execute(httpPost);
92-
HttpEntity responseEntity = response.getEntity();
93-
int responseStatusCode = response.getStatusLine().getStatusCode();
94-
95-
if (responseStatusCode == 201) {
62+
if (result.statusCode == 201) {
9663
if (elementClass != null) {
97-
return JsonUtils.getObjectFromJson(responseEntity.getContent(), elementClass);
64+
return JsonUtils.getObjectFromJson(result.content, elementClass);
9865
} else {
9966
return null;
10067
}
10168
} else {
102-
throw new GenericException("POST request response status code: " + responseStatusCode);
69+
throw new GenericException("POST request response status code: " + result.statusCode);
10370
}
10471
} catch (IOException e) {
10572
throw new GenericException("Error sending POST request", e);
10673
}
10774
}
10875

109-
public static <T extends Serializable> T sendPostRequest(Object element, Class<T> elementClass, String url,
76+
public static <T extends Serializable> T sendPostRequestHttpClient5(Object element, Class<T> elementClass, String url,
11077
String resource, AccessToken accessToken) throws GenericException {
111-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
112-
HttpPost httpPost = new HttpPost(url + resource);
113-
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
114-
httpPost.addHeader("content-type", "application/json");
115-
httpPost.addHeader("Accept", "application/json");
78+
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
79+
HttpPost httpPost = new HttpPost(url + resource);
80+
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
81+
httpPost.addHeader("content-type", "application/json");
82+
httpPost.addHeader("Accept", "application/json");
11683

117-
try {
11884
httpPost.setEntity(new StringEntity(JsonUtils.getJsonFromObject(element)));
119-
HttpResponse response;
120-
response = httpClient.execute(httpPost);
121-
HttpEntity responseEntity = response.getEntity();
12285

123-
int responseStatusCode = response.getStatusLine().getStatusCode();
124-
if (responseStatusCode == 201) {
86+
Result result = httpclient.execute(httpPost, response -> new Result(response.getCode(),
87+
EntityUtils.toString(response.getEntity(), Charset.defaultCharset())));
88+
89+
if (result.statusCode == 201) {
12590
if (elementClass != null) {
126-
return JsonUtils.getObjectFromJson(responseEntity.getContent(), elementClass);
91+
return JsonUtils.getObjectFromJson(result.content, elementClass);
12792
} else {
12893
return null;
12994
}
13095
} else {
131-
throw new GenericException("POST request response status code: " + responseStatusCode);
96+
throw new GenericException("POST request response status code: " + result.statusCode);
13297
}
13398
} catch (IOException e) {
13499
throw new GenericException("Error sending POST request", e);
135100
}
136101
}
137102

138-
public static int sendPostRequestWithCompressedFile(String url, String resource, Path path, AccessToken accessToken)
139-
throws RODAException, IOException {
140-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
141-
142-
HttpPost httpPost = new HttpPost(url + resource);
143-
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
144-
145-
InputStream inputStream = Files.newInputStream(path.toFile().toPath());
146-
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
147-
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
148-
builder.addBinaryBody(RodaConstants.API_QUERY_KEY_FILE, inputStream, ContentType.create("application/zip"),
149-
path.getFileName().toString());
103+
public static int sendPostRequestWithCompressedFileHttp5(String url, String resource, Path path,
104+
AccessToken accessToken) throws RODAException {
105+
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
106+
HttpPost httpPost = new HttpPost(url + resource);
107+
httpPost.addHeader("Authorization", "Bearer " + accessToken.getToken());
150108

151-
HttpEntity entity = builder.build();
109+
InputStream inputStream = Files.newInputStream(path.toFile().toPath());
110+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
111+
builder.setMode(HttpMultipartMode.STRICT);
112+
builder.addBinaryBody(RodaConstants.API_QUERY_KEY_FILE, inputStream, ContentType.create("application/zip"),
113+
path.getFileName().toString());
114+
httpPost.setEntity(builder.build());
152115

153-
try {
154-
httpPost.setEntity(entity);
155-
HttpResponse response = httpClient.execute(httpPost);
156-
157-
return response.getStatusLine().getStatusCode();
116+
Result result = httpclient.execute(httpPost, response -> new Result(response.getCode(), null));
158117

118+
return result.statusCode;
159119
} catch (IOException e) {
160120
throw new RODAException("Error sending POST request", e);
161121
}
162122
}
163123

164-
public static JsonNode sendPostRequest(String url, String resource, Object object) throws GenericException {
165-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
166-
HttpPost httpPost = new HttpPost(url + resource);
167-
httpPost.addHeader("content-type", "application/json");
168-
httpPost.addHeader("Accept", "application/json");
169-
170-
try {
171-
httpPost.setEntity(new StringEntity(JsonUtils.getJsonFromObject(object)));
172-
HttpResponse response;
173-
response = httpClient.execute(httpPost);
174-
HttpEntity responseEntity = response.getEntity();
175-
176-
int responseStatusCode = response.getStatusLine().getStatusCode();
177-
if (responseStatusCode == 200) {
178-
String json = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
179-
return JsonUtils.parseJson(json);
180-
} else {
181-
throw new GenericException("POST request response status code: " + responseStatusCode);
124+
public static int sendPostRequestWithFileHttp5(String url, String resource, String username, SecureString password,
125+
Path file) throws RODAException {
126+
try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
127+
HttpPost httpPost = new HttpPost(url + resource);
128+
try (
129+
SecureString basicAuth = new SecureString(
130+
ArrayUtils.addAll((username + ":").toCharArray(), password.getChars()));
131+
SecureString basicAuthToken = new SecureString(
132+
Base64.encode(StandardCharsets.UTF_8.encode(CharBuffer.wrap(basicAuth)).array()))) {
133+
134+
httpPost.setHeader("Authorization", "Basic " + basicAuthToken);
135+
File fileToUpload = new File(FilenameUtils.normalize(file.toString()));
136+
InputStream inputStream = new FileInputStream(fileToUpload);
137+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
138+
builder.addBinaryBody(RodaConstants.API_QUERY_KEY_FILE, inputStream, ContentType.DEFAULT_BINARY,
139+
fileToUpload.getName());
140+
httpPost.setEntity(builder.build());
141+
142+
Result result = httpclient.execute(httpPost, response -> new Result(response.getCode(), null));
143+
144+
return result.statusCode;
182145
}
183146
} catch (IOException e) {
184-
throw new GenericException("Error sending POST request", e);
147+
throw new RODAException("Error sending POST request", e);
185148
}
186149
}
187150

188-
public static int sendPostRequestWithFile(String url, String resource, String username, SecureString password,
189-
Path file) throws RODAException, FileNotFoundException {
190-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
191-
HttpPost httpPost = new HttpPost(url + resource);
192-
SecureString basicAuth = new SecureString(ArrayUtils.addAll((username + ":").toCharArray(), password.getChars()));
193-
SecureString basicAuthToken = new SecureString(
194-
Base64.encode(StandardCharsets.UTF_8.encode(CharBuffer.wrap(basicAuth)).array()));
195-
196-
httpPost.setHeader("Authorization", "Basic " + basicAuthToken.toString());
197-
198-
File fileToUpload = new File(FilenameUtils.normalize(file.toString()));
199-
InputStream inputStream = new FileInputStream(fileToUpload);
200-
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
201-
builder.addBinaryBody(RodaConstants.API_QUERY_KEY_FILE, inputStream, ContentType.DEFAULT_BINARY,
202-
fileToUpload.getName());
203-
204-
HttpEntity multipart = builder.build();
205-
206-
try {
207-
httpPost.setEntity(multipart);
208-
HttpResponse response = httpClient.execute(httpPost);
209-
210-
return response.getStatusLine().getStatusCode();
211-
212-
} catch (IOException e) {
213-
throw new RODAException("Error sending POST request", e);
214-
} finally {
215-
basicAuth.close();
216-
basicAuthToken.close();
217-
}
151+
private record Result(int statusCode, String content) {
218152
}
219153
}

roda-core/roda-core/src/main/java/org/roda/core/model/ModelService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,7 @@ public synchronized void findOldLogsAndSendThemToMaster(Path logDirectory, Path
22012201
RodaCoreFactory.getProperty(RodaConstants.CORE_ACTION_LOGS_PRIMARY_PASS, "").toCharArray())) {
22022202
for (Path path : directoryStream) {
22032203
if (!path.equals(currentLogFile)) {
2204-
int httpExitCode = RESTClientUtility.sendPostRequestWithFile(url, resource, username, password, path);
2204+
int httpExitCode = RESTClientUtility.sendPostRequestWithFileHttp5(url, resource, username, password, path);
22052205
if (httpExitCode == RodaConstants.HTTP_RESPONSE_CODE_CREATED) {
22062206
LOGGER.info("The action log file ({}) was moved to Master successfully!", path);
22072207
Files.delete(path);

roda-core/roda-core/src/main/java/org/roda/core/plugins/base/synchronization/instance/RegisterPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void subscribeLocalInstance(ModelService model, Job cachedJob, Report pl
149149
AccessToken accessToken = TokenManager.getInstance().getAccessToken(localInstance);
150150
String resource = RodaConstants.API_SEP + RodaConstants.API_REST_V2_DISTRIBUTED_INSTANCE + RodaConstants.API_SEP
151151
+ localInstance.getId() + RodaConstants.API_SEP + RodaConstants.API_PATH_PARAM_DISTRIBUTED_INSTANCE_REGISTER;
152-
RESTClientUtility.sendPostRequestWithoutBody(null, localInstance.getCentralInstanceURL(), resource,
152+
RESTClientUtility.sendPostRequestWithoutBodyHttp5(null, localInstance.getCentralInstanceURL(), resource,
153153
accessToken);
154154
localInstance.setIsSubscribed(true);
155155
localInstance.setStatus(SynchronizingStatus.ACTIVE);

roda-core/roda-core/src/main/java/org/roda/core/plugins/base/synchronization/proccess/SendSyncBundlePlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ private int send(Path zipPath) throws GenericException {
196196

197197
String resource = RodaConstants.API_SEP + RodaConstants.API_REST_V2_DISTRIBUTED_INSTANCE
198198
+ RodaConstants.API_PATH_PARAM_DISTRIBUTED_INSTANCE_SYNC + RodaConstants.API_SEP + localInstance.getId();
199-
return RESTClientUtility.sendPostRequestWithCompressedFile(localInstance.getCentralInstanceURL(), resource,
199+
return RESTClientUtility.sendPostRequestWithCompressedFileHttp5(localInstance.getCentralInstanceURL(), resource,
200200
zipPath, accessToken);
201-
} catch (RODAException | IOException e) {
201+
} catch (RODAException e) {
202202
LOGGER.error("Unable to send bundle to central instance", e);
203203
throw new GenericException("Unable to send bundle to central instance", e);
204204
}

roda-ui/roda-wui/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@
497497
<version>2.10.0</version>
498498
</dependency>
499499
<dependency>
500-
<groupId>org.apache.httpcomponents</groupId>
501-
<artifactId>httpclient</artifactId>
500+
<groupId>org.apache.httpcomponents.client5</groupId>
501+
<artifactId>httpclient5</artifactId>
502502
</dependency>
503503
<dependency>
504504
<groupId>com.github.nmorel.gwtjackson</groupId>

0 commit comments

Comments
 (0)