-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[java] feat: Add native Java 11 HTTP client methods to HttpClient interface #16412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
[java] feat: Add native Java 11 HTTP client methods to HttpClient interface #16412
Conversation
Add sendAsyncNative() and sendNative() methods to leverage java.net.http.HttpClient capabilities while maintaining backward compatibility with existing executeAsync(). ## New Methods Added: - sendAsyncNative(): Asynchronous HTTP requests using CompletableFuture - sendNative(): Synchronous HTTP requests with native error handling ## Benefits: - HTTP/2 support with automatic protocol negotiation and multiplexing - Efficient streaming for large files without memory overhead - Native async operations with CompletableFuture integration - Flexible response handling via BodyHandler (String, File, Stream, Lines) - Better error handling with specific HTTP exceptions (IOException, InterruptedException) - Improved performance for concurrent requests ## Implementation Details: - Added method signatures to HttpClient interface - Implemented native delegation in JdkHttpClient using underlying java.net.http.HttpClient - Added pass-through implementations in TracedHttpClient - Added UnsupportedOperationException stubs in test classes and utility clients - Maintained full backward compatibility with existing methods ## Testing: - Created comprehensive unit tests (NativeHttpClientMethodsTest) - Tests cover both synchronous and asynchronous operations - Exception handling validation for IOException and InterruptedException - Request parameter validation for different HTTP methods (GET, POST) - BodyHandler variations testing (String, Void, Stream) - Mock implementations for reliable testing without external dependencies The new methods provide a migration path towards modern Java 11 HTTP APIs without breaking current implementations, enabling developers to leverage native HTTP/2 features, better async handling, and improved performance when using Selenium's HTTP client infrastructure.
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
PR Code Suggestions ✨Explore these optional code suggestions:
|
…terface Add sendAsyncNative() and sendNative() generic methods to leverage java.net.http.HttpClient capabilities with full BodyHandler flexibility while maintaining backward compatibility. ## New Methods Added: - <T> sendAsyncNative(): Generic asynchronous HTTP requests using CompletableFuture - <T> sendNative(): Generic synchronous HTTP requests with native error handling ## Key Features: - **Full BodyHandler Support**: String, File, Stream, Lines, ByteArray, Void (discarding) - **HTTP/2 Support**: Automatic protocol negotiation and multiplexing - **Efficient Streaming**: Large files without memory overhead via BodyHandlers.ofFile() - **Native Async Operations**: CompletableFuture integration with proper type safety - **Flexible Response Handling**: Type-safe responses based on BodyHandler type - **Better Error Handling**: Specific HTTP exceptions (IOException, InterruptedException) - **Improved Performance**: Concurrent requests and HTTP/2 optimizations ## Implementation Details: - Added generic method signatures to HttpClient interface with proper JavaDoc - Implemented native delegation in JdkHttpClient using underlying java.net.http.HttpClient - Added pass-through generic implementations in TracedHttpClient - Added UnsupportedOperationException stubs in test classes and utility clients - Maintained full backward compatibility with existing executeAsync() method - Enhanced type safety with proper generic constraints
User description
Add sendAsyncNative() and sendNative() methods to leverage java.net.http.HttpClient capabilities while maintaining backward compatibility with existing executeAsync().
New Methods Added:
sendAsyncNative()
: Asynchronous HTTP requests using CompletableFuturesendNative()
: Synchronous HTTP requests with native error handlingBenefits:
Implementation Details:
Testing:
The new methods provide a migration path towards modern Java 11 HTTP APIs without breaking current implementations, enabling developers to leverage native HTTP/2 features, better async handling, and improved performance when using Selenium's HTTP client infrastructure.
🔗 Related Issues
Addresses the need for leveraging native Java 11 HTTP client capabilities in Selenium's HTTP infrastructure.
💥 What does this PR do?
This PR adds two new methods to the
HttpClient
interface that expose the native Java 11java.net.http.HttpClient
functionality:sendAsyncNative()
: Enables asynchronous HTTP requests using the nativeCompletableFuture
API with full access to Java 11's HTTP/2 support and advanced BodyHandlerssendNative()
: Provides synchronous HTTP requests with native error handling and streaming capabilitiesThe implementation maintains 100% backward compatibility while offering developers a modern alternative that can:
CompletableFuture
🔧 Implementation Notes
Design Decisions:
HttpClient
interface requiring all implementations to provide native method supportjava.net.http.HttpClient
for maximum performance and feature parityUnsupportedOperationException
for the native methods, clearly indicating they're not intended for production HTTP operationsjava.net.http.HttpRequest
andjava.net.http.HttpResponse<String>
directly to maintain type safety and IDE supportAlternative Approaches Considered:
Follow-on Work:
Migration Path:
🔄 Types of changes
Files Modified:
HttpClient.java
- Added new method signaturesJdkHttpClient.java
- Implemented native delegationTracedHttpClient.java
- Added pass-through implementationsRemoteWebDriverBuilder.java
- Added stub implementationsRoutableHttpClientFactory.java
- Added stub implementationsProtocolHandshakeTest.java
- Added stub implementationsPassthroughHttpClient.java
- Added stub implementationsNativeHttpClientMethodsTest.java
- New comprehensive test suiteTest Coverage:
PR Type
Enhancement
Description
Add native Java 11 HTTP client methods to HttpClient interface
Implement
sendAsyncNative()
andsendNative()
methods for HTTP/2 supportMaintain backward compatibility with existing HTTP client implementations
Add comprehensive unit tests for new native HTTP functionality
Diagram Walkthrough
File Walkthrough
HttpClient.java
Add native HTTP client method signatures
java/src/org/openqa/selenium/remote/http/HttpClient.java
sendAsyncNative()
method returning CompletableFuture>sendNative()
method for synchronous HTTP requestsJdkHttpClient.java
Implement native HTTP methods in JdkHttpClient
java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java
sendAsyncNative()
delegating to underlyingjava.net.http.HttpClient
sendNative()
with direct delegation for synchronous requestsTracedHttpClient.java
Add native method delegation in TracedHttpClient
java/src/org/openqa/selenium/remote/tracing/TracedHttpClient.java
sendAsyncNative()
andsendNative()
RoutableHttpClientFactory.java
Add unsupported native method stubs
java/src/org/openqa/selenium/grid/web/RoutableHttpClientFactory.java
RemoteWebDriverBuilder.java
Add native method stubs in RemoteWebDriverBuilder
java/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java
PassthroughHttpClient.java
Add native method stubs in test utility
java/test/org/openqa/selenium/grid/testing/PassthroughHttpClient.java
sendAsyncNative()
andsendNative()
ProtocolHandshakeTest.java
Update test HttpClient with native method stubs
java/test/org/openqa/selenium/remote/ProtocolHandshakeTest.java
methods
NativeHttpClientMethodsTest.java
Add comprehensive native HTTP method tests
java/test/org/openqa/selenium/remote/http/NativeHttpClientMethodsTest.java
variations
simulation