java.lang.Object
de.cuioss.http.client.handler.HttpHandler
Secure HTTP client wrapper providing simplified HTTP request execution with robust SSL handling.
This class provides a builder-based wrapper around Java's HttpClient that simplifies
HTTP request configuration and execution while ensuring secure defaults for SSL/TLS connections.
It handles common HTTP client setup patterns and provides consistent timeout and SSL management.
Design Principles
- Security First - Automatic secure SSL context creation for HTTPS
- Builder Pattern - Fluent API for easy configuration
- Immutable - Thread-safe after construction
- Fail Fast - Validates configuration at build time
Security Features
- Automatic SSL Context - Creates secure SSL contexts when not provided
- TLS Version Control - Uses
SecureSSLContextProviderfor modern TLS versions - URL Validation - Validates URI format and convertibility at build time
- Timeout Protection - Configurable timeouts prevent resource exhaustion
Usage Examples
// Basic HTTPS request
HttpHandler handler = HttpHandler.builder()
.uri("https://api.example.com/users")
.connectionTimeoutSeconds(5)
.readTimeoutSeconds(10)
.build();
// Execute GET request
HttpResponse<String> response = handler.executeGetRequest();
if (response.statusCode() == 200) {
String body = response.body();
// Process response
}
// Custom SSL context
SSLContext customSSL = mySecureSSLProvider.getSSLContext();
HttpHandler secureHandler = HttpHandler.builder()
.uri("https://secure.example.com/api")
.sslContext(customSSL)
.build();
// URI object
URI apiEndpoint = URI.create("https://example.com/api/v1/data");
HttpHandler uriHandler = HttpHandler.builder()
.uri(apiEndpoint)
.build();
Configuration Contract
- URI Validation - URI must be valid and convertible to URL (checked at build time)
- HTTPS SSL Context - Automatically created if not provided for HTTPS URIs
- Timeout Defaults - Uses 10 seconds for both connection and read timeouts if not specified
- URL Scheme Detection - Automatically handles URLs with or without explicit schemes
Thread Safety
HttpHandler instances are immutable and thread-safe after construction. The underlying
HttpClient is also thread-safe and can be used concurrently from multiple threads.
Error Handling
- Build-time Validation -
IllegalStateExceptionfor invalid URIs or configuration - Runtime Exceptions -
IOExceptionfor network errors,InterruptedExceptionfor thread interruption
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int -
Method Summary
Modifier and TypeMethodDescriptionCreates a pre-configuredHttpHandler.HttpHandlerBuilderwith the same configuration as this handler.builder()Gets the configuredHttpClientfor making HTTP requests.pingGet()Pings the URI using the GET method and returns the HTTP status code.pingHead()Pings the URI using the HEAD method and returns the HTTP status code.Creates a pre-configuredHttpRequest.Builderfor the URI contained in this handler.
-
Field Details
-
DEFAULT_CONNECTION_TIMEOUT_SECONDS
- See Also:
-
DEFAULT_READ_TIMEOUT_SECONDS
- See Also:
-
-
Method Details
-
builder
-
requestBuilder
Creates a pre-configuredHttpRequest.Builderfor the URI contained in this handler. The builder is configured with the read timeout from this handler.- Returns:
- A pre-configured
HttpRequest.Builder
-
asBuilder
Creates a pre-configuredHttpHandler.HttpHandlerBuilderwith the same configuration as this handler. The builder is configured with the connection timeout, read timeout and sslContext from this handler.This method allows creating a new builder based on the current handler's configuration, which can be used to create a new handler with modified URL.
- Returns:
- A pre-configured
HttpHandler.HttpHandlerBuilderwith the same timeouts as this handler
-
pingHead
Pings the URI using the HEAD method and returns the HTTP status code.- Returns:
- The HTTP status code family, or
HttpStatusFamily.UNKNOWNif an error occurred
-
pingGet
Pings the URI using the GET method and returns the HTTP status code.- Returns:
- The HTTP status code family, or
HttpStatusFamily.UNKNOWNif an error occurred
-
createHttpClient
Gets the configuredHttpClientfor making HTTP requests. The HttpClient is created once during construction and reused for all requests, improving performance by leveraging connection pooling.- Returns:
- A configured
HttpClientwith the SSL context and connection timeout
-