From dd00a0ab56b0b6483d48650c16028032c67b7174 Mon Sep 17 00:00:00 2001 From: Titus Fortner Date: Wed, 11 Oct 2023 18:04:07 -0500 Subject: [PATCH] [java] allow setting version in the Http Client Config (#12919) * [java] allow setting version in the Http Client Config * [java] set client config version as string and convert in Jdk Client --- .../selenium/remote/http/ClientConfig.java | 57 +++++++++++++++---- .../remote/http/jdk/JdkHttpClient.java | 6 ++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/java/src/org/openqa/selenium/remote/http/ClientConfig.java b/java/src/org/openqa/selenium/remote/http/ClientConfig.java index 26552eeb24327..b7ce769ad4651 100644 --- a/java/src/org/openqa/selenium/remote/http/ClientConfig.java +++ b/java/src/org/openqa/selenium/remote/http/ClientConfig.java @@ -38,8 +38,8 @@ public class ClientConfig { private final Filter filters; private final Proxy proxy; private final Credentials credentials; - private final SSLContext sslContext; + private String version; protected ClientConfig( URI baseUri, @@ -48,7 +48,8 @@ protected ClientConfig( Filter filters, Proxy proxy, Credentials credentials, - SSLContext sslContext) { + SSLContext sslContext, + String version) { this.baseUri = baseUri; this.connectionTimeout = Require.nonNegative("Connection timeout", connectionTimeout); this.readTimeout = Require.nonNegative("Read timeout", readTimeout); @@ -56,11 +57,19 @@ protected ClientConfig( this.proxy = proxy; this.credentials = credentials; this.sslContext = sslContext; + this.version = version; } public static ClientConfig defaultConfig() { return new ClientConfig( - null, Duration.ofSeconds(10), Duration.ofMinutes(3), DEFAULT_FILTER, null, null, null); + null, + Duration.ofSeconds(10), + Duration.ofMinutes(3), + DEFAULT_FILTER, + null, + null, + null, + null); } public ClientConfig baseUri(URI baseUri) { @@ -71,7 +80,8 @@ public ClientConfig baseUri(URI baseUri) { filters, proxy, credentials, - sslContext); + sslContext, + version); } public ClientConfig baseUrl(URL baseUrl) { @@ -102,7 +112,8 @@ public ClientConfig connectionTimeout(Duration timeout) { filters, proxy, credentials, - sslContext); + sslContext, + version); } public Duration connectionTimeout() { @@ -117,7 +128,8 @@ public ClientConfig readTimeout(Duration timeout) { filters, proxy, credentials, - sslContext); + sslContext, + version); } public Duration readTimeout() { @@ -133,7 +145,8 @@ public ClientConfig withFilter(Filter filter) { filter.andThen(DEFAULT_FILTER), proxy, credentials, - sslContext); + sslContext, + version); } public ClientConfig withRetries() { @@ -144,7 +157,8 @@ public ClientConfig withRetries() { filters.andThen(RETRY_FILTER), proxy, credentials, - sslContext); + sslContext, + version); } public Filter filter() { @@ -159,7 +173,8 @@ public ClientConfig proxy(Proxy proxy) { filters, Require.nonNull("Proxy", proxy), credentials, - sslContext); + sslContext, + version); } public Proxy proxy() { @@ -174,7 +189,8 @@ public ClientConfig authenticateAs(Credentials credentials) { filters, proxy, Require.nonNull("Credentials", credentials), - sslContext); + sslContext, + version); } public Credentials credentials() { @@ -189,13 +205,30 @@ public ClientConfig sslContext(SSLContext sslContext) { filters, proxy, credentials, - Require.nonNull("SSL Context", sslContext)); + Require.nonNull("SSL Context", sslContext), + version); } public SSLContext sslContext() { return sslContext; } + public ClientConfig version(String version) { + return new ClientConfig( + baseUri, + connectionTimeout, + readTimeout, + filters, + proxy, + credentials, + sslContext, + Require.nonNull("Version", version)); + } + + public String version() { + return version; + } + @Override public String toString() { return "ClientConfig{" @@ -213,6 +246,8 @@ public String toString() { + credentials + ", sslcontext=" + sslContext + + ", version=" + + version + '}'; } } diff --git a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java index 019dd8ad06b70..86aed0a109c92 100644 --- a/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java +++ b/java/src/org/openqa/selenium/remote/http/jdk/JdkHttpClient.java @@ -29,6 +29,7 @@ import java.net.SocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.net.http.HttpClient.Version; import java.net.http.HttpResponse.BodyHandler; import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpTimeoutException; @@ -150,6 +151,11 @@ public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { builder.sslContext(sslContext); } + String version = config.version(); + if (version != null) { + builder.version(Version.valueOf(version)); + } + this.client = builder.build(); }