Package com.storedobject.common
Class HTTP2
java.lang.Object
com.storedobject.common.HTTP2
The HTTP2 class facilitates HTTP/2 client-server communication
with helper methods and a builder pattern for request customization.
It leverages Java's HttpClient for synchronous and asynchronous
HTTP calls and provides utilities for handling and parsing responses.
Note: This utility class can transparently handle chunked and compressed (zipped or deflated) content as well.
Features:
- Connection pooling optimization with custom client caching
- Automatic retry mechanism for transient failures
- Configurable debug logging with custom log destinations
- Proper handling of chunked transfer encoding
- Automatic decompression of gzip/deflate responses
- Virtual threads executor for async operations (Java 21+)
Typical usage:
String s = HTTP2.build("https://www.google.com").string();
System.out.println("Google's default page's content is: " + s);
JSON json = HTTP2.build("https://www.example.com?api=xxx").json(); // Get the JSON content
// With retry and custom logger
HTTP2.setLogWriter(new PrintWriter(System.out));
String data = HTTP2.builder("https://api.example.com/data")
.retry(3, Duration.ofMillis(500))
.debug()
.string();
- Author:
- Syam
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis class provides a builder pattern for constructing HTTP requests. -
Method Summary
Modifier and TypeMethodDescriptionstatic HTTP2.Builderbuilder()Creates a new instance of theBuilderclass, which provides methods to configure and build an HTTP request.static HTTP2.BuilderCreates a newBuilderinstance initialized with the specified URL.static booleanChecks if global debug is enabled.static booleanisServiceAvailable(String url) Checks if a service is available with default timeout of 5 seconds.static booleanisServiceAvailable(String url, Duration timeout) Checks if a service is available by sending a HEAD request.static voidsetGlobalDebugEnabled(boolean enabled) Enables or disables global debug logging.static voidsetLogWriter(LogWriter logWriter) Sets the global log writer for all HTTP2 debug output.
-
Method Details
-
setLogWriter
Sets the global log writer for all HTTP2 debug output.- Parameters:
logWriter- the log writer to use
-
setGlobalDebugEnabled
public static void setGlobalDebugEnabled(boolean enabled) Enables or disables global debug logging.- Parameters:
enabled- true to enable debug logging globally
-
isGlobalDebugEnabled
public static boolean isGlobalDebugEnabled()Checks if global debug is enabled.- Returns:
- true if global debug is enabled, false otherwise
-
isServiceAvailable
-
isServiceAvailable
Checks if a service is available with default timeout of 5 seconds.- Parameters:
url- the URL to check- Returns:
- true if service responds with 200 OK within 5 seconds
-
builder
Creates a new instance of theBuilderclass, which provides methods to configure and build an HTTP request.- Returns:
- a new
Builderinstance
-
builder
Creates a newBuilderinstance initialized with the specified URL.- Parameters:
url- the URL to be set for the builder- Returns:
- a new
Builderinstance with the URL set
-