mirror of
https://github.com/jetty/jetty.project.git
synced 2025-03-03 20:39:18 +00:00
Issue #3698 - Applying changes from PR feedback.
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
a6b2bd86ef
commit
986dd77e60
@ -29,7 +29,6 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterRegistration;
|
||||
@ -61,7 +60,6 @@ import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.HandlerContainer;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.server.handler.ErrorHandler;
|
||||
import org.eclipse.jetty.server.handler.HandlerCollection;
|
||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
|
||||
import org.eclipse.jetty.server.session.SessionHandler;
|
||||
@ -780,6 +778,16 @@ public class ServletContextHandler extends ContextHandler
|
||||
_objFactory.destroy(filter);
|
||||
}
|
||||
|
||||
public static ServletContextHandler getServletContextHandler(ServletContext context)
|
||||
{
|
||||
ContextHandler handler = getContextHandler(context);
|
||||
if (handler == null)
|
||||
return null;
|
||||
if (handler instanceof ServletContextHandler)
|
||||
return (ServletContextHandler) handler;
|
||||
return null;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public static class JspPropertyGroup implements JspPropertyGroupDescriptor
|
||||
{
|
||||
|
@ -34,7 +34,6 @@ import javax.websocket.server.ServerEndpointConfig;
|
||||
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
|
||||
import org.eclipse.jetty.util.TypeUtil;
|
||||
@ -50,11 +49,16 @@ import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter;
|
||||
{ ServerApplicationConfig.class, ServerEndpoint.class, Endpoint.class })
|
||||
public class WebSocketServerContainerInitializer implements ServletContainerInitializer
|
||||
{
|
||||
/**
|
||||
* The ServletContext attribute key name for the
|
||||
* ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
|
||||
*/
|
||||
public static final String ATTR_JAVAX_SERVER_CONTAINER = javax.websocket.server.ServerContainer.class.getName();
|
||||
|
||||
public static final String ENABLE_KEY = "org.eclipse.jetty.websocket.jsr356";
|
||||
public static final String ADD_DYNAMIC_FILTER_KEY = "org.eclipse.jetty.websocket.jsr356.addDynamicFilter";
|
||||
private static final Logger LOG = Log.getLogger(WebSocketServerContainerInitializer.class);
|
||||
public static final String HTTPCLIENT_ATTRIBUTE = "org.eclipse.jetty.websocket.jsr356.HttpClient";
|
||||
public static final String ATTR_JAVAX_SERVER_CONTAINER = javax.websocket.server.ServerContainer.class.getName();
|
||||
|
||||
/**
|
||||
* DestroyListener
|
||||
@ -70,10 +74,10 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
//remove any ServerContainer beans
|
||||
if (sce.getServletContext() instanceof ContextHandler.Context)
|
||||
// remove any ServerContainer beans
|
||||
ServletContextHandler handler = ServletContextHandler.getServletContextHandler(sce.getServletContext());
|
||||
if (handler != null)
|
||||
{
|
||||
ContextHandler handler = ((ContextHandler.Context)sce.getServletContext()).getContextHandler();
|
||||
ServerContainer bean = handler.getBean(ServerContainer.class);
|
||||
if (bean != null)
|
||||
handler.removeBean(bean);
|
||||
@ -142,33 +146,28 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
||||
* @return a configured {@link ServerContainer} instance
|
||||
* @throws ServletException if the {@link WebSocketUpgradeFilter} cannot be configured
|
||||
* @deprecated use {@link #configure(ServletContextHandler, Configurator)} instead
|
||||
* @see #initialize(ServletContext)
|
||||
*/
|
||||
@Deprecated
|
||||
public static ServerContainer configureContext(ServletContextHandler context) throws ServletException
|
||||
{
|
||||
ServletContext servletContext = context.getServletContext();
|
||||
initialize(servletContext);
|
||||
return (ServerContainer)servletContext.getAttribute(ATTR_JAVAX_SERVER_CONTAINER);
|
||||
return initialize(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context the {@link ServletContext} to use
|
||||
* @param jettyContext not used
|
||||
* @param context not used
|
||||
* @param jettyContext the {@link ServletContextHandler} to use
|
||||
* @return a configured {@link ServerContainer} instance
|
||||
* @throws ServletException if the {@link WebSocketUpgradeFilter} cannot be configured
|
||||
* @deprecated use {@link #configure(ServletContextHandler, Configurator)} instead
|
||||
* @see #initialize(ServletContext)
|
||||
*/
|
||||
@Deprecated
|
||||
public static ServerContainer configureContext(ServletContext context, ServletContextHandler jettyContext) throws ServletException
|
||||
{
|
||||
initialize(context);
|
||||
return (ServerContainer)context.getAttribute(ATTR_JAVAX_SERVER_CONTAINER);
|
||||
return initialize(jettyContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the {@link ServletContext} with the default (and empty) {@link ServerContainer}.
|
||||
* Immediately initialize the {@link ServletContext} with the default (and empty) {@link ServerContainer}.
|
||||
*
|
||||
* <p>
|
||||
* This performs a subset of the behaviors that {@link #onStartup(Set, ServletContext)} does.
|
||||
@ -178,65 +177,41 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
||||
* </p>
|
||||
*
|
||||
* @param context the context to work with
|
||||
* @return the default {@link ServerContainer} for this context
|
||||
*/
|
||||
public static void initialize(ServletContext context) throws ServletException
|
||||
public static ServerContainer initialize(ServletContextHandler context) throws ServletException
|
||||
{
|
||||
// Create Basic components
|
||||
NativeWebSocketServletContainerInitializer.initialize(context);
|
||||
NativeWebSocketConfiguration nativeWebSocketConfiguration = (NativeWebSocketConfiguration)context.getAttribute(NativeWebSocketServletContainerInitializer.ATTR_KEY);
|
||||
|
||||
ContextHandler contextHandler = null;
|
||||
// Attach default configuration to context lifecycle
|
||||
if (context instanceof ContextHandler.Context)
|
||||
ServerContainer serverContainer = (ServerContainer) context.getAttribute(ATTR_JAVAX_SERVER_CONTAINER);
|
||||
if(serverContainer == null)
|
||||
{
|
||||
contextHandler = ((ContextHandler.Context)context).getContextHandler();
|
||||
}
|
||||
// Create Basic components
|
||||
NativeWebSocketConfiguration nativeWebSocketConfiguration = NativeWebSocketServletContainerInitializer.initialize(context);
|
||||
|
||||
// Obtain HttpClient
|
||||
HttpClient httpClient = (HttpClient)context.getAttribute(HTTPCLIENT_ATTRIBUTE);
|
||||
if ((httpClient == null) && (contextHandler != null))
|
||||
{
|
||||
Server server = contextHandler.getServer();
|
||||
if (server != null)
|
||||
// Obtain HttpClient
|
||||
HttpClient httpClient = (HttpClient) context.getAttribute(HTTPCLIENT_ATTRIBUTE);
|
||||
if (httpClient == null)
|
||||
{
|
||||
httpClient = (HttpClient)server.getAttribute(HTTPCLIENT_ATTRIBUTE);
|
||||
}
|
||||
}
|
||||
|
||||
// Create the Jetty ServerContainer implementation
|
||||
ServerContainer jettyContainer = new ServerContainer(nativeWebSocketConfiguration, httpClient);
|
||||
contextHandler.addBean(jettyContainer);
|
||||
|
||||
// Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
|
||||
context.setAttribute(javax.websocket.server.ServerContainer.class.getName(),jettyContainer);
|
||||
|
||||
if(contextHandler instanceof ServletContextHandler)
|
||||
{
|
||||
ServletContextHandler servletContextHandler = (ServletContextHandler)contextHandler;
|
||||
// Create Filter
|
||||
if(isEnabledViaContext(context, ADD_DYNAMIC_FILTER_KEY, true))
|
||||
{
|
||||
String instanceKey = WebSocketUpgradeFilter.class.getName() + ".SCI";
|
||||
if(context.getAttribute(instanceKey) == null)
|
||||
Server server = context.getServer();
|
||||
if (server != null)
|
||||
{
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Dynamic filter add to support JSR356/javax.websocket.server: {}", WebSocketUpgradeFilter.class.getName());
|
||||
WebSocketUpgradeFilter wsuf = WebSocketUpgradeFilter.configureContext(servletContextHandler);
|
||||
context.setAttribute(instanceKey, wsuf);
|
||||
httpClient = (HttpClient) server.getAttribute(HTTPCLIENT_ATTRIBUTE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the {@link ServletContextHandler} to call {@link WebSocketServerContainerInitializer#onStartup(Set, ServletContext)}
|
||||
* during the {@link ServletContext} initialization phase.
|
||||
*
|
||||
* @param context the context to add listener to
|
||||
*/
|
||||
public static void configure(ServletContextHandler context)
|
||||
{
|
||||
context.addEventListener(ContainerInitializer.asContextListener(new WebSocketServerContainerInitializer()));
|
||||
// Create the Jetty ServerContainer implementation
|
||||
serverContainer = new ServerContainer(nativeWebSocketConfiguration, httpClient);
|
||||
context.addBean(serverContainer);
|
||||
|
||||
// Store a reference to the ServerContainer per javax.websocket spec 1.0 final section 6.4 Programmatic Server Deployment
|
||||
context.setAttribute(ATTR_JAVAX_SERVER_CONTAINER, serverContainer);
|
||||
|
||||
// Create Filter
|
||||
if (isEnabledViaContext(context.getServletContext(), ADD_DYNAMIC_FILTER_KEY, true))
|
||||
{
|
||||
WebSocketUpgradeFilter.initialize(context);
|
||||
}
|
||||
}
|
||||
return serverContainer;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -258,13 +233,16 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
||||
.afterStartup((servletContext) ->
|
||||
{
|
||||
ServerContainer serverContainer = (ServerContainer)servletContext.getAttribute(ATTR_JAVAX_SERVER_CONTAINER);
|
||||
try
|
||||
if (configurator != null)
|
||||
{
|
||||
configurator.accept(servletContext, serverContainer);
|
||||
}
|
||||
catch (DeploymentException e)
|
||||
{
|
||||
throw new RuntimeException("Failed to deploy WebSocket Endpoint", e);
|
||||
try
|
||||
{
|
||||
configurator.accept(servletContext, serverContainer);
|
||||
}
|
||||
catch (DeploymentException e)
|
||||
{
|
||||
throw new RuntimeException("Failed to deploy WebSocket Endpoint", e);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
@ -278,24 +256,17 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
||||
return;
|
||||
}
|
||||
|
||||
ContextHandler handler = ContextHandler.getContextHandler(context);
|
||||
ServletContextHandler handler = ServletContextHandler.getServletContextHandler(context);
|
||||
|
||||
if (handler == null)
|
||||
{
|
||||
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 unavailable");
|
||||
}
|
||||
|
||||
try(ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader()))
|
||||
{
|
||||
// Create the Jetty ServerContainer implementation
|
||||
initialize(context);
|
||||
ServerContainer jettyContainer = (ServerContainer)context.getAttribute(ATTR_JAVAX_SERVER_CONTAINER);
|
||||
|
||||
// Initialize the Jetty ServerContainer implementation
|
||||
ServerContainer jettyContainer = initialize(handler);
|
||||
context.addListener(new ContextDestroyListener()); // make sure context is cleaned up when the context stops
|
||||
|
||||
if (c.isEmpty())
|
||||
|
@ -0,0 +1,212 @@
|
||||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.jsr356.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.websocket.DeploymentException;
|
||||
import javax.websocket.OnMessage;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.server.handler.DefaultHandler;
|
||||
import org.eclipse.jetty.server.handler.HandlerList;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
|
||||
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
|
||||
import org.eclipse.jetty.websocket.api.util.WSURI;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class RestartContextTest
|
||||
{
|
||||
private Server server;
|
||||
private WebSocketClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void startClient() throws Exception
|
||||
{
|
||||
client = new WebSocketClient();
|
||||
client.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stopClient() throws Exception
|
||||
{
|
||||
client.stop();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void startServer() throws Exception
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartStopStart_ServletContextListener() throws Exception
|
||||
{
|
||||
server = new Server();
|
||||
|
||||
ServerConnector connector = new ServerConnector(server);
|
||||
connector.setPort(0);
|
||||
server.addConnector(connector);
|
||||
|
||||
// Setup Context
|
||||
ServletContextHandler context = new ServletContextHandler();
|
||||
context.setContextPath("/");
|
||||
WebSocketServerContainerInitializer.configure(context, null);
|
||||
// late initialization via my own ServletContextListener
|
||||
context.addEventListener(new AddEndpointListener());
|
||||
|
||||
// Setup handler tree
|
||||
HandlerList handlers = new HandlerList();
|
||||
handlers.addHandler(context);
|
||||
handlers.addHandler(new DefaultHandler());
|
||||
|
||||
// Add handler tree to server
|
||||
server.setHandler(handlers);
|
||||
|
||||
// Start server
|
||||
server.start();
|
||||
|
||||
// verify functionality
|
||||
verifyWebSocketEcho(server.getURI().resolve("/echo"));
|
||||
|
||||
// Stop server
|
||||
server.stop();
|
||||
|
||||
// Start server (again)
|
||||
server.start();
|
||||
|
||||
// verify functionality (again)
|
||||
verifyWebSocketEcho(server.getURI().resolve("/echo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartStopStart_Configurator() throws Exception
|
||||
{
|
||||
server = new Server();
|
||||
|
||||
ServerConnector connector = new ServerConnector(server);
|
||||
connector.setPort(0);
|
||||
server.addConnector(connector);
|
||||
|
||||
// Setup Context
|
||||
ServletContextHandler context = new ServletContextHandler();
|
||||
context.setContextPath("/");
|
||||
WebSocketServerContainerInitializer.configure(context, (servletContext, serverContainer) -> {
|
||||
// Add endpoint via configurator
|
||||
serverContainer.addEndpoint(EchoEndpoint.class);
|
||||
});
|
||||
|
||||
// Setup handler tree
|
||||
HandlerList handlers = new HandlerList();
|
||||
handlers.addHandler(context);
|
||||
handlers.addHandler(new DefaultHandler());
|
||||
|
||||
// Add handler tree to server
|
||||
server.setHandler(handlers);
|
||||
|
||||
// Start server
|
||||
server.start();
|
||||
|
||||
// verify functionality
|
||||
verifyWebSocketEcho(server.getURI().resolve("/echo"));
|
||||
|
||||
// Stop server
|
||||
server.stop();
|
||||
|
||||
// Start server (again)
|
||||
server.start();
|
||||
|
||||
// verify functionality (again)
|
||||
verifyWebSocketEcho(server.getURI().resolve("/echo"));
|
||||
}
|
||||
|
||||
private void verifyWebSocketEcho(URI endpointUri) throws URISyntaxException, IOException, ExecutionException, InterruptedException
|
||||
{
|
||||
ClientEndpoint endpoint = new ClientEndpoint();
|
||||
Future<Session> fut = client.connect(endpoint, WSURI.toWebsocket(endpointUri));
|
||||
try(Session session = fut.get())
|
||||
{
|
||||
session.getRemote().sendString("Test Echo");
|
||||
String msg = endpoint.messages.poll(5, TimeUnit.SECONDS);
|
||||
assertThat("msg", msg, is("Test Echo"));
|
||||
}
|
||||
}
|
||||
|
||||
public static class AddEndpointListener implements ServletContextListener
|
||||
{
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(javax.websocket.server.ServerContainer.class.getName());
|
||||
try
|
||||
{
|
||||
container.addEndpoint(EchoEndpoint.class);
|
||||
}
|
||||
catch (DeploymentException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@ServerEndpoint("/echo")
|
||||
public static class EchoEndpoint
|
||||
{
|
||||
@OnMessage
|
||||
public String onMessage(String msg)
|
||||
{
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
@WebSocket
|
||||
public static class ClientEndpoint
|
||||
{
|
||||
public LinkedBlockingQueue<String> messages = new LinkedBlockingQueue<>();
|
||||
|
||||
@OnWebSocketMessage
|
||||
public void onMessage(String msg)
|
||||
{
|
||||
this.messages.offer(msg);
|
||||
}
|
||||
}
|
||||
}
|
@ -22,7 +22,6 @@ import java.util.Set;
|
||||
import javax.servlet.ServletContainerInitializer;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.listener.ContainerInitializer;
|
||||
|
||||
@ -36,38 +35,29 @@ public class NativeWebSocketServletContainerInitializer implements ServletContai
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the ServletContext with the default (and empty) {@link NativeWebSocketConfiguration}
|
||||
* Immediately initialize the {@link ServletContextHandler} with the default {@link NativeWebSocketConfiguration}.
|
||||
*
|
||||
* <p>
|
||||
* This will return the default {@link NativeWebSocketConfiguration} if already initialized,
|
||||
* and not create a new {@link NativeWebSocketConfiguration} each time it is called.
|
||||
* </p>
|
||||
*
|
||||
* @param context the context to work with
|
||||
* @return the default {@link NativeWebSocketConfiguration}
|
||||
*/
|
||||
public static void initialize(ServletContext context)
|
||||
public static NativeWebSocketConfiguration initialize(ServletContextHandler context)
|
||||
{
|
||||
NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration)context.getAttribute(ATTR_KEY);
|
||||
if (configuration != null)
|
||||
return; // it exists.
|
||||
|
||||
// Not provided to us, create a new default one.
|
||||
configuration = new NativeWebSocketConfiguration(context);
|
||||
context.setAttribute(ATTR_KEY, configuration);
|
||||
|
||||
// Attach default configuration to context lifecycle
|
||||
if (context instanceof ContextHandler.Context)
|
||||
NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration) context.getAttribute(ATTR_KEY);
|
||||
if (configuration == null)
|
||||
{
|
||||
ContextHandler handler = ((ContextHandler.Context)context).getContextHandler();
|
||||
// Let ContextHandler handle configuration lifecycle
|
||||
handler.addManaged(configuration);
|
||||
}
|
||||
}
|
||||
// Not provided to us, create a new default one.
|
||||
configuration = new NativeWebSocketConfiguration(context.getServletContext());
|
||||
context.setAttribute(ATTR_KEY, configuration);
|
||||
|
||||
/**
|
||||
* Configure the {@link ServletContextHandler} to call the {@link NativeWebSocketServletContainerInitializer}
|
||||
* during the {@link ServletContext} initialization phase.
|
||||
*
|
||||
* @param context the context to add listener to.
|
||||
*/
|
||||
public static void configure(ServletContextHandler context)
|
||||
{
|
||||
context.addEventListener(ContainerInitializer.asContextListener(new NativeWebSocketServletContainerInitializer()));
|
||||
// Attach default configuration to context lifecycle
|
||||
context.addManaged(configuration);
|
||||
}
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,8 +75,11 @@ public class NativeWebSocketServletContainerInitializer implements ServletContai
|
||||
.asContextListener(new NativeWebSocketServletContainerInitializer())
|
||||
.afterStartup((servletContext) ->
|
||||
{
|
||||
NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration)servletContext.getAttribute(ATTR_KEY);
|
||||
configurator.accept(servletContext, configuration);
|
||||
if (configurator != null)
|
||||
{
|
||||
NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration) servletContext.getAttribute(ATTR_KEY);
|
||||
configurator.accept(servletContext, configuration);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@ -95,7 +88,6 @@ public class NativeWebSocketServletContainerInitializer implements ServletContai
|
||||
*
|
||||
* @param context the context to work with
|
||||
* @return the default {@link NativeWebSocketConfiguration}
|
||||
* @see #initialize(ServletContext)
|
||||
* @see #configure(ServletContextHandler)
|
||||
* @see #configure(ServletContextHandler, Configurator)
|
||||
* @deprecated use {@link #configure(ServletContextHandler, Configurator)} instead
|
||||
@ -103,14 +95,22 @@ public class NativeWebSocketServletContainerInitializer implements ServletContai
|
||||
@Deprecated
|
||||
public static NativeWebSocketConfiguration getDefaultFrom(ServletContext context)
|
||||
{
|
||||
initialize(context);
|
||||
return (NativeWebSocketConfiguration)context.getAttribute(ATTR_KEY);
|
||||
ServletContextHandler handler = ServletContextHandler.getServletContextHandler(context);
|
||||
if (handler == null)
|
||||
{
|
||||
throw new IllegalStateException("Unable to find ServletContextHandler for provided ServletContext");
|
||||
}
|
||||
return initialize(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartup(Set<Class<?>> c, ServletContext ctx)
|
||||
public void onStartup(Set<Class<?>> c, ServletContext context)
|
||||
{
|
||||
// initialize
|
||||
initialize(ctx);
|
||||
ServletContextHandler handler = ServletContextHandler.getServletContextHandler(context);
|
||||
if (handler == null)
|
||||
{
|
||||
throw new IllegalStateException("Unable to find ServletContextHandler for provided ServletContext");
|
||||
}
|
||||
initialize(handler);
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import javax.servlet.DispatcherType;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
@ -34,7 +33,6 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.http.pathmap.MappedResource;
|
||||
import org.eclipse.jetty.http.pathmap.PathSpec;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.servlet.FilterHolder;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||
@ -54,43 +52,60 @@ public class WebSocketUpgradeFilter implements Filter, MappedWebSocketCreator, D
|
||||
private static final Logger LOG = Log.getLogger(WebSocketUpgradeFilter.class);
|
||||
public static final String CONTEXT_ATTRIBUTE_KEY = "contextAttributeKey";
|
||||
public static final String CONFIG_ATTRIBUTE_KEY = "configAttributeKey";
|
||||
public static final String ATTR_KEY = WebSocketUpgradeFilter.class.getName();
|
||||
|
||||
/**
|
||||
* Immediately initialize the default WebSocketUpgradeFilter.
|
||||
*
|
||||
* <p>
|
||||
* Return default {@link WebSocketUpgradeFilter} if
|
||||
* </p>
|
||||
*
|
||||
* @param context the {@link ServletContextHandler} to use
|
||||
* @return the configured default {@link WebSocketUpgradeFilter} instance
|
||||
* @throws ServletException if the filer cannot be configured
|
||||
*/
|
||||
public static WebSocketUpgradeFilter initialize(ServletContextHandler context) throws ServletException
|
||||
{
|
||||
// Prevent double configure
|
||||
WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter) context.getAttribute(ATTR_KEY);
|
||||
if (filter == null)
|
||||
{
|
||||
// Dynamically add filter
|
||||
NativeWebSocketConfiguration configuration = NativeWebSocketServletContainerInitializer.initialize(context);
|
||||
filter = new WebSocketUpgradeFilter(configuration);
|
||||
filter.setToAttribute(context, ATTR_KEY);
|
||||
|
||||
String name = "Jetty_WebSocketUpgradeFilter";
|
||||
String pathSpec = "/*";
|
||||
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
|
||||
|
||||
FilterHolder fholder = new FilterHolder(filter);
|
||||
fholder.setName(name);
|
||||
fholder.setAsyncSupported(true);
|
||||
fholder.setInitParameter(CONTEXT_ATTRIBUTE_KEY, WebSocketUpgradeFilter.class.getName());
|
||||
context.addFilter(fholder, pathSpec, dispatcherTypes);
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("Adding [{}] {} mapped to {} to {}", name, filter, pathSpec, context);
|
||||
}
|
||||
}
|
||||
|
||||
return filter;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param context the {@link ServletContextHandler} to use
|
||||
* @return a configured {@link WebSocketUpgradeFilter} instance
|
||||
* @throws ServletException if the filer cannot be configured
|
||||
* @deprecated use {@link #initialize(ServletContextHandler)} instead
|
||||
*/
|
||||
@Deprecated
|
||||
public static WebSocketUpgradeFilter configureContext(ServletContextHandler context) throws ServletException
|
||||
{
|
||||
// Prevent double configure
|
||||
WebSocketUpgradeFilter filter = (WebSocketUpgradeFilter) context.getAttribute(WebSocketUpgradeFilter.class.getName());
|
||||
if (filter != null)
|
||||
{
|
||||
return filter;
|
||||
}
|
||||
|
||||
// Dynamically add filter
|
||||
NativeWebSocketConfiguration configuration = NativeWebSocketServletContainerInitializer.getDefaultFrom(context.getServletContext());
|
||||
filter = new WebSocketUpgradeFilter(configuration);
|
||||
filter.setToAttribute(context, WebSocketUpgradeFilter.class.getName());
|
||||
|
||||
String name = "Jetty_WebSocketUpgradeFilter";
|
||||
String pathSpec = "/*";
|
||||
EnumSet<DispatcherType> dispatcherTypes = EnumSet.of(DispatcherType.REQUEST);
|
||||
|
||||
FilterHolder fholder = new FilterHolder(filter);
|
||||
fholder.setName(name);
|
||||
fholder.setAsyncSupported(true);
|
||||
fholder.setInitParameter(CONTEXT_ATTRIBUTE_KEY, WebSocketUpgradeFilter.class.getName());
|
||||
context.addFilter(fholder, pathSpec, dispatcherTypes);
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
{
|
||||
LOG.debug("Adding [{}] {} mapped to {} to {}", name, filter, pathSpec, context);
|
||||
}
|
||||
|
||||
return filter;
|
||||
return initialize(context);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,19 +117,12 @@ public class WebSocketUpgradeFilter implements Filter, MappedWebSocketCreator, D
|
||||
@Deprecated
|
||||
public static WebSocketUpgradeFilter configureContext(ServletContext context) throws ServletException
|
||||
{
|
||||
ContextHandler handler = ContextHandler.getContextHandler(context);
|
||||
|
||||
ServletContextHandler handler = ServletContextHandler.getServletContextHandler(context);
|
||||
if (handler == null)
|
||||
{
|
||||
throw new ServletException("Not running on Jetty, WebSocket support unavailable");
|
||||
}
|
||||
|
||||
if (!(handler instanceof ServletContextHandler))
|
||||
{
|
||||
throw new ServletException("Not running in Jetty ServletContextHandler, WebSocket support via " + WebSocketUpgradeFilter.class.getName() + " unavailable");
|
||||
}
|
||||
|
||||
return configureContext((ServletContextHandler) handler);
|
||||
return initialize(handler);
|
||||
}
|
||||
|
||||
private NativeWebSocketConfiguration configuration;
|
||||
|
Loading…
x
Reference in New Issue
Block a user