Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#156] docs: improve English and add JavaDocs for java client module (#157)

Justin Mclean5c0c4d94ccd8
diff --git a/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java b/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java
index d6f1f38068..d2a31f9e50 100644
--- a/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java
+++ b/client-java/src/main/java/com/datastrato/graviton/DTOConverters.java
@@ -10,9 +10,17 @@ import com.datastrato.graviton.dto.AuditDTO;
import com.datastrato.graviton.dto.MetalakeDTO;
import com.datastrato.graviton.dto.requests.MetalakeUpdateRequest;
+/** Utility class for converting between DTO objects and GravitonMetaLake objects. */
class DTOConverters {
private DTOConverters() {}
+ /**
+ * Converts a MetalakeDTO object to a GravitonMetaLake object.
+ *
+ * @param metalake The Metalake DTO to be converted.
+ * @param client The REST client associated with the GravitonMetaLake.
+ * @return A GravitonMetaLake object representing the converted MetalakeDTO.
+ */
static GravitonMetaLake toMetaLake(MetalakeDTO metalake, RESTClient client) {
return new GravitonMetaLake.Builder()
.withName(metalake.name())
@@ -23,6 +31,13 @@ class DTOConverters {
.build();
}
+ /**
+ * Converts a MetalakeChange object to a MetalakeUpdateRequest object.
+ *
+ * @param change The Metalake change to be converted.
+ * @return A MetalakeUpdateRequest object representing the converted Metalake change.
+ * @throws IllegalArgumentException If the provided Metalake change is of an unknown type.
+ */
static MetalakeUpdateRequest toMetalakeUpdateRequest(MetalakeChange change) {
if (change instanceof MetalakeChange.RenameMetalake) {
return new MetalakeUpdateRequest.RenameMetalakeRequest(
diff --git a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java b/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java
index 8eef64f211..71bc120833 100644
--- a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java
+++ b/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java
@@ -17,20 +17,41 @@ import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Utility class providing error handling for REST requests and specific to Metalake errors.
+ *
+ * <p>It also includes utility methods to format error messages and convert stack traces to strings.
+ */
public class ErrorHandlers {
private static final Logger LOG = LoggerFactory.getLogger(ErrorHandlers.class);
+ /**
+ * Creates an error handler specific to Metalake operations.
+ *
+ * @return A Consumer representing the Metalake error handler.
+ */
public static Consumer<ErrorResponse> metalakeErrorHandler() {
return MetalakeErrorHandler.INSTANCE;
}
+ /**
+ * Creates a generic error handler for REST requests.
+ *
+ * @return A Consumer representing the generic REST error handler.
+ */
public static Consumer<ErrorResponse> restErrorHandler() {
return RestErrorHandler.INSTANCE;
}
private ErrorHandlers() {}
+ /**
+ * Converts a list of stack trace elements to a formatted string with line breaks.
+ *
+ * @param stack The list of stack trace elements to be converted.
+ * @return A formatted string representing the stack trace.
+ */
private static String getStackString(List<String> stack) {
if (stack == null || stack.isEmpty()) {
return "";
@@ -40,6 +61,12 @@ public class ErrorHandlers {
}
}
+ /**
+ * Formats the error message along with the stack trace, if available.
+ *
+ * @param errorResponse The ErrorResponse object containing the error message and stack trace.
+ * @return A formatted error message string.
+ */
private static String formatErrorMessage(ErrorResponse errorResponse) {
String message = errorResponse.getMessage();
String stack = getStackString(errorResponse.getStack());
@@ -50,6 +77,7 @@ public class ErrorHandlers {
}
}
+ /** Error handler specific to Metalake operations. */
private static class MetalakeErrorHandler extends RestErrorHandler {
private static final ErrorHandler INSTANCE = new MetalakeErrorHandler();
@@ -68,6 +96,7 @@ public class ErrorHandlers {
}
}
+ /** Generic error handler for REST requests. */
private static class RestErrorHandler extends ErrorHandler {
private static final ErrorHandler INSTANCE = new RestErrorHandler();
diff --git a/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java b/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java
index a013a51408..c5aaf0bcea 100644
--- a/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java
+++ b/client-java/src/main/java/com/datastrato/graviton/GravitonClient.java
@@ -27,6 +27,13 @@ import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+/**
+ * Graviton Client for interacting with the Graviton API, allowing the client to list, load, create,
+ * and alter Metalakes.
+ *
+ * <p>It uses an underlying {@link RESTClient} to send HTTP requests and receive responses from the
+ * API.
+ */
public class GravitonClient implements SupportsMetalakes, Closeable {
private static final Logger LOG = LoggerFactory.getLogger(GravitonClient.class);
@@ -35,11 +42,21 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
private final RESTClient restClient;
+ /**
+ * Constructs a new GravitonClient with the given URI.
+ *
+ * @param uri The base URI for the Graviton API.
+ */
private GravitonClient(String uri) {
this.restClient =
HTTPClient.builder(Collections.emptyMap()).uri(uri).withObjectMapper(MAPPER).build();
}
+ /**
+ * Retrieves a list of Metalakes from the Graviton API.
+ *
+ * @return An array of GravitonMetaLake objects representing the Metalakes.
+ */
@Override
public GravitonMetaLake[] listMetalakes() {
MetalakeListResponse resp =
@@ -55,6 +72,13 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
.toArray(GravitonMetaLake[]::new);
}
+ /**
+ * Loads a specific Metalake from the Graviton API.
+ *
+ * @param ident The identifier of the Metalake to be loaded.
+ * @return A GravitonMetaLake instance representing the loaded Metalake.
+ * @throws NoSuchMetalakeException If the specified Metalake does not exist.
+ */
@Override
public GravitonMetaLake loadMetalake(NameIdentifier ident) throws NoSuchMetalakeException {
Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
@@ -73,6 +97,16 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
return DTOConverters.toMetaLake(resp.getMetalake(), restClient);
}
+ /**
+ * Creates a new Metalake using the Graviton API.
+ *
+ * @param ident The identifier of the new Metalake.
+ * @param comment The comment for the new Metalake.
+ * @param properties The properties of the new Metalake.
+ * @return A GravitonMetaLake instance representing the newly created Metalake.
+ * @throws MetalakeAlreadyExistsException If a Metalake with the specified identifier already
+ * exists.
+ */
@Override
public GravitonMetaLake createMetalake(
NameIdentifier ident, String comment, Map<String, String> properties)
@@ -97,6 +131,15 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
return DTOConverters.toMetaLake(resp.getMetalake(), restClient);
}
+ /**
+ * Alters a specific Metalake using the Graviton API.
+ *
+ * @param ident The identifier of the Metalake to be altered.
+ * @param changes The changes to be applied to the Metalake.
+ * @return A GravitonMetaLake instance representing the updated Metalake.
+ * @throws NoSuchMetalakeException If the specified Metalake does not exist.
+ * @throws IllegalArgumentException If the provided changes are invalid or not applicable.
+ */
@Override
public GravitonMetaLake alterMetalake(NameIdentifier ident, MetalakeChange... changes)
throws NoSuchMetalakeException, IllegalArgumentException {
@@ -124,6 +167,12 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
return DTOConverters.toMetaLake(resp.getMetalake(), restClient);
}
+ /**
+ * Drops a specific Metalake using the Graviton API.
+ *
+ * @param ident The identifier of the Metalake to be dropped.
+ * @return True if the Metalake was successfully dropped, false otherwise.
+ */
@Override
public boolean dropMetalake(NameIdentifier ident) {
Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
@@ -147,6 +196,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
}
}
+ /** Closes the GravitonClient and releases any underlying resources. */
@Override
public void close() {
if (restClient != null) {
@@ -159,18 +209,36 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
}
}
+ /**
+ * Creates a new builder for constructing a GravitonClient.
+ *
+ * @param uri The base URI for the Graviton API.
+ * @return A new instance of the Builder class for constructing a GravitonClient.
+ */
public static Builder builder(String uri) {
return new Builder(uri);
}
+ /** Builder class for constructing a GravitonClient. */
public static class Builder {
private String uri;
+ /**
+ * Private constructor for the Builder class.
+ *
+ * @param uri The base URI for the Graviton API.
+ */
private Builder(String uri) {
this.uri = uri;
}
+ /**
+ * Builds a new GravitonClient instance.
+ *
+ * @return A new instance of GravitonClient with the specified base URI.
+ * @throws IllegalArgumentException If the base URI is null or empty.
+ */
public GravitonClient build() {
Preconditions.checkArgument(
uri != null && !uri.isEmpty(), "The argument 'uri' must be a valid URI");
diff --git a/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java b/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java
index c99c177bf7..861e54948b 100644
--- a/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java
+++ b/client-java/src/main/java/com/datastrato/graviton/GravitonMetaLake.java
@@ -14,10 +14,23 @@ import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
import com.google.common.base.Preconditions;
import java.util.Map;
+/**
+ * GravitonMetaLake is a specific implementation of MetalakeDTO that also supports Catalog
+ * operations.
+ */
public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
private final RESTClient restClient;
+ /**
+ * Constructs a new Graviton MetaLake instance with the provided details.
+ *
+ * @param name The name of the Graviton MetaLake.
+ * @param comment The comment for the Graviton MetaLake.
+ * @param properties The properties of the Graviton MetaLake.
+ * @param auditDTO The audit information for the Graviton MetaLake.
+ * @param restClient The REST client used for API communication.
+ */
GravitonMetaLake(
String name,
String comment,
@@ -28,16 +41,42 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
this.restClient = restClient;
}
+ /**
+ * Retrieves an array of Catalog names associated with this Graviton MetaLake.
+ *
+ * @param namespace The namespace to filter Catalogs.
+ * @return An array of name identifier's representing the Catalogs.
+ * @throws NoSuchMetalakeException If the Graviton MetaLake does not exist.
+ */
@Override
public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {
throw new UnsupportedOperationException("Not implemented yet");
}
+ /**
+ * Loads a specific Catalog associated with this Graviton MetaLake.
+ *
+ * @param ident The identifier of the Catalog to be loaded.
+ * @return A Catalog instance representing the loaded Catalog.
+ * @throws NoSuchCatalogException If the specified Catalog does not exist.
+ */
@Override
public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {
throw new UnsupportedOperationException("Not implemented yet");
}
+ /**
+ * Creates a new Catalog associated with this Graviton MetaLake.
+ *
+ * @param ident The identifier of the new Catalog.
+ * @param type The type of the new Catalog.
+ * @param comment The comment for the new Catalog.
+ * @param properties The properties of the new Catalog.
+ * @return A Catalog instance representing the newly created Catalog.
+ * @throws NoSuchMetalakeException If the Graviton MetaLake does not exist.
+ * @throws CatalogAlreadyExistsException If a Catalog with the specified identifier already
+ * exists.
+ */
@Override
public Catalog createCatalog(
NameIdentifier ident, Catalog.Type type, String comment, Map<String, String> properties)
@@ -45,25 +84,54 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
throw new UnsupportedOperationException("Not implemented yet");
}
+ /**
+ * Alters a specific Catalog associated with this GravitonMetaLake.
+ *
+ * @param ident The identifier of the Catalog to be altered.
+ * @param changes The changes to be applied to the Catalog.
+ * @return A Catalog instance representing the updated Catalog.
+ * @throws NoSuchCatalogException If the specified Catalog does not exist.
+ * @throws IllegalArgumentException If the provided changes are invalid.
+ */
@Override
public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)
throws NoSuchCatalogException, IllegalArgumentException {
throw new UnsupportedOperationException("Not implemented yet");
}
+ /**
+ * Drops a specific Catalog associated with this Graviton MetaLake.
+ *
+ * @param ident The identifier of the Catalog to be dropped.
+ * @return True if the drop operation was successful, false otherwise.
+ */
@Override
public boolean dropCatalog(NameIdentifier ident) {
throw new UnsupportedOperationException("Not implemented yet");
}
+ /** The Builder class for GravitonMetaLake. */
static class Builder extends MetalakeDTO.Builder<Builder> {
private RESTClient restClient;
+ /**
+ * Sets the RESTClient for the GravitonMetaLake being built.
+ *
+ * @param restClient The REST client for API communication.
+ * @return The Builder instance with the updated REST client.
+ */
Builder withRestClient(RESTClient restClient) {
this.restClient = restClient;
return this;
}
+ /**
+ * Builds the GravitonMetaLake instance.
+ *
+ * @return A new Graviton MetaLake instance with the specified details.
+ * @throws NullPointerException If the REST client is not set.
+ * @throws IllegalArgumentException If the name is null or empty, or the audit is null.
+ */
@Override
public GravitonMetaLake build() {
Preconditions.checkNotNull(restClient, "restClient must be set");
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandler.java b/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandler.java
index 10939ee598..f1e29b5cae 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandler.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/ErrorHandler.java
@@ -9,7 +9,23 @@ import com.datastrato.graviton.dto.responses.ErrorResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.function.Consumer;
+/**
+ * The ErrorHandler class is an abstract class specialized for handling ErrorResponse objects.
+ * Subclasses of ErrorHandler must implement the parseResponse method to provide custom parsing
+ * logic for different types of errors.
+ *
+ * @param <ErrorResponse> The type of ErrorResponse that this ErrorHandler handles.
+ */
public abstract class ErrorHandler implements Consumer<ErrorResponse> {
+ /**
+ * Parses the response and creates an ErrorResponse object based on the response code and the JSON
+ * data using the provided ObjectMapper.
+ *
+ * @param code The response code indicating the error status.
+ * @param json The JSON data representing the error response.
+ * @param mapper The ObjectMapper used to deserialize the JSON data.
+ * @return The ErrorResponse object representing the parsed error response.
+ */
public abstract ErrorResponse parseResponse(int code, String json, ObjectMapper mapper);
}
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/HTTPClient.java b/client-java/src/main/java/com/datastrato/graviton/client/HTTPClient.java
index 8f4359b686..02add7e005 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/HTTPClient.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/HTTPClient.java
@@ -60,6 +60,10 @@ import org.slf4j.LoggerFactory;
/**
* An HttpClient for usage with the REST catalog.
*
+ * <p>This class provides functionality for making HTTP requests to a REST API and processing the
+ * corresponding responses. It supports common HTTP methods like GET, POST, PUT, DELETE, and HEAD.
+ * Additionally, it allows handling server error responses using a custom error handler.
+ *
* <p>Referred from core/src/main/java/org/apache/iceberg/rest/HTTPClient.java
*/
public class HTTPClient implements RESTClient {
@@ -72,6 +76,13 @@ public class HTTPClient implements RESTClient {
private final CloseableHttpClient httpClient;
private final ObjectMapper mapper;
+ /**
+ * Constructs an instance of HTTPClient with the provided information.
+ *
+ * @param uri The base URI of the REST API.
+ * @param baseHeaders A map of base headers to be included in all HTTP requests.
+ * @param objectMapper The ObjectMapper used for JSON serialization and deserialization.
+ */
private HTTPClient(String uri, Map<String, String> baseHeaders, ObjectMapper objectMapper) {
this.uri = uri;
this.mapper = objectMapper;
@@ -88,6 +99,13 @@ public class HTTPClient implements RESTClient {
this.httpClient = clientBuilder.build();
}
+ /**
+ * Extracts the response body as a string from the provided HTTP response.
+ *
+ * @param response The HTTP response from which the response body will be extracted.
+ * @return The response body as a string.
+ * @throws RESTException If an error occurs during conversion of the response body to a string.
+ */
private String extractResponseBodyAsString(CloseableHttpResponse response) {
try {
if (response.getEntity() == null) {
@@ -101,7 +119,14 @@ public class HTTPClient implements RESTClient {
}
}
- // Per the spec, the only currently defined / used "success" responses are 200 and 202.
+ /**
+ * Checks if the response indicates a successful response.
+ *
+ * <p>According to the spec, the only currently defined/used "success" responses are 200 and 202.
+ *
+ * @param response The reponse to check for success.
+ * @return True if the response is successful, false otherwise.
+ */
private boolean isSuccessful(CloseableHttpResponse response) {
int code = response.getCode();
return code == HttpStatus.SC_OK
@@ -109,6 +134,16 @@ public class HTTPClient implements RESTClient {
|| code == HttpStatus.SC_NO_CONTENT;
}
+ /**
+ * Builds an error reponse based on the provided HTTP response.
+ *
+ * <p>This method extracts the reason phrase from the response and uses it as the message for the
+ * ErrorResponse. If the reason phrase doesn't exist, it retrieves the standard reason phrase from
+ * teh English phrase catalog.
+ *
+ * @param response The response from which the ErrorResponse is built.
+ * @return An ErrorResponse object representing the REST error response.
+ */
private ErrorResponse buildRestErrorResponse(CloseableHttpResponse response) {
String responseReason = response.getReasonPhrase();
String message =
@@ -118,9 +153,18 @@ public class HTTPClient implements RESTClient {
return ErrorResponse.restError(message);
}
- // Process a failed response through the provided errorHandler, and throw a RESTException.java if
- // the
- // provided error handler doesn't already throw.
+ /**
+ * Processes a failed response through the provided error.
+ *
+ * <p>This method takes a response representing a failed response from an HTTP request. It tries
+ * to parse the response body using the provided parseResponse method.
+ *
+ * @param response The failed response from the HTTP request.
+ * @param responseBody The response body as a string (can be null).
+ * @param errorHandler The error handler (as a Consumer) used to handle the error response.
+ * @throws RESTException If the error handler does not throw an exception or an error occurs
+ * during parsing.
+ */
private void throwFailure(
CloseableHttpResponse response, String responseBody, Consumer<ErrorResponse> errorHandler) {
ErrorResponse errorResponse = null;
@@ -143,13 +187,11 @@ public class HTTPClient implements RESTClient {
} catch (UncheckedIOException | IllegalArgumentException e) {
// It's possible to receive a non-successful response that isn't a properly defined
- // BaseResponse
- // without any bugs in the server implementation. So we ignore this exception and build an
- // error response for the user.
- //
- // For example, the connection could time out before every reaching the server, in which
- // case we'll
- // likely get a 5xx with the load balancers default 5xx response.
+ // BaseResponse due to various reasons, such as server misconfiguration or
+ // unanticipated external factors.
+ // In such cases, we handle the situation by building an error response for the user.
+ // Examples of such scenarios include network timeouts or load balancers returning default
+ // 5xx responses.
LOG.error("Failed to parse an error response. Will create one instead.", e);
}
}
@@ -164,6 +206,19 @@ public class HTTPClient implements RESTClient {
throw new RESTException("Unhandled error: %s", errorResponse);
}
+ /**
+ * Builds a URI for the HTTP request using the given path and query parameters.
+ *
+ * <p>This method constructs a URI by combining the base URI (stored in the "uri" field) with the
+ * provided path. If query parameters are provided in the "params" map, they are added to the URI,
+ * ensuring proper encoding of query parameters and that the URI is well-formed.
+ *
+ * @param path The URL path to append to the base URI.
+ * @param params A map of query parameters (key-value pairs) to include in the URI (can be null).
+ * @return The constructed URI for the HTTP request.
+ * @throws RESTException If there is an issue building the URI from the base URI and query
+ * parameters.
+ */
private URI buildUri(String path, Map<String, String> params) {
String baseUri = String.format("%s/%s", uri, path);
try {
@@ -179,19 +234,24 @@ public class HTTPClient implements RESTClient {
}
/**
- * Method to execute an HTTP request and process the corresponding response.
+ * Executes an HTTP request and processes the corresponding response.
*
- * @param method - HTTP method, such as GET, POST, HEAD, etc.
- * @param queryParams - A map of query parameters
- * @param path - URL path to send the request to
- * @param requestBody - Content to place in the request body
- * @param responseType - Class of the Response type. Needs to have serializer registered with
- * ObjectMapper
- * @param errorHandler - Error handler delegated for HTTP responses which handles server error
- * responses
- * @param <T> - Class type of the response for deserialization. Must be registered with the
- * ObjectMapper.
- * @return The response entity, parsed and converted to its type T
+ * <p>This method is a helper function to execute HTTP requests.
+ *
+ * @param method The HTTP method to use (e.g., GET, POST, PUT, DELETE).
+ * @param path The URL path to send the request to.
+ * @param queryParams A map of query parameters (key-value pairs) to include in the request URL
+ * (can be null).
+ * @param requestBody The content to place in the request body (can be null).
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization. (Must be registered with the
+ * ObjectMapper).
+ * @return The response entity parsed and converted to its type T.
*/
private <T> T execute(
Method method,
@@ -221,6 +281,37 @@ public class HTTPClient implements RESTClient {
* ObjectMapper.
* @return The response entity, parsed and converted to its type T
*/
+
+ /**
+ * Executes an HTTP request and processes the corresponding response with support for response
+ * headers.
+ *
+ * <p>The method constructs the HTTP request using the provided parameters and sends it to the
+ * server. It then processes the server's response, handling successful responses and server error
+ * responses accordingly.
+ *
+ * <p>Response headers from the server are extracted and passed to the responseHeaders Consumer
+ * for further processing by the caller.
+ *
+ * @param method The HTTP method to use (e.g., GET, POST, PUT, DELETE).
+ * @param path The URL path to send the request to.
+ * @param queryParams A map of query parameters (key-value pairs) to include in the request URL
+ * (can be null).
+ * @param requestBody The content to place in the request body (can be null).
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param responseHeaders The consumer of the response headers for further processing.
+ * @param <T> The class type of the response for deserialization. (Must be registered with the
+ * ObjectMapper).
+ * @return The response entity parsed and converted to its type T.
+ * @throws RESTException If the provided path is malformed, if there is an issue with the HTTP
+ * request or response processing, or if the errorHandler does not throw an exception for
+ * server error responses.
+ */
private <T> T execute(
Method method,
String path,
@@ -291,11 +382,35 @@ public class HTTPClient implements RESTClient {
}
}
+ /**
+ * Sends an HTTP HEAD request to the specified path and processes the response.
+ *
+ * @param path The URL path to send the HEAD request to.
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ */
@Override
public void head(String path, Map<String, String> headers, Consumer<ErrorResponse> errorHandler) {
execute(Method.HEAD, path, null, null, null, headers, errorHandler);
}
+ /**
+ * Sends an HTTP GET request to the specified path and processes the response.
+ *
+ * @param path The URL path to send the GET request to.
+ * @param queryParams A map of query parameters (key-value pairs) to include in the request URL
+ * (can be null).
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T get(
String path,
@@ -306,6 +421,21 @@ public class HTTPClient implements RESTClient {
return execute(Method.GET, path, queryParams, null, responseType, headers, errorHandler);
}
+ /**
+ * Sends an HTTP POST request to the specified path with the provided request body and processes
+ * the response.
+ *
+ * @param path The URL path to send the POST request to.
+ * @param body The REST body to place in the request body.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T post(
String path,
@@ -316,6 +446,22 @@ public class HTTPClient implements RESTClient {
return execute(Method.POST, path, null, body, responseType, headers, errorHandler);
}
+ /**
+ * Sends an HTTP POST request to the specified path with the provided request body and processes
+ * the response with support for response headers.
+ *
+ * @param path The URL path to send the POST request to.
+ * @param body The REST request to place in the request body.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param responseHeaders The consumer of the response headers for further processing.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T post(
String path,
@@ -328,6 +474,21 @@ public class HTTPClient implements RESTClient {
Method.POST, path, null, body, responseType, headers, errorHandler, responseHeaders);
}
+ /**
+ * Sends an HTTP PUT request to the specified path with the provided request body and processes
+ * the response.
+ *
+ * @param path The URL path to send the PUT request to.
+ * @param body The REST request to place in the request body.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T put(
String path,
@@ -338,6 +499,22 @@ public class HTTPClient implements RESTClient {
return execute(Method.PUT, path, null, body, responseType, headers, errorHandler);
}
+ /**
+ * Sends an HTTP PUT request to the specified path with the provided request body and processes
+ * the response with support for response headers.
+ *
+ * @param path The URL path to send the PUT request to.
+ * @param body The REST request to place in the request body.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param responseHeaders The consumer of the response headers for further processing.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T put(
String path,
@@ -350,6 +527,20 @@ public class HTTPClient implements RESTClient {
Method.PUT, path, null, body, responseType, headers, errorHandler, responseHeaders);
}
+ /**
+ * Sends an HTTP DELETE request to the specified path without query parameters and processes the
+ * response.
+ *
+ * @param path The URL path to send the DELETE request to.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T delete(
String path,
@@ -359,6 +550,22 @@ public class HTTPClient implements RESTClient {
return execute(Method.DELETE, path, null, null, responseType, headers, errorHandler);
}
+ /**
+ * Sends an HTTP DELETE request to the specified path with the provided query parameters and
+ * processes the response.
+ *
+ * @param path The URL path to send the DELETE request to.
+ * @param queryParams A map of query parameters (key-value pairs) to include in the request (can
+ * be null).
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
@Override
public <T extends RESTResponse> T delete(
String path,
@@ -379,24 +586,55 @@ public class HTTPClient implements RESTClient {
return execute(Method.POST, path, null, formData, responseType, headers, errorHandler);
}
+ /**
+ * Sends an HTTP POST request with form data to the specified path and processes the response.
+ *
+ * @param path The URL path to send the POST request to.
+ * @param formData A map of form data (key-value pairs) to include in the request body.
+ * @param responseType The class type of the response for deserialization (Must be registered with
+ * the ObjectMapper).
+ * @param headers A map of request headers (key-value pairs) to include in the request (can be
+ * null).
+ * @param errorHandler The error handler delegated for HTTP responses, which handles server error
+ * responses.
+ * @param <T> The class type of the response for deserialization.
+ * @return The response entity parsed and converted to its type T.
+ */
private void addRequestHeaders(
HttpUriRequest request, Map<String, String> requestHeaders, String bodyMimeType) {
- // Many systems require that content type is set regardless and will fail, even on an empty
- // bodied request.
+ // Some systems require the Content-Type header to be set even for empty-bodied requests to
+ // avoid failures.
request.setHeader(HttpHeaders.CONTENT_TYPE, bodyMimeType);
request.setHeader(HttpHeaders.ACCEPT, VERSION_HEADER);
requestHeaders.forEach(request::setHeader);
}
+ /**
+ * Closes the underlying HTTP client gracefully.
+ *
+ * @throws IOException If an I/O error occurs while closing the HTTP client.
+ */
@Override
public void close() throws IOException {
httpClient.close(CloseMode.GRACEFUL);
}
+ /**
+ * Creates a new instance of the HTTPClient.Builder with the specified properties.
+ *
+ * @param properties A map of properties (key-value pairs) used to configure the HTTP client.
+ * @return A new instance of HTTPClient.Builder with the provided properties.
+ */
public static Builder builder(Map<String, String> properties) {
return new Builder(properties);
}
+ /**
+ * Builder class for configuring and creating instances of HTTPClient.
+ *
+ * <p>This class allows for setting various configuration options for the HTTP client such as base
+ * URI, request headers, and ObjectMapper.
+ */
public static class Builder {
private final Map<String, String> properties;
private final Map<String, String> baseHeaders = Maps.newHashMap();
@@ -407,27 +645,58 @@ public class HTTPClient implements RESTClient {
this.properties = properties;
}
+ /**
+ * Sets the base URI for the HTTP client.
+ *
+ * @param baseUri The base URI to be used for all HTTP requests.
+ * @return This Builder instance for method chaining.
+ */
public Builder uri(String baseUri) {
Preconditions.checkNotNull(baseUri, "Invalid uri for http client: null");
this.uri = RESTUtils.stripTrailingSlash(baseUri);
return this;
}
+ /**
+ * Adds a single request header to the HTTP client.
+ *
+ * @param key The header name.
+ * @param value The header value.
+ * @return This Builder instance for method chaining.
+ */
public Builder withHeader(String key, String value) {
baseHeaders.put(key, value);
return this;
}
+ /**
+ * Adds multiple request headers to the HTTP client.
+ *
+ * @param headers A map of request headers (key-value pairs) to be included in all HTTP
+ * requests.
+ * @return This Builder instance for method chaining.
+ */
public Builder withHeaders(Map<String, String> headers) {
baseHeaders.putAll(headers);
return this;
}
+ /**
+ * Sets the custom ObjectMapper for the HTTP client.
+ *
+ * @param objectMapper The custom ObjectMapper to be used for request/response serialization.
+ * @return This Builder instance for method chaining.
+ */
public Builder withObjectMapper(ObjectMapper objectMapper) {
this.mapper = objectMapper;
return this;
}
+ /**
+ * Builds and returns an instance of the HTTPClient with the configured options.
+ *
+ * @return An instance of HTTPClient with the configured options.
+ */
public HTTPClient build() {
return new HTTPClient(uri, baseHeaders, mapper);
}
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/RESTClient.java b/client-java/src/main/java/com/datastrato/graviton/client/RESTClient.java
index f9d6f93de3..745b4ec51e 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/RESTClient.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/RESTClient.java
@@ -34,6 +34,13 @@ import java.util.function.Supplier;
*/
public interface RESTClient extends Closeable {
+ /**
+ * Perform a HEAD request on the specified path with the given headers and error handling.
+ *
+ * @param path The path to be requested.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ */
default void head(
String path, Supplier<Map<String, String>> headers, Consumer<ErrorResponse> errorHandler) {
head(path, headers.get(), errorHandler);
@@ -41,6 +48,17 @@ public interface RESTClient extends Closeable {
void head(String path, Map<String, String> headers, Consumer<ErrorResponse> errorHandler);
+ /**
+ * Perform a DELETE request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param queryParams The query parameters to be included in the request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the DELETE request.
+ */
default <T extends RESTResponse> T delete(
String path,
Map<String, String> queryParams,
@@ -50,6 +68,17 @@ public interface RESTClient extends Closeable {
return delete(path, queryParams, responseType, headers.get(), errorHandler);
}
+ /**
+ * Perform a DELETE request on the specified path with with given information and no query
+ * parameters.
+ *
+ * @param path The path to be requested.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the DELETE request.
+ */
default <T extends RESTResponse> T delete(
String path,
Class<T> responseType,
@@ -64,6 +93,19 @@ public interface RESTClient extends Closeable {
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler);
+ /**
+ * Perform a DELETE request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param queryParams The query parameters to be included in the request (ignored).
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the DELETE request.
+ * @throws UnsupportedOperationException If query parameters are provided, as they are not
+ * supported for DELETE requests.
+ */
default <T extends RESTResponse> T delete(
String path,
Map<String, String> queryParams,
@@ -77,6 +119,16 @@ public interface RESTClient extends Closeable {
return delete(path, responseType, headers, errorHandler);
}
+ /**
+ * Perform a GET request on the specified path with given information and no query parameters.
+ *
+ * @param path The path to be requested.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the GET request.
+ */
default <T extends RESTResponse> T get(
String path,
Class<T> responseType,
@@ -85,6 +137,16 @@ public interface RESTClient extends Closeable {
return get(path, ImmutableMap.of(), responseType, headers, errorHandler);
}
+ /**
+ * Perform a GET request on the specified path with given information and no query parameters.
+ *
+ * @param path The path to be requested.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the GET request.
+ */
default <T extends RESTResponse> T get(
String path,
Class<T> responseType,
@@ -93,6 +155,17 @@ public interface RESTClient extends Closeable {
return get(path, ImmutableMap.of(), responseType, headers, errorHandler);
}
+ /**
+ * Perform a GET request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param queryParams The query parameters to be included in the request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the GET request.
+ */
default <T extends RESTResponse> T get(
String path,
Map<String, String> queryParams,
@@ -102,6 +175,17 @@ public interface RESTClient extends Closeable {
return get(path, queryParams, responseType, headers.get(), errorHandler);
}
+ /**
+ * Perform a GET request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param queryParams The query parameters to be included in the request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the GET request.
+ */
<T extends RESTResponse> T get(
String path,
Map<String, String> queryParams,
@@ -109,6 +193,17 @@ public interface RESTClient extends Closeable {
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler);
+ /**
+ * Perform a POST request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the POST request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the POST request.
+ */
default <T extends RESTResponse> T post(
String path,
RESTRequest body,
@@ -118,6 +213,18 @@ public interface RESTClient extends Closeable {
return post(path, body, responseType, headers.get(), errorHandler);
}
+ /**
+ * Perform a POST request on the specified path with a given information.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the POST request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param responseHeaders The consumer for handling response headers (unsupported in this method).
+ * @param <T> The type of the response.
+ * @return The response of the POST request.
+ */
default <T extends RESTResponse> T post(
String path,
RESTRequest body,
@@ -128,6 +235,19 @@ public interface RESTClient extends Closeable {
return post(path, body, responseType, headers.get(), errorHandler, responseHeaders);
}
+ /**
+ * Perform a POST request on the specified path with a given information.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the POST request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param responseHeaders The consumer for handling response headers (unsupported in this method).
+ * @param <T> The type of the response.
+ * @return The response of the POST request.
+ * @throws UnsupportedOperationException If trying to handle response headers (unsupported).
+ */
default <T extends RESTResponse> T post(
String path,
RESTRequest body,
@@ -149,6 +269,17 @@ public interface RESTClient extends Closeable {
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler);
+ /**
+ * Perform a PUT request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the PUT request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the PUT request.
+ */
default <T extends RESTResponse> T put(
String path,
RESTRequest body,
@@ -158,6 +289,18 @@ public interface RESTClient extends Closeable {
return put(path, body, responseType, headers.get(), errorHandler);
}
+ /**
+ * Perform a PUT request on the specified path with given information.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the PUT request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The supplier for providing headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param responseHeaders The consumer for handling response headers.
+ * @param <T> The type of the response.
+ * @return The response of the PUT request.
+ */
default <T extends RESTResponse> T put(
String path,
RESTRequest body,
@@ -168,6 +311,21 @@ public interface RESTClient extends Closeable {
return put(path, body, responseType, headers.get(), errorHandler, responseHeaders);
}
+ /**
+ * Perform a PUT request on the specified path with a request body, response type, headers, and
+ * error handling.
+ *
+ * @param path The path to be requested.
+ * @param body The request body to be included in the PUT request.
+ * @param responseType The class representing the type of the response.
+ * @param headers The headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param responseHeaders This parameter is not supported and should be set to null.
+ * @param <T> The type of the response.
+ * @return The response of the PUT request.
+ * @throws UnsupportedOperationException if {@code responseHeaders} is not null, as returning
+ * response headers is not supported.
+ */
default <T extends RESTResponse> T put(
String path,
RESTRequest body,
@@ -189,6 +347,17 @@ public interface RESTClient extends Closeable {
Map<String, String> headers,
Consumer<ErrorResponse> errorHandler);
+ /**
+ * Perform a POST request with form data on the specified path with teh given information.
+ *
+ * @param path The path to be requested.
+ * @param formData The form data to be included in the POST request body.
+ * @param responseType The class representing the type of the response.
+ * @param headers The supplier for providing headers to be included in the request.
+ * @param errorHandler The consumer for handling error responses.
+ * @param <T> The type of the response.
+ * @return The response of the POST request.
+ */
default <T extends RESTResponse> T postForm(
String path,
Map<String, String> formData,