32
32
import org .openqa .selenium .remote .http .HttpResponse ;
33
33
import org .openqa .selenium .remote .http .WebSocket ;
34
34
35
- import java .util .concurrent .atomic .AtomicBoolean ;
36
- import java .util .function .BiFunction ;
37
35
import java .io .IOException ;
36
+ import java .util .function .BiFunction ;
38
37
39
38
public class NettyClient implements HttpClient {
40
39
41
- private static final AsyncHttpClient httpClient =
42
- Dsl .asyncHttpClient (
43
- new DefaultAsyncHttpClientConfig .Builder ()
44
- .setThreadFactory (new DefaultThreadFactory ("AsyncHttpClient" , true ))
45
- .setUseInsecureTrustManager (true )
46
- .setAggregateWebSocketFrameFragments (true )
47
- .setWebSocketMaxBufferSize (Integer .MAX_VALUE )
48
- .setWebSocketMaxFrameSize (Integer .MAX_VALUE ));
49
-
40
+ private final ClientConfig config ;
41
+ private final AsyncHttpClient client ;
50
42
private final HttpHandler handler ;
51
- private BiFunction <HttpRequest , WebSocket .Listener , WebSocket > toWebSocket ;
43
+ private final BiFunction <HttpRequest , WebSocket .Listener , WebSocket > toWebSocket ;
52
44
53
- private NettyClient (HttpHandler handler , BiFunction <HttpRequest , WebSocket .Listener , WebSocket > toWebSocket ) {
54
- this .handler = Require .nonNull ("Handler" , handler );
55
- this .toWebSocket = Require .nonNull ("WebSocket creation function" , toWebSocket );
45
+ private NettyClient (ClientConfig config ) {
46
+ this .config = Require .nonNull ("HTTP client config" , config );
47
+ this .client = createHttpClient (config );
48
+ this .handler = new NettyHttpHandler (config , this .client ).with (config .filter ());
49
+ this .toWebSocket = NettyWebSocket .create (config , this .client );
50
+ }
51
+
52
+ private AsyncHttpClient createHttpClient (ClientConfig config ) {
53
+ DefaultAsyncHttpClientConfig .Builder builder =
54
+ new DefaultAsyncHttpClientConfig .Builder ()
55
+ .setThreadFactory (new DefaultThreadFactory ("AsyncHttpClient" , true ))
56
+ .setUseInsecureTrustManager (true )
57
+ .setAggregateWebSocketFrameFragments (true )
58
+ .setWebSocketMaxBufferSize (Integer .MAX_VALUE )
59
+ .setWebSocketMaxFrameSize (Integer .MAX_VALUE )
60
+ .setConnectTimeout ((int ) config .connectionTimeout ().toMillis ());
61
+
62
+ String info = config .baseUrl ().getUserInfo ();
63
+ if (info != null && !info .equals ("" )) {
64
+ String [] parts = info .split (":" , 2 );
65
+ String user = parts [0 ];
66
+ String pass = parts .length > 1 ? parts [1 ] : null ;
67
+ builder .setRealm (Dsl .basicAuthRealm (user , pass ).setUsePreemptiveAuth (true ).build ());
68
+ }
69
+
70
+ return Dsl .asyncHttpClient (builder );
56
71
}
57
72
58
73
@ Override
@@ -73,29 +88,18 @@ public HttpClient with(Filter filter) {
73
88
Require .nonNull ("Filter" , filter );
74
89
75
90
// TODO: We should probably ensure that websocket requests are run through the filter.
76
- return new NettyClient (handler .with (filter ), toWebSocket );
91
+ return new NettyClient (config .withFilter (filter ));
92
+ }
93
+
94
+ @ Override
95
+ public void close () throws IOException {
96
+ client .close ();
77
97
}
78
98
79
99
@ AutoService (HttpClient .Factory .class )
80
100
@ HttpClientName ("netty" )
81
101
public static class Factory implements HttpClient .Factory {
82
102
83
- private static final AtomicBoolean addedHook = new AtomicBoolean ();
84
-
85
- public Factory () {
86
- if (!addedHook .get ()) {
87
- Runtime .getRuntime ().addShutdownHook (new Thread (this ::callAsyncClientShutdown ));
88
- addedHook .set (true );
89
- }
90
- }
91
-
92
- private void callAsyncClientShutdown () {
93
- try {
94
- httpClient .close ();
95
- } catch (IOException ignore ) {
96
- }
97
- }
98
-
99
103
@ Override
100
104
public HttpClient createClient (ClientConfig config ) {
101
105
Require .nonNull ("Client config" , config );
@@ -104,7 +108,7 @@ public HttpClient createClient(ClientConfig config) {
104
108
return new NettyDomainSocketClient (config );
105
109
}
106
110
107
- return new NettyClient (new NettyHttpHandler ( config , httpClient ). with ( config . filter ()), NettyWebSocket . create ( config , httpClient ) );
111
+ return new NettyClient (config );
108
112
}
109
113
}
110
114
}
0 commit comments