From 94bafba6acda7bd31e35f8188c98d644dd01e1cf Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 9 Oct 2020 17:31:41 +1100 Subject: [PATCH] simplify the usage of WebSocketUpgradeFilter Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/servlet/FilterHolder.java | 34 ++++++++++++++++++ .../util/server/WebSocketUpgradeFilter.java | 36 ++++++++++--------- .../server/internal/WebSocketMapping.java | 2 -- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java index 579ae8dddb2..14dc3c94b74 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/FilterHolder.java @@ -36,6 +36,7 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.util.Loader; import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.DumpableCollection; @@ -221,6 +222,39 @@ public class FilterHolder extends Holder } } + /** + * Work out the class of the held {@link Filter} even before the {@link FilterHolder} has been started. + * @return the class of the held {@link Filter}, or null if not available. + */ + @SuppressWarnings("unchecked") + public Class getFilterClass() + { + if (_filter != null) + return _filter.getClass(); + + Filter filter = getInstance(); + if (filter != null) + return filter.getClass(); + + Class heldClass = getHeldClass(); + if (heldClass != null) + return heldClass; + + String className = getClassName(); + if (className != null) + { + try + { + return Loader.loadClass(className); + } + catch (ClassNotFoundException e) + { + LOG.warn("Could not load filter class", e); + } + } + return null; + } + @Override public void dump(Appendable out, String indent) throws IOException { diff --git a/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/WebSocketUpgradeFilter.java index d756b0bcc18..a89d7e67a03 100644 --- a/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/WebSocketUpgradeFilter.java +++ b/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/WebSocketUpgradeFilter.java @@ -40,7 +40,6 @@ import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.websocket.core.Configuration; -import org.eclipse.jetty.websocket.core.server.WebSocketServerComponents; import org.eclipse.jetty.websocket.util.server.internal.WebSocketMapping; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,34 +77,40 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable private static final Logger LOG = LoggerFactory.getLogger(WebSocketUpgradeFilter.class); private static final AutoLock LOCK = new AutoLock(); + /** + * The init parameter name used to define {@link ServletContext} attribute used to share the {@link WebSocketMapping}. + */ + public static final String MAPPING_ATTRIBUTE_INIT_PARAM = "org.eclipse.jetty.websocket.util.server.internal.WebSocketMapping.key"; + + /** + * Return any {@link WebSocketUpgradeFilter} already present on the {@link ServletContext}. + * + * @param servletContext the {@link ServletContext} to use. + * @return the configured default {@link WebSocketUpgradeFilter} instance. + */ private static FilterHolder getFilter(ServletContext servletContext) { ContextHandler contextHandler = Objects.requireNonNull(ContextHandler.getContextHandler(servletContext)); ServletHandler servletHandler = contextHandler.getChildHandlerByClass(ServletHandler.class); - for (FilterHolder holder : servletHandler.getFilters()) { - if (holder.getInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM) != null) + if (WebSocketUpgradeFilter.class.isAssignableFrom(holder.getFilterClass())) return holder; } - return null; } /** - * Configure the default WebSocketUpgradeFilter. - * - *

- * This will return the default {@link WebSocketUpgradeFilter} on the - * provided {@link ServletContext}, creating the filter if necessary. + * Ensure a {@link WebSocketUpgradeFilter} is available on the provided {@link ServletContext}, + * a new filter will added if one does not already exist. *

*

* The default {@link WebSocketUpgradeFilter} is also available via * the {@link ServletContext} attribute named {@code org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter} *

* - * @param servletContext the {@link ServletContext} to use - * @return the configured default {@link WebSocketUpgradeFilter} instance + * @param servletContext the {@link ServletContext} to use. + * @return the configured default {@link WebSocketUpgradeFilter} instance. */ public static FilterHolder ensureFilter(ServletContext servletContext) { @@ -132,8 +137,6 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable } } - public static final String MAPPING_ATTRIBUTE_INIT_PARAM = "org.eclipse.jetty.websocket.util.server.internal.WebSocketMapping.key"; - private final Configuration.ConfigurationCustomizer defaultCustomizer = new Configuration.ConfigurationCustomizer(); private WebSocketMapping mapping; @@ -174,10 +177,9 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable final ServletContext context = config.getServletContext(); String mappingKey = config.getInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM); - if (mappingKey != null) - mapping = WebSocketMapping.ensureMapping(context, mappingKey); - else - mapping = new WebSocketMapping(WebSocketServerComponents.ensureWebSocketComponents(context)); + if (mappingKey == null) + mappingKey = WebSocketMapping.DEFAULT_KEY; + mapping = WebSocketMapping.ensureMapping(context, mappingKey); String max = config.getInitParameter("idleTimeout"); if (max == null) diff --git a/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/internal/WebSocketMapping.java b/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/internal/WebSocketMapping.java index 49283dbf8e1..40d58edd2ec 100644 --- a/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/internal/WebSocketMapping.java +++ b/jetty-websocket/websocket-util-server/src/main/java/org/eclipse/jetty/websocket/util/server/internal/WebSocketMapping.java @@ -63,7 +63,6 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener public static WebSocketMapping getMapping(ServletContext servletContext, String mappingKey) { Object mappingObject = servletContext.getAttribute(mappingKey); - if (mappingObject != null) { if (mappingObject instanceof WebSocketMapping) @@ -86,7 +85,6 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener public static WebSocketMapping ensureMapping(ServletContext servletContext, String mappingKey) { WebSocketMapping mapping = getMapping(servletContext, mappingKey); - if (mapping == null) { mapping = new WebSocketMapping(WebSocketServerComponents.ensureWebSocketComponents(servletContext));