From 176e58d0c4e7e3d23c583093aed1a45de81ea405 Mon Sep 17 00:00:00 2001 From: gregw Date: Wed, 23 Dec 2020 12:34:27 +0100 Subject: [PATCH] Fix #5835 Durable Filters, Servlets and Listeners Update WebSocketUpgradeFilter to have durable configuration. --- .../server/WebSocketUpgradeFilter.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java index b691ab313e14..9d25582b02a1 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.util.EnumSet; +import java.util.Objects; import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -131,12 +132,12 @@ public static WebSocketUpgradeFilter configureContext(ServletContext context) th private NativeWebSocketConfiguration configuration; private String instanceKey; - private boolean localConfiguration = false; + private boolean durableConfiguration = false; + private boolean managedConfiguration = false; private boolean alreadySetToAttribute = false; public WebSocketUpgradeFilter() { - // do nothing } public WebSocketUpgradeFilter(WebSocketServerFactory factory) @@ -146,6 +147,7 @@ public WebSocketUpgradeFilter(WebSocketServerFactory factory) public WebSocketUpgradeFilter(NativeWebSocketConfiguration configuration) { + durableConfiguration = configuration != null; this.configuration = configuration; } @@ -182,14 +184,17 @@ public void destroy() { try { - // We must clear out the configuration as the configuration is cleared on restart. NativeWebSocketConfiguration config = configuration; - boolean localConfig = localConfiguration; - configuration = null; - localConfiguration = false; - alreadySetToAttribute = false; - if (localConfig) + if (!durableConfiguration) + configuration = null; + + boolean managed = managedConfiguration; + managedConfiguration = false; + if (managed) + { config.stop(); + } + alreadySetToAttribute = false; } catch (Exception e) { @@ -332,7 +337,14 @@ public void init(FilterConfig config) throws ServletException configurationKey = NativeWebSocketConfiguration.class.getName(); } - if (configuration == null) + if (durableConfiguration) + { + Objects.requireNonNull(this.configuration); + // We have a NativeWebSocketConfiguration already present, make sure it exists on the ServletContext + config.getServletContext().setAttribute(configurationKey, this.configuration); + + } + else { this.configuration = (NativeWebSocketConfiguration)config.getServletContext().getAttribute(configurationKey); if (this.configuration == null) @@ -342,18 +354,10 @@ public void init(FilterConfig config) throws ServletException NativeWebSocketConfiguration.class.getName() + " at ServletContext attribute '" + configurationKey + "'"); } } - else - { - // We have a NativeWebSocketConfiguration already present, make sure it exists on the ServletContext - if (config.getServletContext().getAttribute(configurationKey) == null) - { - config.getServletContext().setAttribute(configurationKey, this.configuration); - } - } if (!this.configuration.isRunning()) { - localConfiguration = true; + managedConfiguration = true; this.configuration.start(); }