Skip to content

Commit 456563b

Browse files
committed
[java] Properly converting URLs and logging exceptions in the netty-based http client
1 parent f2e587e commit 456563b

File tree

6 files changed

+47
-39
lines changed

6 files changed

+47
-39
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
package org.openqa.selenium.remote.http;
1919

2020
import java.io.Closeable;
21-
import java.io.UncheckedIOException;
2221

2322
public interface WebSocket extends Closeable {
2423

2524
WebSocket sendText(CharSequence data);
2625

2726
@Override
28-
void close() throws UncheckedIOException;
27+
void close();
2928

3029
void abort();
3130

java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.openqa.selenium.remote.http.HttpResponse;
2626
import org.openqa.selenium.remote.http.WebSocket;
2727

28-
import java.io.UncheckedIOException;
2928
import java.util.Objects;
3029
import java.util.function.BiFunction;
3130

@@ -40,7 +39,7 @@ private NettyClient(HttpHandler handler, BiFunction<HttpRequest, WebSocket.Liste
4039
}
4140

4241
@Override
43-
public HttpResponse execute(HttpRequest request) throws UncheckedIOException {
42+
public HttpResponse execute(HttpRequest request) {
4443
return handler.execute(request);
4544
}
4645

java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ private HttpResponse makeCall(HttpRequest request) {
5454
try {
5555
Response response = whenResponse.get();
5656
return NettyMessages.toSeleniumResponse(response);
57-
} catch (InterruptedException | ExecutionException e) {
58-
throw new RuntimeException(e);
57+
} catch (InterruptedException e) {
58+
Thread.currentThread().interrupt();
59+
throw new RuntimeException("NettyHttpHandler request interrupted", e);
60+
} catch (ExecutionException e) {
61+
throw new RuntimeException("NettyHttpHandler request execution error", e);
5962
}
6063
}
6164
}

java/client/src/org/openqa/selenium/remote/http/netty/NettyWebSocket.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,60 @@
2626
import org.openqa.selenium.remote.http.HttpResponse;
2727
import org.openqa.selenium.remote.http.WebSocket;
2828

29-
import java.io.UncheckedIOException;
29+
import java.net.MalformedURLException;
30+
import java.net.URI;
31+
import java.net.URISyntaxException;
32+
import java.net.URL;
3033
import java.util.Objects;
3134
import java.util.concurrent.ExecutionException;
3235
import java.util.concurrent.atomic.AtomicReference;
3336
import java.util.function.BiFunction;
3437
import java.util.function.Function;
38+
import java.util.logging.Level;
39+
import java.util.logging.Logger;
3540

3641
class NettyWebSocket implements WebSocket {
3742

43+
private static final Logger log = Logger.getLogger(NettyWebSocket.class.getName());
44+
3845
private org.asynchttpclient.ws.WebSocket socket;
3946

4047
private NettyWebSocket(AsyncHttpClient client, org.asynchttpclient.Request request, Listener listener) {
4148
Objects.requireNonNull(client, "HTTP client to use must be set.");
4249
Objects.requireNonNull(listener, "WebSocket listener must be set.");
4350

4451
try {
45-
socket = client.prepareGet(request.toString().replace("http://", "ws://"))
52+
URL origUrl = new URL(request.getUrl());
53+
URI wsUri = new URI("ws", null, origUrl.getHost(), origUrl.getPort(), origUrl.getPath(), null, null);
54+
socket = client.prepareGet(wsUri.toString())
4655
.execute(new WebSocketUpgradeHandler.Builder()
47-
.addWebSocketListener(new WebSocketListener() {
48-
@Override
49-
public void onOpen(org.asynchttpclient.ws.WebSocket websocket) {
50-
51-
}
52-
53-
@Override
54-
public void onClose(org.asynchttpclient.ws.WebSocket websocket, int code, String reason) {
55-
listener.onClose(code, reason);
56-
}
57-
58-
@Override
59-
public void onError(Throwable t) {
60-
listener.onError(t);
61-
}
62-
63-
@Override
64-
public void onTextFrame(String payload, boolean finalFragment, int rsv) {
65-
if (payload != null) {
66-
listener.onText(payload);
67-
}
68-
}
69-
}).build()).get();
56+
.addWebSocketListener(new WebSocketListener() {
57+
@Override
58+
public void onOpen(org.asynchttpclient.ws.WebSocket websocket) {
59+
}
60+
61+
@Override
62+
public void onClose(org.asynchttpclient.ws.WebSocket websocket, int code, String reason) {
63+
listener.onClose(code, reason);
64+
}
65+
66+
@Override
67+
public void onError(Throwable t) {
68+
listener.onError(t);
69+
}
70+
71+
@Override
72+
public void onTextFrame(String payload, boolean finalFragment, int rsv) {
73+
if (payload != null) {
74+
listener.onText(payload);
75+
}
76+
}
77+
}).build()).get();
7078
} catch (InterruptedException e) {
71-
e.printStackTrace();
72-
} catch (ExecutionException e) {
73-
e.printStackTrace();
79+
Thread.currentThread().interrupt();
80+
log.log(Level.WARNING, "NettyWebSocket initial request interrupted", e);
81+
} catch (ExecutionException | MalformedURLException | URISyntaxException e) {
82+
throw new RuntimeException("NettyWebSocket initial request execution error", e);
7483
}
7584
}
7685

@@ -103,7 +112,7 @@ public WebSocket sendText(CharSequence data) {
103112
}
104113

105114
@Override
106-
public void close() throws UncheckedIOException {
115+
public void close() {
107116
socket.sendCloseFrame(1000, "WebDriver closing socket");
108117
}
109118

java/client/src/org/openqa/selenium/remote/http/okhttp/OkHttpClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.openqa.selenium.remote.http.HttpResponse;
2727
import org.openqa.selenium.remote.http.WebSocket;
2828

29-
import java.io.UncheckedIOException;
3029
import java.util.Objects;
3130
import java.util.function.BiFunction;
3231

@@ -41,7 +40,7 @@ private OkHttpClient(HttpHandler handler, BiFunction<HttpRequest, WebSocket.List
4140
}
4241

4342
@Override
44-
public HttpResponse execute(HttpRequest request) throws UncheckedIOException {
43+
public HttpResponse execute(HttpRequest request) {
4544
return handler.execute(request);
4645
}
4746

java/client/src/org/openqa/selenium/remote/http/okhttp/OkHttpWebSocket.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.HttpResponse;
2828
import org.openqa.selenium.remote.http.WebSocket;
2929

30-
import java.io.UncheckedIOException;
3130
import java.util.Objects;
3231
import java.util.concurrent.atomic.AtomicReference;
3332
import java.util.function.BiFunction;
@@ -91,7 +90,7 @@ public WebSocket sendText(CharSequence data) {
9190
}
9291

9392
@Override
94-
public void close() throws UncheckedIOException {
93+
public void close() {
9594
socket.close(1000, "WebDriver closing socket");
9695
}
9796

0 commit comments

Comments
 (0)