diff --git a/java/client/src/org/openqa/selenium/remote/http/netty/CreateNettyClient.java b/java/client/src/org/openqa/selenium/remote/http/netty/CreateNettyClient.java deleted file mode 100644 index d017dd0dc2441..0000000000000 --- a/java/client/src/org/openqa/selenium/remote/http/netty/CreateNettyClient.java +++ /dev/null @@ -1,49 +0,0 @@ -// Licensed to the Software Freedom Conservancy (SFC) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The SFC licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -package org.openqa.selenium.remote.http.netty; - -import com.google.common.base.Strings; - -import org.asynchttpclient.AsyncHttpClient; -import org.asynchttpclient.DefaultAsyncHttpClientConfig; -import org.asynchttpclient.Dsl; -import org.openqa.selenium.remote.http.ClientConfig; - -import java.util.Objects; -import java.util.function.Function; - -class CreateNettyClient implements Function { - - @Override - public AsyncHttpClient apply(ClientConfig config) { - Objects.requireNonNull(config, "Client config to use must be set."); - - DefaultAsyncHttpClientConfig.Builder clientConfig = Dsl.config(); - - String info = config.baseUrl().getUserInfo(); - if (!Strings.isNullOrEmpty(info)) { - String[] parts = info.split(":", 2); - String user = parts[0]; - String pass = parts.length > 1 ? parts[1] : null; - - clientConfig.setRealm(Dsl.basicAuthRealm(user, pass).setUsePreemptiveAuth(true).build()); - } - - return Dsl.asyncHttpClient(clientConfig); - } -} diff --git a/java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java b/java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java index c528b59251939..5835acb8c2480 100644 --- a/java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java +++ b/java/client/src/org/openqa/selenium/remote/http/netty/NettyClient.java @@ -17,6 +17,8 @@ package org.openqa.selenium.remote.http.netty; +import org.asynchttpclient.AsyncHttpClient; +import org.asynchttpclient.Dsl; import org.openqa.selenium.remote.http.ClientConfig; import org.openqa.selenium.remote.http.Filter; import org.openqa.selenium.remote.http.HttpClient; @@ -30,6 +32,8 @@ public class NettyClient implements HttpClient { + private final static AsyncHttpClient httpClient = Dsl.asyncHttpClient(); + private final HttpHandler handler; private BiFunction toWebSocket; @@ -65,7 +69,7 @@ public static class Factory implements HttpClient.Factory { public HttpClient createClient(ClientConfig config) { Objects.requireNonNull(config, "Client config to use must be set."); - return new NettyClient(new NettyHttpHandler(config).with(config.filter()), NettyWebSocket.create(config)); + return new NettyClient(new NettyHttpHandler(config, httpClient).with(config.filter()), NettyWebSocket.create(config, httpClient)); } } } diff --git a/java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java b/java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java index 4f8a153925f4e..b95d500e3d9bf 100644 --- a/java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java +++ b/java/client/src/org/openqa/selenium/remote/http/netty/NettyHttpHandler.java @@ -31,12 +31,12 @@ public class NettyHttpHandler extends RemoteCall { - private final AsyncHttpClient client; private final HttpHandler handler; + private final AsyncHttpClient client; - public NettyHttpHandler(ClientConfig config) { + public NettyHttpHandler(ClientConfig config, AsyncHttpClient client) { super(config); - this.client = new CreateNettyClient().apply(config); + this.client = client; this.handler = config.filter().andFinally(this::makeCall); } diff --git a/java/client/src/org/openqa/selenium/remote/http/netty/NettyMessages.java b/java/client/src/org/openqa/selenium/remote/http/netty/NettyMessages.java index e2cb979641539..cb379ca321bc4 100644 --- a/java/client/src/org/openqa/selenium/remote/http/netty/NettyMessages.java +++ b/java/client/src/org/openqa/selenium/remote/http/netty/NettyMessages.java @@ -19,6 +19,7 @@ import static org.openqa.selenium.remote.http.Contents.empty; +import org.asynchttpclient.Dsl; import org.asynchttpclient.Request; import org.asynchttpclient.RequestBuilder; import org.asynchttpclient.Response; @@ -31,6 +32,8 @@ import static org.asynchttpclient.Dsl.request; +import com.google.common.base.Strings; + class NettyMessages { private NettyMessages() { @@ -64,6 +67,15 @@ protected static Request toNettyRequest(URI baseUrl, HttpRequest request) { } } + String info = baseUrl.getUserInfo(); + if (!Strings.isNullOrEmpty(info)) { + String[] parts = info.split(":", 2); + String user = parts[0]; + String pass = parts.length > 1 ? parts[1] : null; + + builder.setRealm(Dsl.basicAuthRealm(user, pass).setUsePreemptiveAuth(true).build()); + } + if (request.getMethod().equals(HttpMethod.POST)) { builder.setBody(request.getContent().get()); } diff --git a/java/client/src/org/openqa/selenium/remote/http/netty/NettyWebSocket.java b/java/client/src/org/openqa/selenium/remote/http/netty/NettyWebSocket.java index 577c62eb746a6..ea24dd0923c65 100644 --- a/java/client/src/org/openqa/selenium/remote/http/netty/NettyWebSocket.java +++ b/java/client/src/org/openqa/selenium/remote/http/netty/NettyWebSocket.java @@ -48,7 +48,7 @@ class NettyWebSocket implements WebSocket { private static final Logger log = Logger.getLogger(NettyWebSocket.class.getName()); - private org.asynchttpclient.ws.WebSocket socket; + private final org.asynchttpclient.ws.WebSocket socket; private NettyWebSocket(AsyncHttpClient client, org.asynchttpclient.Request request, Listener listener) { Objects.requireNonNull(client, "HTTP client to use must be set."); @@ -97,7 +97,7 @@ public void onTextFrame(String payload, boolean finalFragment, int rsv) { } } - static BiFunction create(ClientConfig config) { + static BiFunction create(ClientConfig config, AsyncHttpClient client) { Filter filter = config.filter(); Function filterRequest = req -> { @@ -109,7 +109,6 @@ static BiFunction create(ClientConfig config) return ref.get(); }; - AsyncHttpClient client = new CreateNettyClient().apply(config); return (req, listener) -> { HttpRequest filtered = filterRequest.apply(req);