This is an automated email from the ASF dual-hosted git repository.
biyan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 1747e1a7bd [core] Update deprecated HttpClient 5 API calls (#6311)
1747e1a7bd is described below
commit 1747e1a7bd0f242b465d3e5c55f9b1a6a0b1bef3
Author: Kerwin Zhang <[email protected]>
AuthorDate: Tue Sep 23 22:20:44 2025 +0800
[core] Update deprecated HttpClient 5 API calls (#6311)
---
.../java/org/apache/paimon/rest/HttpClient.java | 61 ++++++++++++----------
.../main/java/org/apache/paimon/rest/RESTUtil.java | 6 +--
.../org/apache/paimon/rest/SimpleHttpClient.java | 33 ++++++------
.../rest/interceptor/LoggingInterceptor.java | 2 +-
.../paimon/rest/interceptor/TimingInterceptor.java | 2 +-
5 files changed, 55 insertions(+), 49 deletions(-)
diff --git a/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java
b/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java
index a8a18a4155..e682a2aee5 100644
--- a/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java
+++ b/paimon-api/src/main/java/org/apache/paimon/rest/HttpClient.java
@@ -33,9 +33,8 @@ 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.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
@@ -137,32 +136,36 @@ public class HttpClient implements RESTClient {
}
private <T extends RESTResponse> T exec(HttpUriRequestBase request,
Class<T> responseType) {
- try (CloseableHttpResponse response = HTTP_CLIENT.execute(request)) {
- String responseBodyStr =
RESTUtil.extractResponseBodyAsString(response);
- if (!RESTUtil.isSuccessful(response)) {
- ErrorResponse error;
- try {
- error = RESTApi.fromJson(responseBodyStr,
ErrorResponse.class);
- } catch (JsonProcessingException e) {
- error =
- new ErrorResponse(
- null,
- null,
- responseBodyStr != null
- ? responseBodyStr
- : "response body is null",
- response.getCode());
- }
- errorHandler.accept(error, getRequestId(response));
- }
- if (responseType != null && responseBodyStr != null) {
- return RESTApi.fromJson(responseBodyStr, responseType);
- } else if (responseType == null) {
- return null;
- } else {
- throw new RESTException("response body is null.");
- }
- } catch (IOException | ParseException e) {
+ try {
+ return HTTP_CLIENT.execute(
+ request,
+ response -> {
+ String responseBodyStr =
RESTUtil.extractResponseBodyAsString(response);
+ if (!RESTUtil.isSuccessful(response)) {
+ ErrorResponse error;
+ try {
+ error = RESTApi.fromJson(responseBodyStr,
ErrorResponse.class);
+ } catch (JsonProcessingException e) {
+ error =
+ new ErrorResponse(
+ null,
+ null,
+ responseBodyStr != null
+ ? responseBodyStr
+ : "response body is
null",
+ response.getCode());
+ }
+ errorHandler.accept(error, getRequestId(response));
+ }
+ if (responseType != null && responseBodyStr != null) {
+ return RESTApi.fromJson(responseBodyStr,
responseType);
+ } else if (responseType == null) {
+ return null;
+ } else {
+ throw new RESTException("response body is null.");
+ }
+ });
+ } catch (IOException e) {
throw new RESTException(
e, "Error occurred while processing %s request",
request.getMethod());
}
@@ -194,7 +197,7 @@ public class HttpClient implements RESTClient {
return uri;
}
- private static String getRequestId(CloseableHttpResponse response) {
+ private static String getRequestId(ClassicHttpResponse response) {
Header header =
response.getFirstHeader(LoggingInterceptor.REQUEST_ID_KEY);
return header != null ? header.getValue() :
LoggingInterceptor.DEFAULT_REQUEST_ID;
}
diff --git a/paimon-api/src/main/java/org/apache/paimon/rest/RESTUtil.java
b/paimon-api/src/main/java/org/apache/paimon/rest/RESTUtil.java
index 5872af2091..8a5e760793 100644
--- a/paimon-api/src/main/java/org/apache/paimon/rest/RESTUtil.java
+++ b/paimon-api/src/main/java/org/apache/paimon/rest/RESTUtil.java
@@ -27,7 +27,7 @@ import
org.apache.paimon.shade.guava30.com.google.common.collect.ImmutableMap;
import org.apache.paimon.shade.guava30.com.google.common.collect.Maps;
import
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
+import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -173,7 +173,7 @@ public class RESTUtil {
return null;
}
- public static String extractResponseBodyAsString(CloseableHttpResponse
response)
+ public static String extractResponseBodyAsString(ClassicHttpResponse
response)
throws IOException, ParseException {
if (response.getEntity() == null) {
return null;
@@ -182,7 +182,7 @@ public class RESTUtil {
return EntityUtils.toString(response.getEntity(),
StandardCharsets.UTF_8);
}
- public static boolean isSuccessful(CloseableHttpResponse response) {
+ public static boolean isSuccessful(ClassicHttpResponse response) {
int code = response.getCode();
return code == HttpStatus.SC_OK
|| code == HttpStatus.SC_ACCEPTED
diff --git
a/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java
b/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java
index e0e043fc1e..c2e044d2cc 100644
--- a/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java
+++ b/paimon-api/src/main/java/org/apache/paimon/rest/SimpleHttpClient.java
@@ -24,9 +24,7 @@ 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.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
-import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
@@ -80,19 +78,24 @@ public class SimpleHttpClient implements Closeable {
}
private String exec(HttpUriRequestBase request) {
- try (CloseableHttpResponse response = client.execute(request)) {
- String responseBodyStr =
RESTUtil.extractResponseBodyAsString(response);
-
- if (StringUtils.isNullOrWhitespaceOnly(responseBodyStr)
- || !RESTUtil.isSuccessful(response)) {
- throw new RuntimeException(
- RESTUtil.isSuccessful(response)
- ? "ResponseBody is null or empty."
- : String.format(
- "Response is not successful, response
is %s", response));
- }
- return responseBodyStr;
- } catch (IOException | ParseException e) {
+ try {
+ return client.execute(
+ request,
+ response -> {
+ String responseBodyStr =
RESTUtil.extractResponseBodyAsString(response);
+
+ if (StringUtils.isNullOrWhitespaceOnly(responseBodyStr)
+ || !RESTUtil.isSuccessful(response)) {
+ throw new RuntimeException(
+ RESTUtil.isSuccessful(response)
+ ? "ResponseBody is null or empty."
+ : String.format(
+ "Response is not
successful, response is %s",
+ response));
+ }
+ return responseBodyStr;
+ });
+ } catch (IOException e) {
throw new RuntimeException(
"Failed to convert HTTP response body to string, error : "
+ e.getMessage());
}
diff --git
a/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/LoggingInterceptor.java
b/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/LoggingInterceptor.java
index 02add8a45a..8422944dca 100644
---
a/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/LoggingInterceptor.java
+++
b/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/LoggingInterceptor.java
@@ -40,7 +40,7 @@ public class LoggingInterceptor implements
HttpResponseInterceptor {
@Override
public void process(
HttpResponse httpResponse, EntityDetails entityDetails,
HttpContext httpContext) {
- HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext);
+ HttpCoreContext coreContext = HttpCoreContext.cast(httpContext);
HttpRequest request = coreContext.getRequest();
Long startTime = (Long)
coreContext.getAttribute(REQUEST_START_TIME_KEY);
long durationMs = System.currentTimeMillis() - startTime;
diff --git
a/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/TimingInterceptor.java
b/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/TimingInterceptor.java
index 75d2187b58..a6915a1621 100644
---
a/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/TimingInterceptor.java
+++
b/paimon-api/src/main/java/org/apache/paimon/rest/interceptor/TimingInterceptor.java
@@ -35,7 +35,7 @@ public class TimingInterceptor implements
HttpRequestInterceptor {
public void process(
HttpRequest httpRequest, EntityDetails entityDetails, HttpContext
httpContext)
throws HttpException, IOException {
- HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext);
+ HttpCoreContext coreContext = HttpCoreContext.cast(httpContext);
coreContext.setAttribute(REQUEST_START_TIME_KEY,
System.currentTimeMillis());
}
}