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 @@ -91,6 +91,7 @@ public static JettyWebSocketServerContainer ensureContainer(ServletContext servl

private final ServletContextHandler contextHandler;
private final WebSocketMapping webSocketMapping;
private final WebSocketComponents components;
private final FrameHandlerFactory frameHandlerFactory;
private final Executor executor;
private final Configuration.ConfigurationCustomizer customizer = new Configuration.ConfigurationCustomizer();
Expand All @@ -102,14 +103,15 @@ public static JettyWebSocketServerContainer ensureContainer(ServletContext servl
* Main entry point for {@link JettyWebSocketServletContainerInitializer}.
*
* @param webSocketMapping the {@link WebSocketMapping} that this container belongs to
* @param webSocketComponents the {@link WebSocketComponents} instance to use
* @param components the {@link WebSocketComponents} instance to use
* @param executor the {@link Executor} to use
*/
JettyWebSocketServerContainer(ServletContextHandler contextHandler, WebSocketMapping webSocketMapping, WebSocketComponents webSocketComponents, Executor executor)
JettyWebSocketServerContainer(ServletContextHandler contextHandler, WebSocketMapping webSocketMapping, WebSocketComponents components, Executor executor)
{
this.contextHandler = contextHandler;
this.webSocketMapping = webSocketMapping;
this.executor = executor;
this.components = components;

// Ensure there is a FrameHandlerFactory
JettyServerFrameHandlerFactory factory = contextHandler.getBean(JettyServerFrameHandlerFactory.class);
Expand Down Expand Up @@ -155,6 +157,11 @@ public void addMapping(String pathSpec, final Class<?> endpointClass)
});
}

public WebSocketComponents getWebSocketComponents()
{
return components;
}

@Override
public Executor getExecutor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ public static void configure(ServletContextHandler context, Configurator configu
*/
private static JettyWebSocketServerContainer initialize(ServletContextHandler context)
{
WebSocketComponents components = WebSocketServerComponents.ensureWebSocketComponents(context.getServletContext());
WebSocketMapping mapping = WebSocketMapping.ensureMapping(context.getServletContext(), WebSocketMapping.DEFAULT_KEY);
JettyWebSocketServerContainer container = JettyWebSocketServerContainer.ensureContainer(context.getServletContext());

if (LOG.isDebugEnabled())
LOG.debug("configureContext {} {} {}", container, mapping, components);
LOG.debug("initialize {}", container);

return container;
}
Expand All @@ -102,6 +99,8 @@ private static JettyWebSocketServerContainer initialize(ServletContextHandler co
public void onStartup(Set<Class<?>> c, ServletContext context)
{
ServletContextHandler contextHandler = ServletContextHandler.getServletContextHandler(context, "Jetty WebSocket SCI");
JettyWebSocketServletContainerInitializer.initialize(contextHandler);
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.initialize(contextHandler);
if (LOG.isDebugEnabled())
LOG.debug("onStartup {}", container);
}
}
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 = "jetty.websocket.WebSocketMapping";

/**
* 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)
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)
throw new ServletException("the WebSocketMapping init param must be set");
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 Expand Up @@ -135,7 +133,7 @@ else if (rawSpec.startsWith("uri-template|"))
throw new IllegalArgumentException("Unrecognized path spec syntax [" + rawSpec + "]");
}

public static final String DEFAULT_KEY = "org.eclipse.jetty.websocket.util.server.internal.WebSocketMapping";
public static final String DEFAULT_KEY = "jetty.websocket.defaultMapping";

private final PathMappings<Negotiator> mappings = new PathMappings<>();
private final WebSocketComponents components;
Expand Down