@@ -101,31 +101,7 @@ public class RemoteWebDriverBuilder {
101101 config -> {
102102 HttpClient .Factory factory = HttpClient .Factory .createDefault ();
103103 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 ));
129105 };
130106 private ClientConfig clientConfig = ClientConfig .defaultConfig ();
131107 private URI remoteHost = null ;
@@ -412,8 +388,7 @@ private WebDriver getRemoteDriver() {
412388 HttpHandler handler =
413389 Require .nonNull ("Http handler" , client )
414390 .with (
415- new CloseHttpClientFilter (client )
416- .andThen (new AddWebDriverSpecHeaders ())
391+ new AddWebDriverSpecHeaders ()
417392 .andThen (new ErrorFilter ())
418393 .andThen (new DumpHttpExchangeFilter ()));
419394
@@ -531,9 +506,11 @@ private NewSessionPayload getPayload() {
531506
532507 private static class CloseHttpClientFilter implements Filter {
533508
534- private final HttpHandler client ;
509+ private final HttpClient .Factory factory ;
510+ private final HttpClient client ;
535511
536- CloseHttpClientFilter (HttpHandler client ) {
512+ CloseHttpClientFilter (HttpClient .Factory factory , HttpClient client ) {
513+ this .factory = Require .nonNull ("Http client factory" , factory );
537514 this .client = Require .nonNull ("Http client" , client );
538515 }
539516
@@ -543,16 +520,17 @@ public HttpHandler apply(HttpHandler next) {
543520 try {
544521 return next .execute (req );
545522 } finally {
546- if (req .getMethod () == DELETE && client instanceof Closeable ) {
523+ if (req .getMethod () == DELETE ) {
547524 HttpSessionId .getSessionId (req .getUri ())
548525 .ifPresent (
549526 id -> {
550527 if (("/session/" + id ).equals (req .getUri ())) {
551528 try {
552- (( Closeable ) client ) .close ();
553- } catch (IOException e ) {
529+ client .close ();
530+ } catch (Exception e ) {
554531 LOG .log (WARNING , "Exception swallowed while closing http client" , e );
555532 }
533+ factory .cleanupIdleClients ();
556534 }
557535 });
558536 }
0 commit comments