Class AbstractRequestExecutor<T extends AbstractRequestExecutor<T>>
- Type Parameters:
T- the concrete subclass type, used to support fluent builder-style APIs
- Direct Known Subclasses:
BraveRequestExecutor
HttpClient.
This executor is designed to be extended by concrete request builders or clients. It provides a consistent execution model that:
- Uses a single, reusable
HttpClientinstance - Fully consumes HTTP responses using a
byte[]body handler - Performs response decompression and decoding after transport completion
- Adapts the result into an
HttpResponsewith a transformed body
Lifecycle and resource management: This class intentionally reuses a single
HttpClient instance for its entire lifetime. The client is not closed per request.
Connection pooling, HTTP/2 multiplexing, and cleanup are managed internally by the JDK HTTP
client implementation.
If callers require explicit control over the HttpClient lifecycle or configuration,
they should manage their own client instance externally and use the request builders provided by
JBrave to construct HttpRequest and URI instances.
Builders in the api package are designed to be independent of any specific HTTP client
implementation.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <R,U> HttpResponse <U> adaptHttpResponse(HttpResponse<R> httpResponse, U newResponseBody) Adapts an existingHttpResponseto a new response body type.protected StringdecodeByteArray(byte[] bytes, Charset charset) Decodes a byte array into aStringusing the given character set.protected StringdecodeByteHttpResponse(HttpResponse<byte[]> byteResponse) protected byte[]decompressGunzip(byte[] compressedBytes) Decompresses a GZIP-compressed byte array.final @NotNull HttpResponse<String> execute(HttpRequest request) Executes the given HTTP request and returns a decoded response.protected Tself()Returns the current instance cast to the concrete subclass type.
-
Field Details
-
client
-
-
Constructor Details
-
AbstractRequestExecutor
public AbstractRequestExecutor()
-
-
Method Details
-
self
Returns the current instance cast to the concrete subclass type.This method supports fluent APIs in subclasses by avoiding repeated casts. The cast is safe by construction because the type parameter
Tis constrained to extendAbstractRequestExecutor<T>.- Returns:
- this instance, cast to
T
-
decompressGunzip
Decompresses a GZIP-compressed byte array.This method performs a bounded, in-memory decompression of the provided byte array. It should be used only after the full response body has been received.
- Parameters:
compressedBytes- the GZIP-compressed data- Returns:
- the decompressed bytes
- Throws:
IOException- if the compressed data is malformed or decompression fails
-
decodeByteArray
Decodes a byte array into aStringusing the given character set.The character set must be provided explicitly to avoid reliance on platform default encodings.
- Parameters:
bytes- the raw byte datacharset- the character set to use for decoding- Returns:
- the decoded string
-
decodeByteHttpResponse
Decodes anHttpResponsewith abyte[]body into aString.If the response indicates GZIP compression, the body is decompressed before decoding. Decoding assumes UTF-8 encoding, as response bodies are expected to represent textual content such as JSON or other UTF-8 encoded payloads intended for deserialization into domain objects.
This method performs all transformations eagerly and should be invoked only after the HTTP response has been fully received.
- Parameters:
byteResponse- the HTTP response containing a byte array body- Returns:
- the decoded response body as a string
- Throws:
ResponseDecompressionException- if decompression fails
-
adaptHttpResponse
Adapts an existingHttpResponseto a new response body type.The returned response delegates all metadata (status code, headers, request, URI, protocol version, etc.) to the original response while exposing the provided body value.
This adapter does not preserve redirect history;
HttpResponse.previousResponse()always returnsOptional.empty(), regardless of whether redirects occurred during request execution.This is a structural adaptation only; no additional HTTP processing occurs.
- Type Parameters:
R- the original response body typeU- the adapted response body type- Parameters:
httpResponse- the original HTTP responsenewResponseBody- the transformed response body- Returns:
- an
HttpResponseexposing the transformed body
-
execute
@NotNull public final @NotNull HttpResponse<String> execute(HttpRequest request) throws InterruptedException Executes the given HTTP request and returns a decoded response.The request is sent using the shared
HttpClient. The response body is fully materialized as abyte[]before any transformation occurs.If the response is GZIP-compressed, it is decompressed and then decoded into a
String. The resulting body is adapted into a newHttpResponseinstance.This method performs no streaming and does not expose partially received data.
- Parameters:
request- the HTTP request to execute- Returns:
- an
HttpResponsecontaining the decoded response body - Throws:
InterruptedException- if the executing thread is interruptedBraveClientException- if request execution or response processing fails
-