445374 - Reevaluate org.eclipse.jetty.websocket.jsr356 enablement concepts

+ Making key also work inside of WEB-INF/web.xml via context params
+ Making WebSocketUpgradeFilter generic enough to be used in a
  web.xml descriptor
+ Adding global={bool} init-param on WebSocketUpgradeFilter to aid
  library developers and end users more ways to tweak the filter
  order
This commit is contained in:
Joakim Erdfelt 2014-09-30 07:40:07 -07:00
parent ab58438600
commit 6500931f8c
2 changed files with 73 additions and 34 deletions

View File

@ -84,49 +84,71 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
return jettyContainer;
}
@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException
private boolean isEnabled(Set<Class<?>> c, ServletContext context)
{
Object enable = context.getAttribute(ENABLE_KEY);
// Disable if explicitly disabled
if (TypeUtil.isFalse(enable))
// Try context parameters first
String cp = context.getInitParameter(ENABLE_KEY);
if(TypeUtil.isTrue(cp))
{
if (c.isEmpty())
{
if (LOG.isDebugEnabled())
{
LOG.debug("JSR-356 support disabled via attribute on context {} - {}",context.getContextPath(),context);
}
}
else
{
LOG.warn("JSR-356 support disabled via attribute on context {} - {}",context.getContextPath(),context);
}
return;
// forced on
return true;
}
// Disabled if not explicitly enabled and there are no discovered annotations or interfaces
if (!TypeUtil.isTrue(enable) && c.isEmpty())
if(TypeUtil.isFalse(cp))
{
// forced off
LOG.warn("JSR-356 support disabled via parameter on context {} - {}",context.getContextPath(),context);
return false;
}
// Next, try attribute on context
Object enable = context.getAttribute(ENABLE_KEY);
if(TypeUtil.isTrue(enable))
{
// forced on
return true;
}
if (TypeUtil.isFalse(enable))
{
// forced off
LOG.warn("JSR-356 support disabled via attribute on context {} - {}",context.getContextPath(),context);
return false;
}
// if not forced on or off, determine behavior based on annotations.
if (c.isEmpty())
{
if (LOG.isDebugEnabled())
{
LOG.debug("No JSR-356 annotations or interfaces discovered. JSR-356 support disabled",context.getContextPath(),context);
}
return false;
}
return true;
}
@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException
{
if(!isEnabled(c,context))
{
return;
}
ContextHandler handler = ContextHandler.getContextHandler(context);
if (handler == null)
{
throw new ServletException("Not running on Jetty, JSR-356 support disabled");
throw new ServletException("Not running on Jetty, JSR-356 support unavailable");
}
if (!(handler instanceof ServletContextHandler))
{
throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support disabled");
throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support unavailable");
}
ServletContextHandler jettyContext = (ServletContextHandler)handler;

View File

@ -43,7 +43,6 @@ import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.server.pathmap.PathMappings;
import org.eclipse.jetty.websocket.server.pathmap.PathMappings.MappedResource;
@ -60,8 +59,8 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
public static WebSocketUpgradeFilter configureContext(ServletContextHandler context)
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter(policy);
// Dynamically add filter
WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter();
String name = "Jetty_WebSocketUpgradeFilter";
String pathSpec = "/*";
@ -69,6 +68,7 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
FilterHolder fholder = new FilterHolder(filter);
fholder.setName(name);
fholder.setInitParameter("global","true");
context.addFilter(fholder,pathSpec,dispatcherTypes);
if (LOG.isDebugEnabled())
@ -84,8 +84,7 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
public static WebSocketUpgradeFilter configureContext(ServletContext context)
{
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter(policy);
WebSocketUpgradeFilter filter = new WebSocketUpgradeFilter();
String name = "Jetty_Dynamic_WebSocketUpgradeFilter";
String pathSpec = "/*";
@ -94,6 +93,7 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
String urlPatterns[] = { pathSpec };
FilterRegistration.Dynamic dyn = context.addFilter(name,filter);
dyn.setInitParameter("global","true");
dyn.addMappingForUrlPatterns(dispatcherTypes,isMatchAfter,urlPatterns);
if (LOG.isDebugEnabled())
@ -101,18 +101,16 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
LOG.debug("Adding [{}] {} mapped to {} to {}",name,filter,pathSpec,context);
}
// Store reference to the WebSocketUpgradeFilter
context.setAttribute(WebSocketUpgradeFilter.class.getName(),filter);
return filter;
}
private final WebSocketServerFactory factory;
private String fname;
private final PathMappings<WebSocketCreator> pathmap = new PathMappings<>();
public WebSocketUpgradeFilter(WebSocketPolicy policy)
public WebSocketUpgradeFilter()
{
this(policy, new MappedByteBufferPool());
this(WebSocketPolicy.newServerPolicy(), new MappedByteBufferPool());
}
public WebSocketUpgradeFilter(WebSocketPolicy policy, ByteBufferPool bufferPool)
@ -145,6 +143,11 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
chain.doFilter(request,response);
return;
}
if (LOG.isDebugEnabled())
{
LOG.debug(".doFilter({}) - {}",fname,chain);
}
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse))
{
@ -232,6 +235,8 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
@Override
public void init(FilterConfig config) throws ServletException
{
fname = config.getFilterName();
try
{
WebSocketPolicy policy = factory.getPolicy();
@ -259,6 +264,18 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter
{
policy.setInputBufferSize(Integer.parseInt(max));
}
boolean addGlobalAttr = false;
String boolStr = config.getInitParameter("global");
if (boolStr != null)
{
addGlobalAttr = Boolean.parseBoolean(boolStr);
}
if (addGlobalAttr)
{
config.getServletContext().setAttribute(WebSocketUpgradeFilter.class.getName(),this);
}
factory.start();
}