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:
parent
6934b94261
commit
0493a11106
|
@ -153,7 +153,7 @@ public class JavaxWebSocketServletContainerInitializer implements ServletContain
|
||||||
{
|
{
|
||||||
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServer(), context.getServletContext());
|
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServer(), context.getServletContext());
|
||||||
FilterHolder filterHolder = WebSocketUpgradeFilter.ensureFilter(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());
|
serverContainer = JavaxWebSocketServerContainer.ensureContainer(context.getServletContext());
|
||||||
|
|
||||||
if (LOG.isDebugEnabled())
|
if (LOG.isDebugEnabled())
|
||||||
|
|
|
@ -98,7 +98,7 @@ public class JavaxWebSocketServerContainer extends JavaxWebSocketClientContainer
|
||||||
|
|
||||||
// Create the Jetty ServerContainer implementation
|
// Create the Jetty ServerContainer implementation
|
||||||
container = new JavaxWebSocketServerContainer(
|
container = new JavaxWebSocketServerContainer(
|
||||||
WebSocketMapping.ensureMapping(servletContext, WebSocketMapping.DEFAULT_KEY),
|
WebSocketMapping.ensureMapping(servletContext),
|
||||||
WebSocketServerComponents.getWebSocketComponents(servletContext),
|
WebSocketServerComponents.getWebSocketComponents(servletContext),
|
||||||
coreClientSupplier);
|
coreClientSupplier);
|
||||||
contextHandler.addManaged(container);
|
contextHandler.addManaged(container);
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements
|
||||||
// Create the Jetty ServerContainer implementation
|
// Create the Jetty ServerContainer implementation
|
||||||
container = new JettyWebSocketServerContainer(
|
container = new JettyWebSocketServerContainer(
|
||||||
contextHandler,
|
contextHandler,
|
||||||
WebSocketMapping.ensureMapping(servletContext, WebSocketMapping.DEFAULT_KEY),
|
WebSocketMapping.ensureMapping(servletContext),
|
||||||
WebSocketServerComponents.getWebSocketComponents(servletContext), executor);
|
WebSocketServerComponents.getWebSocketComponents(servletContext), executor);
|
||||||
servletContext.setAttribute(JETTY_WEBSOCKET_CONTAINER_ATTRIBUTE, container);
|
servletContext.setAttribute(JETTY_WEBSOCKET_CONTAINER_ATTRIBUTE, container);
|
||||||
contextHandler.addManaged(container);
|
contextHandler.addManaged(container);
|
||||||
|
|
|
@ -93,7 +93,7 @@ public class JettyWebSocketFilterTest
|
||||||
|
|
||||||
// After mapping is added we have an UpgradeFilter.
|
// After mapping is added we have an UpgradeFilter.
|
||||||
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
||||||
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
|
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
|
||||||
assertNotNull(filterHolder);
|
assertNotNull(filterHolder);
|
||||||
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
||||||
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
||||||
|
@ -127,7 +127,7 @@ public class JettyWebSocketFilterTest
|
||||||
// After mapping is added we have an UpgradeFilter.
|
// After mapping is added we have an UpgradeFilter.
|
||||||
container.addMapping("/", EchoSocket.class);
|
container.addMapping("/", EchoSocket.class);
|
||||||
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
||||||
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
|
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
|
||||||
assertNotNull(filterHolder);
|
assertNotNull(filterHolder);
|
||||||
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
||||||
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
||||||
|
@ -164,7 +164,7 @@ public class JettyWebSocketFilterTest
|
||||||
|
|
||||||
// After mapping is added we have an UpgradeFilter.
|
// After mapping is added we have an UpgradeFilter.
|
||||||
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
assertThat(contextHandler.getServletHandler().getFilters().length, is(1));
|
||||||
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter("WebSocketUpgradeFilter");
|
FilterHolder filterHolder = contextHandler.getServletHandler().getFilter(WebSocketUpgradeFilter.class.getName());
|
||||||
assertNotNull(filterHolder);
|
assertNotNull(filterHolder);
|
||||||
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
assertThat(filterHolder.getState(), is(AbstractLifeCycle.STARTED));
|
||||||
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
assertThat(filterHolder.getFilter(), instanceOf(WebSocketUpgradeFilter.class));
|
||||||
|
|
|
@ -77,11 +77,6 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(WebSocketUpgradeFilter.class);
|
private static final Logger LOG = LoggerFactory.getLogger(WebSocketUpgradeFilter.class);
|
||||||
private static final AutoLock LOCK = new AutoLock();
|
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}.
|
* 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));
|
ContextHandler contextHandler = Objects.requireNonNull(ContextHandler.getContextHandler(servletContext));
|
||||||
ServletHandler servletHandler = contextHandler.getChildHandlerByClass(ServletHandler.class);
|
ServletHandler servletHandler = contextHandler.getChildHandlerByClass(ServletHandler.class);
|
||||||
for (FilterHolder holder : servletHandler.getFilters())
|
return servletHandler.getFilter(WebSocketUpgradeFilter.class.getName());
|
||||||
{
|
|
||||||
if (holder.getInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM) != null)
|
|
||||||
return holder;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -121,11 +111,9 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
|
||||||
if (existingFilter != null)
|
if (existingFilter != null)
|
||||||
return existingFilter;
|
return existingFilter;
|
||||||
|
|
||||||
final String name = "WebSocketUpgradeFilter";
|
|
||||||
final String pathSpec = "/*";
|
final String pathSpec = "/*";
|
||||||
FilterHolder holder = new FilterHolder(new WebSocketUpgradeFilter());
|
FilterHolder holder = new FilterHolder(new WebSocketUpgradeFilter());
|
||||||
holder.setName(name);
|
holder.setName(WebSocketUpgradeFilter.class.getName());
|
||||||
holder.setInitParameter(MAPPING_ATTRIBUTE_INIT_PARAM, WebSocketMapping.DEFAULT_KEY);
|
|
||||||
|
|
||||||
holder.setAsyncSupported(true);
|
holder.setAsyncSupported(true);
|
||||||
ContextHandler contextHandler = Objects.requireNonNull(ContextHandler.getContextHandler(servletContext));
|
ContextHandler contextHandler = Objects.requireNonNull(ContextHandler.getContextHandler(servletContext));
|
||||||
|
@ -174,12 +162,7 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable
|
||||||
@Override
|
@Override
|
||||||
public void init(FilterConfig config) throws ServletException
|
public void init(FilterConfig config) throws ServletException
|
||||||
{
|
{
|
||||||
final ServletContext context = config.getServletContext();
|
mapping = WebSocketMapping.ensureMapping(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);
|
|
||||||
|
|
||||||
String max = config.getInitParameter("idleTimeout");
|
String max = config.getInitParameter("idleTimeout");
|
||||||
if (max == null)
|
if (max == null)
|
||||||
|
|
|
@ -59,21 +59,11 @@ import org.slf4j.LoggerFactory;
|
||||||
public class WebSocketMapping implements Dumpable, LifeCycle.Listener
|
public class WebSocketMapping implements Dumpable, LifeCycle.Listener
|
||||||
{
|
{
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(WebSocketMapping.class);
|
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);
|
return (WebSocketMapping)servletContext.getAttribute(WEBSOCKET_MAPPING_ATTRIBUTE);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WebSocketCreator getMapping(PathSpec pathSpec)
|
public WebSocketCreator getMapping(PathSpec pathSpec)
|
||||||
|
@ -82,13 +72,13 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener
|
||||||
return cn == null ? null : cn.getWebSocketCreator();
|
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)
|
if (mapping == null)
|
||||||
{
|
{
|
||||||
mapping = new WebSocketMapping(WebSocketServerComponents.getWebSocketComponents(servletContext));
|
mapping = new WebSocketMapping(WebSocketServerComponents.getWebSocketComponents(servletContext));
|
||||||
servletContext.setAttribute(mappingKey, mapping);
|
servletContext.setAttribute(WEBSOCKET_MAPPING_ATTRIBUTE, mapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapping;
|
return mapping;
|
||||||
|
@ -133,8 +123,6 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener
|
||||||
throw new IllegalArgumentException("Unrecognized path spec syntax [" + rawSpec + "]");
|
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 PathMappings<Negotiator> mappings = new PathMappings<>();
|
||||||
private final WebSocketComponents components;
|
private final WebSocketComponents components;
|
||||||
private final Handshaker handshaker = Handshaker.newInstance();
|
private final Handshaker handshaker = Handshaker.newInstance();
|
||||||
|
|
Loading…
Reference in New Issue