Issue #1124 - post review cleanup of changes with @sbordet
This commit is contained in:
parent
6c48d62da6
commit
63d93160f1
|
@ -146,7 +146,6 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
|
|||
if(isEnabledViaContext(context.getServletContext(), ADD_DYNAMIC_FILTER_KEY, true))
|
||||
{
|
||||
WebSocketUpgradeFilter.configureContext(context);
|
||||
NativeWebSocketServletContainerInitializer.getDefaultFrom(context.getServletContext());
|
||||
}
|
||||
|
||||
return jettyContainer;
|
||||
|
|
|
@ -85,9 +85,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
private final ClassLoader contextClassloader;
|
||||
private final Map<Integer, WebSocketHandshake> handshakes = new HashMap<>();
|
||||
/**
|
||||
* Have the factory maintain 1 and only 1 scheduler. All connections share this scheduler.
|
||||
*/
|
||||
// TODO: obtain shared (per server scheduler, somehow)
|
||||
private final Scheduler scheduler = new ScheduledExecutorScheduler();
|
||||
private final List<WebSocketSession.Listener> listeners = new CopyOnWriteArrayList<>();
|
||||
private final String supportedVersions;
|
||||
|
@ -95,12 +93,12 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
private final EventDriverFactory eventDriverFactory;
|
||||
private final ByteBufferPool bufferPool;
|
||||
private final WebSocketExtensionFactory extensionFactory;
|
||||
private ServletContext context; // can be null when this factory is used from WebSocketHandler
|
||||
private final ServletContext context; // can be null when this factory is used from WebSocketHandler
|
||||
private final List<SessionFactory> sessionFactories = new ArrayList<>();
|
||||
private final List<Class<?>> registeredSocketClasses = new ArrayList<>();
|
||||
private Executor executor;
|
||||
private List<SessionFactory> sessionFactories;
|
||||
private WebSocketCreator creator;
|
||||
private List<Class<?>> registeredSocketClasses;
|
||||
private DecoratedObjectFactory objectFactory;
|
||||
private WebSocketCreator creator;
|
||||
|
||||
public WebSocketServerFactory(ServletContext context)
|
||||
{
|
||||
|
@ -125,45 +123,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
public WebSocketServerFactory(ServletContext context, WebSocketPolicy policy, ByteBufferPool bufferPool)
|
||||
{
|
||||
Objects.requireNonNull(context, ServletContext.class.getName());
|
||||
|
||||
this.context = context;
|
||||
|
||||
handshakes.put(HandshakeRFC6455.VERSION, new HandshakeRFC6455());
|
||||
|
||||
addBean(scheduler);
|
||||
addBean(bufferPool);
|
||||
|
||||
this.contextClassloader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
this.registeredSocketClasses = new ArrayList<>();
|
||||
|
||||
this.defaultPolicy = policy;
|
||||
this.eventDriverFactory = new EventDriverFactory(defaultPolicy);
|
||||
this.bufferPool = bufferPool;
|
||||
this.extensionFactory = new WebSocketExtensionFactory(this);
|
||||
|
||||
this.sessionFactories = new ArrayList<>();
|
||||
this.sessionFactories.add(new WebSocketSessionFactory(this));
|
||||
this.creator = this;
|
||||
|
||||
// Create supportedVersions
|
||||
List<Integer> versions = new ArrayList<>();
|
||||
for (int v : handshakes.keySet())
|
||||
{
|
||||
versions.add(v);
|
||||
}
|
||||
Collections.sort(versions, Collections.reverseOrder()); // newest first
|
||||
StringBuilder rv = new StringBuilder();
|
||||
for (int v : versions)
|
||||
{
|
||||
if (rv.length() > 0)
|
||||
{
|
||||
rv.append(", ");
|
||||
}
|
||||
rv.append(v);
|
||||
}
|
||||
supportedVersions = rv.toString();
|
||||
this(Objects.requireNonNull(context, ServletContext.class.getName()), policy, null, null, bufferPool);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -175,7 +135,13 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
*/
|
||||
protected WebSocketServerFactory(WebSocketPolicy policy, Executor executor, ByteBufferPool bufferPool)
|
||||
{
|
||||
this.objectFactory = new DecoratedObjectFactory();
|
||||
this(null, policy, new DecoratedObjectFactory(), executor, bufferPool);
|
||||
}
|
||||
|
||||
private WebSocketServerFactory(ServletContext context, WebSocketPolicy policy, DecoratedObjectFactory objectFactory, Executor executor, ByteBufferPool bufferPool)
|
||||
{
|
||||
this.context = context;
|
||||
this.objectFactory = objectFactory;
|
||||
this.executor = executor;
|
||||
|
||||
handshakes.put(HandshakeRFC6455.VERSION, new HandshakeRFC6455());
|
||||
|
@ -185,14 +151,11 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
|
||||
this.contextClassloader = Thread.currentThread().getContextClassLoader();
|
||||
|
||||
this.registeredSocketClasses = new ArrayList<>();
|
||||
|
||||
this.defaultPolicy = policy;
|
||||
this.eventDriverFactory = new EventDriverFactory(defaultPolicy);
|
||||
this.bufferPool = bufferPool;
|
||||
this.extensionFactory = new WebSocketExtensionFactory(this);
|
||||
|
||||
this.sessionFactories = new ArrayList<>();
|
||||
this.sessionFactories.add(new WebSocketSessionFactory(this));
|
||||
this.creator = this;
|
||||
|
||||
|
@ -287,12 +250,6 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
this.sessionFactories.add(sessionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebSocketServletFactory createFactory(ServletContext context, WebSocketPolicy policy)
|
||||
{
|
||||
return new WebSocketServerFactory(context, policy, bufferPool);
|
||||
}
|
||||
|
||||
private WebSocketSession createSession(URI requestURI, EventDriver websocket, LogicalConnection connection)
|
||||
{
|
||||
if (websocket == null)
|
||||
|
@ -353,7 +310,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc
|
|||
this.objectFactory = (DecoratedObjectFactory) context.getAttribute(DecoratedObjectFactory.ATTR);
|
||||
if (this.objectFactory == null)
|
||||
{
|
||||
throw new RuntimeException("Unable to find required ServletContext attribute: " + DecoratedObjectFactory.ATTR);
|
||||
throw new IllegalStateException("Unable to find required ServletContext attribute: " + DecoratedObjectFactory.ATTR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,6 @@ import org.eclipse.jetty.servlet.FilterHolder;
|
|||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||
import org.eclipse.jetty.util.component.AbstractLifeCycle;
|
||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||
import org.eclipse.jetty.util.component.Dumpable;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
@ -51,7 +50,7 @@ import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
|||
* Inline Servlet Filter to capture WebSocket upgrade requests and perform path mappings to {@link WebSocketCreator} objects.
|
||||
*/
|
||||
@ManagedObject("WebSocket Upgrade Filter")
|
||||
public class WebSocketUpgradeFilter extends AbstractLifeCycle implements Filter, MappedWebSocketCreator, Dumpable
|
||||
public class WebSocketUpgradeFilter implements Filter, MappedWebSocketCreator, Dumpable
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(WebSocketUpgradeFilter.class);
|
||||
public static final String CONTEXT_ATTRIBUTE_KEY = "contextAttributeKey";
|
||||
|
@ -111,6 +110,7 @@ public class WebSocketUpgradeFilter extends AbstractLifeCycle implements Filter,
|
|||
}
|
||||
|
||||
private NativeWebSocketConfiguration configuration;
|
||||
private boolean localConfiguration = false;
|
||||
private boolean alreadySetToAttribute = false;
|
||||
|
||||
public WebSocketUpgradeFilter()
|
||||
|
@ -150,7 +150,10 @@ public class WebSocketUpgradeFilter extends AbstractLifeCycle implements Filter,
|
|||
try
|
||||
{
|
||||
alreadySetToAttribute = false;
|
||||
configuration.stop();
|
||||
if(localConfiguration)
|
||||
{
|
||||
configuration.stop();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -310,7 +313,11 @@ public class WebSocketUpgradeFilter extends AbstractLifeCycle implements Filter,
|
|||
}
|
||||
}
|
||||
|
||||
this.configuration.start();
|
||||
if(!this.configuration.isRunning())
|
||||
{
|
||||
localConfiguration = true;
|
||||
this.configuration.start();
|
||||
}
|
||||
|
||||
String max = config.getInitParameter("maxIdleTime");
|
||||
if (max != null)
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2016 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.server;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
|
||||
|
||||
public class InfoContextAltAttributeListener implements WebSocketCreator, ServletContextListener
|
||||
{
|
||||
private static final String ATTR = "alt.config";
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce)
|
||||
{
|
||||
NativeWebSocketConfiguration configuration = new NativeWebSocketConfiguration(sce.getServletContext());
|
||||
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
|
||||
configuration.addMapping(new ServletPathSpec("/info/*"), this);
|
||||
sce.getServletContext().setAttribute(ATTR, configuration);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
|
||||
{
|
||||
return new InfoSocket();
|
||||
}
|
||||
}
|
|
@ -208,10 +208,10 @@ public class WebSocketUpgradeFilterTest
|
|||
File testDir = MavenTestingUtils.getTargetTestingDir("WSUF-webxml");
|
||||
|
||||
WSServer server = new WSServer(testDir, "/");
|
||||
|
||||
|
||||
server.copyWebInf("wsuf-config-via-listener.xml");
|
||||
server.copyClass(InfoSocket.class);
|
||||
server.copyClass(InfoContextAttributeListener.class);
|
||||
server.copyWebInf("wsuf-config-via-listener.xml");
|
||||
server.start();
|
||||
|
||||
WebAppContext webapp = server.createWebAppContext();
|
||||
|
@ -231,10 +231,33 @@ public class WebSocketUpgradeFilterTest
|
|||
File testDir = MavenTestingUtils.getTargetTestingDir("WSUF-webxml");
|
||||
|
||||
WSServer server = new WSServer(testDir, "/");
|
||||
|
||||
|
||||
server.copyWebInf("wsuf-config-via-servlet-init.xml");
|
||||
server.copyClass(InfoSocket.class);
|
||||
server.copyClass(InfoServlet.class);
|
||||
server.copyWebInf("wsuf-config-via-servlet-init.xml");
|
||||
server.start();
|
||||
|
||||
WebAppContext webapp = server.createWebAppContext();
|
||||
server.deployWebapp(webapp);
|
||||
|
||||
return server.getServer();
|
||||
}
|
||||
}});
|
||||
|
||||
// xml based, wsuf, on alternate url-pattern and config attribute location
|
||||
|
||||
cases.add(new Object[]{"wsuf/WebAppContext/web.xml/ServletContextListener/alt-config", new ServerProvider()
|
||||
{
|
||||
@Override
|
||||
public Server newServer() throws Exception
|
||||
{
|
||||
File testDir = MavenTestingUtils.getTargetTestingDir("WSUF-webxml");
|
||||
|
||||
WSServer server = new WSServer(testDir, "/");
|
||||
|
||||
server.copyWebInf("wsuf-alt-config-via-listener.xml");
|
||||
server.copyClass(InfoSocket.class);
|
||||
server.copyClass(InfoContextAltAttributeListener.class);
|
||||
server.start();
|
||||
|
||||
WebAppContext webapp = server.createWebAppContext();
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<listener>
|
||||
<listener-class>org.eclipse.jetty.websocket.server.InfoContextAltAttributeListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<filter>
|
||||
<filter-name>wsuf-alt</filter-name>
|
||||
<filter-class>org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter</filter-class>
|
||||
<init-param>
|
||||
<param-name>configAttributeKey</param-name>
|
||||
<param-value>alt.config</param-value>
|
||||
</init-param>
|
||||
</filter>
|
||||
|
||||
<filter-mapping>
|
||||
<filter-name>wsuf-alt</filter-name>
|
||||
<url-pattern>/info/*</url-pattern>
|
||||
</filter-mapping>
|
||||
</web-app>
|
|
@ -66,8 +66,6 @@ public interface WebSocketServletFactory
|
|||
void start() throws Exception;
|
||||
void stop() throws Exception;
|
||||
|
||||
WebSocketServletFactory createFactory(ServletContext context, WebSocketPolicy policy);
|
||||
|
||||
WebSocketCreator getCreator();
|
||||
|
||||
ExtensionFactory getExtensionFactory();
|
||||
|
|
Loading…
Reference in New Issue