@@ -101,31 +101,7 @@ public class RemoteWebDriverBuilder {
101
101
config -> {
102
102
HttpClient .Factory factory = HttpClient .Factory .createDefault ();
103
103
HttpClient client = factory .createClient (config );
104
- return client .with (
105
- next ->
106
- req -> {
107
- try {
108
- return client .execute (req );
109
- } finally {
110
- if (req .getMethod () == DELETE ) {
111
- HttpSessionId .getSessionId (req .getUri ())
112
- .ifPresent (
113
- id -> {
114
- if (("/session/" + id ).equals (req .getUri ())) {
115
- try {
116
- client .close ();
117
- } catch (UncheckedIOException e ) {
118
- LOG .log (
119
- WARNING ,
120
- "Swallowing exception while closing http client" ,
121
- e );
122
- }
123
- factory .cleanupIdleClients ();
124
- }
125
- });
126
- }
127
- }
128
- });
104
+ return client .with (new CloseHttpClientFilter (factory , client ));
129
105
};
130
106
private ClientConfig clientConfig = ClientConfig .defaultConfig ();
131
107
private URI remoteHost = null ;
@@ -412,8 +388,7 @@ private WebDriver getRemoteDriver() {
412
388
HttpHandler handler =
413
389
Require .nonNull ("Http handler" , client )
414
390
.with (
415
- new CloseHttpClientFilter (client )
416
- .andThen (new AddWebDriverSpecHeaders ())
391
+ new AddWebDriverSpecHeaders ()
417
392
.andThen (new ErrorFilter ())
418
393
.andThen (new DumpHttpExchangeFilter ()));
419
394
@@ -531,9 +506,11 @@ private NewSessionPayload getPayload() {
531
506
532
507
private static class CloseHttpClientFilter implements Filter {
533
508
534
- private final HttpHandler client ;
509
+ private final HttpClient .Factory factory ;
510
+ private final HttpClient client ;
535
511
536
- CloseHttpClientFilter (HttpHandler client ) {
512
+ CloseHttpClientFilter (HttpClient .Factory factory , HttpClient client ) {
513
+ this .factory = Require .nonNull ("Http client factory" , factory );
537
514
this .client = Require .nonNull ("Http client" , client );
538
515
}
539
516
@@ -543,16 +520,17 @@ public HttpHandler apply(HttpHandler next) {
543
520
try {
544
521
return next .execute (req );
545
522
} finally {
546
- if (req .getMethod () == DELETE && client instanceof Closeable ) {
523
+ if (req .getMethod () == DELETE ) {
547
524
HttpSessionId .getSessionId (req .getUri ())
548
525
.ifPresent (
549
526
id -> {
550
527
if (("/session/" + id ).equals (req .getUri ())) {
551
528
try {
552
- (( Closeable ) client ) .close ();
553
- } catch (IOException e ) {
529
+ client .close ();
530
+ } catch (Exception e ) {
554
531
LOG .log (WARNING , "Exception swallowed while closing http client" , e );
555
532
}
533
+ factory .cleanupIdleClients ();
556
534
}
557
535
});
558
536
}
0 commit comments