Use Filter name to identify the WebSocketUpgradeFilter.

Don't allow configuration of WebSocketMapping attribute.
The WebSocketUpgradeFilter is identified by it's name, which must be set as the fully qualified class name.

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-11-13 15:44:18 +11:00
parent 6934b94261
commit 0493a11106
6 changed files with 15 additions and 44 deletions

View File

@ -153,7 +153,7 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
{
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServer(), context.getServletContext());
FilterHolder filterHolder = WebSocketUpgradeFilter.ensureFilter(context.getServletContext());
WebSocketMapping mapping = WebSocketMapping.ensureMapping(context.getServletContext(), WebSocketMapping.DEFAULT_KEY);
WebSocketMapping mapping = WebSocketMapping.ensureMapping(context.getServletContext());
serverContainer = JavaxWebSocketServerContainer.ensureContainer(context.getServletContext());
if (LOG.isDebugEnabled())

View File

@ -98,7 +98,7 @@ public class JavaxWebSocketServerContainer extends JavaxWebSocketClientContainer
// Create the Jetty ServerContainer implementation
container = new JavaxWebSocketServerContainer(
WebSocketMapping.ensureMapping(servletContext, WebSocketMapping.DEFAULT_KEY),
WebSocketMapping.ensureMapping(servletContext),
WebSocketServerComponents.getWebSocketComponents(servletContext),
coreClientSupplier);
contextHandler.addManaged(container);

View File

@ -77,7 +77,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements
// Create the Jetty ServerContainer implementation
container = new JettyWebSocketServerContainer(
contextHandler,
WebSocketMapping.ensureMapping(servletContext, WebSocketMapping.DEFAULT_KEY),
WebSocketMapping.ensureMapping(servletContext),
WebSocketServerComponents.getWebSocketComponents(servletContext), executor);
servletContext.setAttribute(JETTY_WEBSOCKET_CONTAINER_ATTRIBUTE, container);
contextHandler.addManaged(container);

View File

@ -93,7 +93,7 @@ public class JettyWebSocketFilterTest
// After mapping is added we have an UpgradeFilter.
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
assertNotNull(filterHolder);
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
@ -127,7 +127,7 @@ public class JettyWebSocketFilterTest
// After mapping is added we have an UpgradeFilter.
container.addMapping("/", EchoSocket.class);
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
assertNotNull(filterHolder);
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
@ -164,7 +164,7 @@ public class JettyWebSocketFilterTest
// After mapping is added we have an UpgradeFilter.
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
assertNotNull(filterHolder);
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));

View File

@ -77,11 +77,6 @@ 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}.
*
@ -92,12 +87,7 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
{
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;
return servletHandler.getFilter(WebSocketUpgradeFilter.class.getName());
}
/**
@ -121,11 +111,9 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
if (existingFilter != null)
return existingFilter;
final String name = "WebSocketUpgradeFilter";
final String pathSpec = "/*";
FilterHolder holder = new FilterHolder(new WebSocketUpgradeFilter());
holder.setName(name);
holder.setInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM, WebSocketMapping.DEFAULT_KEY);
holder.setName(WebSocketUpgradeFilter.class.getName());
holder.setAsyncSupported(true);
ContextHandler contextHandler = Objects.requireNonNull(ContextHandler.getContextHandler(servletContext));
@ -174,12 +162,7 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
@Override
public void init(FilterConfig config) throws ServletException
{
final ServletContext context = config.getServletContext();
String mappingKey = config.getInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM);
if (mappingKey == null)
throw new ServletException("the WebSocketMapping init param must be set");
mapping = WebSocketMapping.ensureMapping(context, mappingKey);
mapping = WebSocketMapping.ensureMapping(config.getServletContext());
String max = config.getInitParameter("idleTimeout");
if (max == null)

View File

@ -59,21 +59,11 @@ import org.slf4j.LoggerFactory;
public class WebSocketMapping implements Dumpable, LifeCycle.Listener
{
private static final Logger LOG = LoggerFactory.getLogger(WebSocketMapping.class);
public static final String WEBSOCKET_MAPPING_ATTRIBUTE = WebSocketMapping.class.getName();
public static WebSocketMapping getMapping(ServletContext servletContext, String mappingKey)
public static WebSocketMapping getMapping(ServletContext servletContext)
{
Object mappingObject = servletContext.getAttribute(mappingKey);
if (mappingObject != null)
{
if (mappingObject instanceof WebSocketMapping)
return (WebSocketMapping)mappingObject;
else
throw new IllegalStateException(
String.format("ContextHandler attribute %s is not of type WebSocketMapping: {%s}",
mappingKey, mappingObject.toString()));
}
return null;
return (WebSocketMapping)servletContext.getAttribute(WEBSOCKET_MAPPING_ATTRIBUTE);
}
public WebSocketCreator getMapping(PathSpec pathSpec)
@ -82,13 +72,13 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener
return cn == null ? null : cn.getWebSocketCreator();
}
public static WebSocketMapping ensureMapping(ServletContext servletContext, String mappingKey)
public static WebSocketMapping ensureMapping(ServletContext servletContext)
{
WebSocketMapping mapping = getMapping(servletContext, mappingKey);
WebSocketMapping mapping = getMapping(servletContext);
if (mapping == null)
{
mapping = new WebSocketMapping(WebSocketServerComponents.getWebSocketComponents(servletContext));
servletContext.setAttribute(mappingKey, mapping);
servletContext.setAttribute(WEBSOCKET_MAPPING_ATTRIBUTE, mapping);
}
return mapping;
@ -133,8 +123,6 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener
throw new IllegalArgumentException("Unrecognized path spec syntax [" + rawSpec + "]");
}
public static final String DEFAULT_KEY = "jetty.websocket.defaultMapping";
private final PathMappings<Negotiator> mappings = new PathMappings<>();
private final WebSocketComponents components;
private final Handshaker handshaker = Handshaker.newInstance();