Skip to content

Commit 395c7db

Browse files
committed
Provide mechanism for getting the default HttpClient.Factory implmentation
1 parent 9891be0 commit 395c7db

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

java/client/src/org/openqa/selenium/remote/HttpCommandExecutor.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,7 @@
4646

4747
public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs {
4848

49-
private final static HttpClient.Factory defaultClientFactory;
50-
static {
51-
String defaultFactory = System.getProperty("webdriver.http.factory", "okhttp");
52-
switch (defaultFactory) {
53-
case "okhttp":
54-
defaultClientFactory = new OkHttpClient.Factory();
55-
break;
56-
57-
case "apache":
58-
default:
59-
defaultClientFactory = new ApacheHttpClient.Factory();
60-
break;
61-
}
62-
}
49+
private final static HttpClient.Factory defaultClientFactory = HttpClient.Factory.createDefault();
6350

6451
private final URL remoteServer;
6552
private final HttpClient client;

java/client/src/org/openqa/selenium/remote/http/HttpClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.openqa.selenium.remote.http;
1919

20+
import org.openqa.selenium.remote.internal.ApacheHttpClient;
21+
import org.openqa.selenium.remote.internal.OkHttpClient;
22+
2023
import java.io.IOException;
2124
import java.net.URL;
2225

@@ -58,6 +61,18 @@ public interface HttpClient {
5861

5962
interface Factory {
6063

64+
static Factory createDefault() {
65+
String defaultFactory = System.getProperty("webdriver.http.factory", "okhttp");
66+
switch (defaultFactory) {
67+
case "okhttp":
68+
return new OkHttpClient.Factory();
69+
70+
case "apache":
71+
default:
72+
return new ApacheHttpClient.Factory();
73+
}
74+
}
75+
6176
/**
6277
* Creates a HTTP client that will send requests to the given URL.
6378
*

java/client/test/org/openqa/selenium/environment/webserver/JettyAppServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.openqa.selenium.remote.http.HttpMethod;
3434
import org.openqa.selenium.remote.http.HttpRequest;
3535
import org.openqa.selenium.remote.http.HttpResponse;
36-
import org.openqa.selenium.remote.internal.ApacheHttpClient;
3736
import org.openqa.selenium.testing.InProject;
3837
import org.seleniumhq.jetty9.http.HttpVersion;
3938
import org.seleniumhq.jetty9.http.MimeTypes;
@@ -206,7 +205,7 @@ public String create(Page page) {
206205
converted.addProperty("content", page.toString());
207206
byte[] data = converted.toString().getBytes(UTF_8);
208207

209-
HttpClient client = new ApacheHttpClient.Factory().createClient(new URL(whereIs("/")));
208+
HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(whereIs("/")));
210209
HttpRequest request = new HttpRequest(HttpMethod.POST, "/common/createPage");
211210
request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
212211
request.setContent(data);

java/client/test/org/openqa/selenium/testing/drivers/GridSupplier.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.openqa.selenium.remote.http.HttpMethod;
2828
import org.openqa.selenium.remote.http.HttpRequest;
2929
import org.openqa.selenium.remote.http.HttpResponse;
30-
import org.openqa.selenium.remote.internal.ApacheHttpClient;
3130
import org.openqa.selenium.support.ui.FluentWait;
3231
import org.openqa.selenium.support.ui.Wait;
3332

@@ -82,7 +81,7 @@ private synchronized void startServers() {
8281
}
8382

8483
// Keep polling the status page of the hub until it claims to be ready
85-
HttpClient client = new ApacheHttpClient.Factory().createClient(hub.getWebDriverUrl());
84+
HttpClient client = HttpClient.Factory.createDefault().createClient(hub.getWebDriverUrl());
8685
Json json = new Json();
8786
Wait<HttpClient> wait = new FluentWait<>(client)
8887
.ignoring(RuntimeException.class)

java/server/src/org/openqa/selenium/remote/server/ProtocolConverter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@
2929
import org.openqa.selenium.remote.http.HttpResponse;
3030
import org.openqa.selenium.remote.internal.ApacheHttpClient;
3131
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;
32+
import org.openqa.selenium.remote.internal.OkHttpClient;
3233

3334
import java.io.IOException;
3435
import java.net.URL;
3536
import java.util.Map;
3637

3738
class ProtocolConverter implements SessionCodec {
3839

40+
private final static HttpClient.Factory defaultClientFactory;
41+
static {
42+
String defaultFactory = System.getProperty("webdriver.http.factory", "okhttp");
43+
switch (defaultFactory) {
44+
case "okhttp":
45+
defaultClientFactory = new OkHttpClient.Factory();
46+
break;
47+
48+
case "apache":
49+
default:
50+
defaultClientFactory = new ApacheHttpClient.Factory();
51+
break;
52+
}
53+
}
54+
3955
private final static ImmutableSet<String> IGNORED_REQ_HEADERS = ImmutableSet.<String>builder()
4056
.add("connection")
4157
.add("keep-alive")
@@ -66,7 +82,7 @@ public ProtocolConverter(
6682
this.downstreamResponse = downstreamResponse;
6783
this.upstreamResponse = upstreamResponse;
6884

69-
client = new ApacheHttpClient.Factory().createClient(upstreamUrl);
85+
client = HttpClient.Factory.createDefault().createClient(upstreamUrl);
7086
converter = new JsonToWebElementConverter(null);
7187
}
7288

java/server/src/org/openqa/selenium/remote/server/RemoteSession.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.openqa.selenium.remote.http.JsonHttpResponseCodec;
4646
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
4747
import org.openqa.selenium.remote.http.W3CHttpResponseCodec;
48-
import org.openqa.selenium.remote.internal.ApacheHttpClient;
4948

5049
import java.io.File;
5150
import java.io.IOException;
@@ -136,7 +135,7 @@ protected Optional<ActiveSession> performHandshake(
136135
Set<Dialect> downstreamDialects,
137136
Capabilities capabilities) {
138137
try {
139-
HttpClient client = new ApacheHttpClient.Factory().createClient(url);
138+
HttpClient client = HttpClient.Factory.createDefault().createClient(url);
140139

141140
Command command = new Command(
142141
null,

0 commit comments

Comments
 (0)