Skip to content

Commit 8c0ed84

Browse files
committed
Refactoring HttpClient to stop using deprecated methods of Apache HC
1 parent 8e2fda8 commit 8c0ed84

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class HttpCommandExecutor implements CommandExecutor, NeedsLocalLogs {
4949

5050
private final URL remoteServer;
5151
private final HttpClient client;
52+
private final HttpClient.Factory httpClientFactory;
5253
private final Map<String, CommandInfo> additionalCommands;
5354
private CommandCodec<HttpRequest> commandCodec;
5455
private ResponseCodec<HttpResponse> responseCodec;
@@ -85,6 +86,7 @@ public HttpCommandExecutor(
8586
}
8687

8788
this.additionalCommands = additionalCommands;
89+
this.httpClientFactory = httpClientFactory;
8890
this.client = httpClientFactory.createClient(remoteServer);
8991
}
9092

@@ -171,7 +173,7 @@ public Response execute(Command command) throws IOException {
171173
}
172174
}
173175
if (QUIT.equals(command.getName())) {
174-
client.close();
176+
httpClientFactory.cleanupIdleClients();
175177
}
176178
return response;
177179
} catch (UnsupportedCommandException e) {

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ public interface HttpClient {
3434
* @throws IOException if an I/O error occurs.
3535
*/
3636
HttpResponse execute(HttpRequest request, boolean followRedirects) throws IOException;
37-
37+
3838
/**
39-
* Closes the connections associated with this client.
40-
*
41-
* @throws IOException if an I/O error occurs.
42-
*/
39+
* Closes the connections associated with this client.
40+
*
41+
* @throws IOException if an I/O error occurs.
42+
* @deprecated This responsibility moved to Factory
43+
*/
44+
@Deprecated
4345
void close() throws IOException;
4446

4547
interface Factory {
@@ -51,5 +53,10 @@ interface Factory {
5153
* @return HttpClient
5254
*/
5355
HttpClient createClient(URL url);
56+
57+
/**
58+
* Closes idle clients.
59+
*/
60+
void cleanupIdleClients();
5461
}
5562
}

java/client/src/org/openqa/selenium/remote/internal/ApacheHttpClient.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import java.net.URI;
4545
import java.net.URISyntaxException;
4646
import java.net.URL;
47-
import java.util.concurrent.TimeUnit;
47+
import java.util.StringTokenizer;
4848

4949
public class ApacheHttpClient implements org.openqa.selenium.remote.http.HttpClient {
5050

@@ -136,22 +136,14 @@ private org.apache.http.HttpResponse fallBackExecute(
136136
HttpContext context, HttpUriRequest httpMethod) throws IOException {
137137
try {
138138
return client.execute(targetHost, httpMethod, context);
139-
} catch (BindException e) {
139+
} catch (BindException | NoHttpResponseException e) {
140140
// If we get this, there's a chance we've used all the local ephemeral sockets
141141
// Sleep for a bit to let the OS reclaim them, then try the request again.
142142
try {
143143
Thread.sleep(2000);
144144
} catch (InterruptedException ie) {
145145
throw new RuntimeException(ie);
146146
}
147-
} catch (NoHttpResponseException e) {
148-
// If we get this, there's a chance we've used all the remote ephemeral sockets
149-
// Sleep for a bit to let the OS reclaim them, then try the request again.
150-
try {
151-
Thread.sleep(2000);
152-
} catch (InterruptedException ie) {
153-
throw new RuntimeException(ie);
154-
}
155147
}
156148
return client.execute(targetHost, httpMethod, context);
157149
}
@@ -228,15 +220,21 @@ public org.openqa.selenium.remote.http.HttpClient createClient(URL url) {
228220
checkNotNull(url, "null URL");
229221
HttpClient client;
230222
if (url.getUserInfo() != null) {
223+
StringTokenizer tokens = new StringTokenizer(url.getUserInfo(), ":");
231224
UsernamePasswordCredentials credentials =
232-
new UsernamePasswordCredentials(url.getUserInfo());
225+
new UsernamePasswordCredentials(tokens.nextToken(), tokens.nextToken());
233226
client = clientFactory.createHttpClient(credentials);
234227
} else {
235228
client = clientFactory.getHttpClient();
236229
}
237230
return new ApacheHttpClient(client, url);
238231
}
239232

233+
@Override
234+
public void cleanupIdleClients() {
235+
clientFactory.cleanupIdleClients();
236+
}
237+
240238
private static synchronized HttpClientFactory getDefaultHttpClientFactory() {
241239
if (defaultClientFactory == null) {
242240
defaultClientFactory = new HttpClientFactory();
@@ -245,8 +243,8 @@ private static synchronized HttpClientFactory getDefaultHttpClientFactory() {
245243
}
246244
}
247245

246+
@Deprecated
248247
@Override
249248
public void close() throws IOException {
250-
client.getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
251249
}
252250
}

java/client/src/org/openqa/selenium/remote/internal/HttpClientFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
import java.io.IOException;
4949
import java.net.ProxySelector;
50+
import java.util.concurrent.TimeUnit;
5051

5152
public class HttpClientFactory {
5253

@@ -156,6 +157,10 @@ public void close() {
156157
gridClientConnectionManager.shutdown();
157158
}
158159

160+
void cleanupIdleClients() {
161+
gridClientConnectionManager.closeIdleConnections(0, TimeUnit.SECONDS);
162+
}
163+
159164
static class MyRedirectHandler implements RedirectStrategy {
160165

161166
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)

java/client/src/org/openqa/selenium/remote/internal/JreHttpClient.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public HttpResponse execute(HttpRequest request, boolean followRedirects) throws
113113
}
114114
}
115115

116+
@Deprecated
116117
@Override
117118
public void close() throws IOException {
118-
119119
}
120120

121121
public static class Factory implements HttpClient.Factory {
@@ -124,5 +124,9 @@ public static class Factory implements HttpClient.Factory {
124124
public HttpClient createClient(URL url) {
125125
return new JreHttpClient(Objects.requireNonNull(url, "Base URL must be set"));
126126
}
127+
128+
@Override
129+
public void cleanupIdleClients() {
130+
}
127131
}
128132
}

0 commit comments

Comments
 (0)