Class HttpHandler

java.lang.Object
de.cuioss.http.client.handler.HttpHandler

public final class HttpHandler extends Object
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 SecureSSLContextProvider for 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

Since:
1.0
See Also: