Skip to content

Commit 7657acb

Browse files
committed
Add an HttpClient backed by OkHttp
1 parent bb788f9 commit 7657acb

File tree

14 files changed

+167
-3
lines changed

14 files changed

+167
-3
lines changed

.idea/libraries/okhttp.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/client/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@
4141
<classpathentry kind="lib" path="/third-party/java/github/org.eclipse.egit.github.core-2.1.5.jar" sourcepath="/third-party/java/github/org.eclipse.egit.github.core-2.1.5.jar-sources.jar"/>
4242
<classpathentry kind="lib" path="/third-party/java/bytebuddy/byte-buddy-1.7.9.jar" sourcepath="/third-party/java/bytebuddy/byte-buddy-1.7.9-sources.jar"/>
4343
<classpathentry kind="lib" path="/third-party/java/javaparser/javaparser-core-3.5.7.jar" sourcepath="/third-party/java/javaparser/javaparser-core-3.5.7-sources.jar"/>
44+
<classpathentry kind="lib" path="/third-party/java/okhttp3/okhttp-3.9.1.jar"/>
45+
<classpathentry kind="lib" path="/third-party/java/okio/okio-1.13.0.jar"/>
4446
<classpathentry kind="output" path="build/production"/>
4547
</classpath>

java/client/client.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<orderEntry type="library" scope="TEST" name="websocket" level="project" />
4343
<orderEntry type="library" scope="TEST" name="github" level="project" />
4444
<orderEntry type="library" name="javaparser" level="project" />
45+
<orderEntry type="library" name="okhttp" level="project" />
4546
</component>
4647
<component name="sonarModuleSettings">
4748
<option name="alternativeWorkingDirPath" value="" />

java/client/src/org/openqa/selenium/remote/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ java_library(name = 'remote-lib',
123123
'internal/ApacheHttpClient.java',
124124
'internal/HttpClientFactory.java',
125125
'internal/JreHttpClient.java',
126+
'internal/OkHttpClient.java',
126127
'internal/JsonToWebElementConverter.java',
127128
'internal/WebElementToJsonConverter.java',
128129
'mobile/RemoteNetworkConnection.java',
@@ -139,6 +140,7 @@ java_library(name = 'remote-lib',
139140
'//java/client/src/org/openqa/selenium/json:json',
140141
'//java/client/src/org/openqa/selenium/remote/session:session',
141142
'//third_party/java/httpcomponents:httpclient',
143+
'//third_party/java/okhttp3:okhttp',
142144
],
143145
deps = [
144146
':http-session-id',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Iterable<String> getHeaders(String name) {
7878
public String getHeader(String name) {
7979
return headers.entries().stream()
8080
.filter(e -> Objects.nonNull(e.getKey()))
81-
.filter(e -> e.getKey().toLowerCase().equals(name.toLowerCase()))
81+
.filter(e -> e.getKey().equalsIgnoreCase(name.toLowerCase()))
8282
.map(Map.Entry::getValue)
8383
.findFirst()
8484
.orElse(null);
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.internal;
19+
20+
import org.openqa.selenium.remote.http.HttpClient;
21+
import org.openqa.selenium.remote.http.HttpRequest;
22+
import org.openqa.selenium.remote.http.HttpResponse;
23+
24+
import okhttp3.ConnectionPool;
25+
import okhttp3.MediaType;
26+
import okhttp3.Request;
27+
import okhttp3.RequestBody;
28+
import okhttp3.Response;
29+
30+
import java.io.IOException;
31+
import java.net.URL;
32+
import java.util.Optional;
33+
34+
public class OkHttpClient implements HttpClient {
35+
36+
private final okhttp3.OkHttpClient client;
37+
private final URL baseUrl;
38+
39+
public OkHttpClient(okhttp3.OkHttpClient client, URL url) {
40+
this.client = client;
41+
this.baseUrl = url;
42+
}
43+
44+
@Override
45+
public HttpResponse execute(HttpRequest request, boolean followRedirects) throws IOException {
46+
if (followRedirects != client.followRedirects()) {
47+
throw new IllegalArgumentException("Unable to change how the http client follows redirets");
48+
}
49+
50+
Request.Builder builder = new Request.Builder();
51+
52+
builder.url(new URL(baseUrl.toString() + request.getUri()));
53+
54+
for (String name : request.getHeaderNames()) {
55+
for (String value : request.getHeaders(name)) {
56+
builder.addHeader(name, value);
57+
}
58+
}
59+
60+
switch (request.getMethod()) {
61+
case GET:
62+
builder.get();
63+
break;
64+
65+
case POST:
66+
String rawType = Optional.of(request.getHeader("Content-Type"))
67+
.orElse("application/json; charset=utf-8");
68+
MediaType type = MediaType.parse(rawType);
69+
RequestBody body = RequestBody.create(type, request.getContent());
70+
builder.post(body);
71+
break;
72+
73+
case DELETE:
74+
builder.delete();
75+
}
76+
77+
Response response = client.newCall(builder.build()).execute();
78+
79+
HttpResponse toReturn = new HttpResponse();
80+
toReturn.setContent(response.body().bytes());
81+
toReturn.setStatus(response.code());
82+
response.headers().names().forEach(
83+
name -> response.headers(name).forEach(value -> toReturn.addHeader(name, value)));
84+
85+
return toReturn;
86+
}
87+
88+
@Override
89+
public void close() {
90+
// No-op
91+
}
92+
93+
public static class Factory implements HttpClient.Factory {
94+
95+
private ConnectionPool pool = new ConnectionPool();
96+
97+
@Override
98+
public HttpClient createClient(URL url) {
99+
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
100+
.connectionPool(pool)
101+
.followRedirects(true)
102+
.followSslRedirects(true)
103+
.build();
104+
return new OkHttpClient(client, url);
105+
}
106+
107+
@Override
108+
public void cleanupIdleClients() {
109+
pool.evictAll();
110+
}
111+
}
112+
}

java/client/test/org/openqa/selenium/remote/internal/HttpClientTestBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public void responseShouldKeepMultipleHeadersSeparate() throws Exception {
7272

7373
ImmutableList<String> values = ImmutableList.copyOf(response.getHeaders("Cheese"));
7474

75-
assertTrue(values.contains("Cheddar"));
76-
assertTrue(values.contains("Brie, Gouda"));
75+
assertTrue(values.toString(), values.contains("Cheddar"));
76+
assertTrue(values.toString(), values.contains("Brie, Gouda"));
7777
}
7878

7979
private HttpResponse getResponseWithHeaders(final Multimap<String, String> headers)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.openqa.selenium.remote.internal;
2+
3+
import org.openqa.selenium.remote.http.HttpClient;
4+
5+
public class OkHttpClientTest extends HttpClientTestBase {
6+
7+
@Override
8+
protected HttpClient.Factory createFactory() {
9+
return new OkHttpClient.Factory();
10+
}
11+
}

third_party/java/okhttp3/BUCK

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
prebuilt_jar(
2+
name = 'okhttp',
3+
maven_coords = 'com.squareup.okhttp3:okhttp:jar:3.9.1',
4+
binary_jar = 'okhttp-3.9.1.jar',
5+
source_jar = 'okhttp-3.9.1-sources.jar',
6+
deps = [
7+
'//third_party/java/okio:okio'
8+
],
9+
visibility = [
10+
'//java/client/src/org/openqa/selenium/remote:remote-lib',
11+
],
12+
)
13+
Binary file not shown.

0 commit comments

Comments
 (0)