errorMatcher)
@@ -143,8 +140,6 @@ public void startServer() throws Exception
});
});
- context.addFilter(WebSocketUpgradeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
-
context.addServlet(new ServletHolder(new SimpleStatusServlet(404)), "/bogus");
context.addServlet(new ServletHolder(new SimpleStatusServlet(200)), "/a-okay");
context.addServlet(new ServletHolder(new InvalidUpgradeServlet()), "/invalid-upgrade/*");
diff --git a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java
index 3725abbb25bc..ee7832720846 100644
--- a/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java
+++ b/jetty-websocket/websocket-jetty-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/WebSocketClientTest.java
@@ -25,12 +25,10 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
-import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
-import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
@@ -50,7 +48,6 @@
import org.eclipse.jetty.websocket.tests.EchoSocket;
import org.eclipse.jetty.websocket.tests.ParamsEndpoint;
import org.eclipse.jetty.websocket.tests.util.FutureWriteCallback;
-import org.eclipse.jetty.websocket.util.server.WebSocketUpgradeFilter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -104,10 +101,7 @@ public void startServer() throws Exception
configuration.addMapping("/get-params", (req, resp) -> new ParamsEndpoint());
});
- context.addFilter(WebSocketUpgradeFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
-
server.setHandler(context);
-
server.start();
}
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 d3cf9e1391b6..176544be380a 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.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 = "jetty.websocket.WebSocketMapping";
+
+ /**
+ * 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)
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 static FilterHolder ensureFilter(ServletContext servletContext)
}
}
- 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 void init(FilterConfig config) throws ServletException
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.getWebSocketComponents(context));
+ if (mappingKey == null)
+ throw new ServletException("the WebSocketMapping init param must be set");
+ 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 5bef84d466f7..e93c0c4c0192 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 WebSocketCreator getMapping(PathSpec pathSpec)
public static WebSocketMapping ensureMapping(ServletContext servletContext, String mappingKey)
{
WebSocketMapping mapping = getMapping(servletContext, mappingKey);
-
if (mapping == null)
{
mapping = new WebSocketMapping(WebSocketServerComponents.getWebSocketComponents(servletContext));
@@ -135,7 +133,7 @@ else if (rawSpec.startsWith("uri-template|"))
throw new IllegalArgumentException("Unrecognized path spec syntax [" + rawSpec + "]");
}
- public static final String DEFAULT_KEY = "org.eclipse.jetty.websocket.util.server.internal.WebSocketMapping";
+ public static final String DEFAULT_KEY = "jetty.websocket.defaultMapping";
private final PathMappings mappings = new PathMappings<>();
private final WebSocketComponents components;