in clients/client-java/src/main/java/org/apache/gravitino/client/HTTPClient.java [321:402]
private <T> T execute(
Method method,
String path,
Map<String, String> queryParams,
Object requestBody,
Class<T> responseType,
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler,
Consumer<Map<String, String>> responseHeaders) {
if (handlerStatus != HandlerStatus.Finished) {
performPreConnectHandler();
}
if (path.startsWith("/")) {
throw new RESTException(
"Received a malformed path for a REST request: %s. Paths should not start with /", path);
}
HttpUriRequestBase request = new HttpUriRequestBase(method.name(), buildUri(path, queryParams));
if (requestBody instanceof Map) {
// encode maps as form data, application/x-www-form-urlencoded
addRequestHeaders(request, headers, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
request.setEntity(toFormEncoding((Map<?, ?>) requestBody));
} else if (requestBody != null) {
// other request bodies are serialized as JSON, application/json
addRequestHeaders(request, headers, ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(toJson(requestBody));
} else {
addRequestHeaders(request, headers, ContentType.APPLICATION_JSON.getMimeType());
}
if (authDataProvider != null) {
request.setHeader(
AuthConstants.HTTP_HEADER_AUTHORIZATION,
new String(authDataProvider.getTokenData(), StandardCharsets.UTF_8));
}
try (CloseableHttpResponse response = httpClient.execute(request)) {
Map<String, String> respHeaders = Maps.newHashMap();
for (Header header : response.getHeaders()) {
respHeaders.put(header.getName(), header.getValue());
}
responseHeaders.accept(respHeaders);
// Skip parsing the response stream for any successful request not expecting a response body
if (response.getCode() == HttpStatus.SC_NO_CONTENT
|| (responseType == null && isSuccessful(response))) {
return null;
}
String responseBody = extractResponseBodyAsString(response);
if (!isSuccessful(response)) {
// The provided error handler is expected to throw, but a RESTException.java is thrown if
// not.
throwFailure(response, responseBody, errorHandler);
}
if (responseBody == null) {
throw new RESTException(
"Invalid (null) response body for request (expected %s): method=%s, path=%s, status=%d",
responseType != null ? responseType.getSimpleName() : "unknown",
method.name(),
path,
response.getCode());
}
try {
return mapper.readValue(responseBody, responseType);
} catch (JsonProcessingException e) {
throw new RESTException(
e,
"Received a success response code of %d, but failed to parse response body into %s",
response.getCode(),
responseType != null ? responseType.getSimpleName() : "unknown");
}
} catch (IOException e) {
throw new RESTException(e, "Error occurred while processing %s request", method);
}
}