Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify the usage of WebSocketUpgradeFilter in jetty 10 #5413

Merged
merged 6 commits into from
Nov 6, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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;
Expand Down Expand Up @@ -221,6 +222,39 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}

/**
* 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;
}

lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void dump(Appendable out, String indent) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
lachlan-roberts marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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)
{
Expand All @@ -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;

Expand Down Expand Up @@ -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.ensureWebSocketComponents(context));
if (mappingKey == null)
mappingKey = WebSocketMapping.DEFAULT_KEY;
mapping = WebSocketMapping.ensureMapping(context, mappingKey);

String max = config.getInitParameter("idleTimeout");
if (max == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.ensureWebSocketComponents(servletContext));
Expand Down