simplify the usage of WebSocketUpgradeFilter
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
d2beb2861d
commit
94bafba6ac
|
@ -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<Filter>
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<? extends Filter> getFilterClass()
|
||||
{
|
||||
if (_filter != null)
|
||||
return _filter.getClass();
|
||||
|
||||
Filter filter = getInstance();
|
||||
if (filter != null)
|
||||
return filter.getClass();
|
||||
|
||||
Class<? extends Filter> 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
|
||||
{
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
* <p>
|
||||
* 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.
|
||||
* </p>
|
||||
* <p>
|
||||
* The default {@link WebSocketUpgradeFilter} is also available via
|
||||
* the {@link ServletContext} attribute named {@code org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter}
|
||||
* </p>
|
||||
*
|
||||
* @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)
|
||||
if (mappingKey == null)
|
||||
mappingKey = WebSocketMapping.DEFAULT_KEY;
|
||||
mapping = WebSocketMapping.ensureMapping(context, mappingKey);
|
||||
else
|
||||
mapping = new WebSocketMapping(WebSocketServerComponents.ensureWebSocketComponents(context));
|
||||
|
||||
String max = config.getInitParameter("idleTimeout");
|
||||
if (max == null)
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue