Merge branch 'issue-1114' into jetty-9.3.x
This commit is contained in:
commit
4c93385611
|
@ -23,6 +23,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||
|
@ -69,6 +70,11 @@ public class PathMappings<E> implements Iterable<MappedResource<E>>, Dumpable
|
|||
mappings.clear();
|
||||
}
|
||||
|
||||
public void removeIf(Predicate<MappedResource<E>> predicate)
|
||||
{
|
||||
mappings.removeIf(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of MappedResource matches for the specified path.
|
||||
*
|
||||
|
|
|
@ -60,7 +60,7 @@ public class NativeWebSocketConfiguration extends ContainerLifeCycle implements
|
|||
@Override
|
||||
public void doStop() throws Exception
|
||||
{
|
||||
mappings.reset();
|
||||
mappings.removeIf((mapped) -> !(mapped.getResource() instanceof PersistedWebSocketCreator));
|
||||
super.doStop();
|
||||
}
|
||||
|
||||
|
@ -111,13 +111,23 @@ public class NativeWebSocketConfiguration extends ContainerLifeCycle implements
|
|||
|
||||
/**
|
||||
* Manually add a WebSocket mapping.
|
||||
* <p>
|
||||
* If mapping is added before this configuration is started, then it is persisted through
|
||||
* stop/start of this configuration's lifecycle. Otherwise it will be removed when
|
||||
* this configuration is stopped.
|
||||
* </p>
|
||||
*
|
||||
* @param pathSpec the pathspec to respond on
|
||||
* @param creator the websocket creator to activate on the provided mapping.
|
||||
*/
|
||||
public void addMapping(PathSpec pathSpec, WebSocketCreator creator)
|
||||
{
|
||||
mappings.put(pathSpec, creator);
|
||||
WebSocketCreator wsCreator = creator;
|
||||
if (!isRunning())
|
||||
{
|
||||
wsCreator = new PersistedWebSocketCreator(creator);
|
||||
}
|
||||
mappings.put(pathSpec, wsCreator);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -170,4 +180,26 @@ public class NativeWebSocketConfiguration extends ContainerLifeCycle implements
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class PersistedWebSocketCreator implements WebSocketCreator
|
||||
{
|
||||
private final WebSocketCreator delegate;
|
||||
|
||||
public PersistedWebSocketCreator(WebSocketCreator delegate)
|
||||
{
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
|
||||
{
|
||||
return delegate.createWebSocket(req, resp);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Persisted[" + super.toString() + "]";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ import javax.servlet.DispatcherType;
|
|||
import org.eclipse.jetty.http.pathmap.ServletPathSpec;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.FilterHolder;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.toolchain.test.EventQueue;
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
|
@ -166,14 +167,44 @@ public class WebSocketUpgradeFilterTest
|
|||
NativeWebSocketConfiguration configuration = new NativeWebSocketConfiguration(context.getServletContext());
|
||||
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
|
||||
configuration.addMapping(new ServletPathSpec("/info/*"), infoCreator);
|
||||
context.getServletContext().setAttribute(NativeWebSocketConfiguration.class.getName(), configuration);
|
||||
context.setAttribute(NativeWebSocketConfiguration.class.getName(), configuration);
|
||||
|
||||
server.start();
|
||||
|
||||
return server;
|
||||
}
|
||||
}});
|
||||
|
||||
|
||||
// Embedded WSUF, added as filter, apply app-ws configuration via wsuf constructor
|
||||
|
||||
cases.add(new Object[]{"wsuf/addFilter/WSUF Constructor configure", new ServerProvider()
|
||||
{
|
||||
@Override
|
||||
public Server newServer() throws Exception
|
||||
{
|
||||
Server server = new Server();
|
||||
ServerConnector connector = new ServerConnector(server);
|
||||
connector.setPort(0);
|
||||
server.addConnector(connector);
|
||||
|
||||
ServletContextHandler context = new ServletContextHandler();
|
||||
context.setContextPath("/");
|
||||
server.setHandler(context);
|
||||
|
||||
NativeWebSocketConfiguration configuration = new NativeWebSocketConfiguration(context.getServletContext());
|
||||
configuration.getFactory().getPolicy().setMaxTextMessageSize(10 * 1024 * 1024);
|
||||
configuration.addMapping(new ServletPathSpec("/info/*"), infoCreator);
|
||||
context.addBean(configuration, true);
|
||||
|
||||
FilterHolder wsufHolder = new FilterHolder(new WebSocketUpgradeFilter(configuration));
|
||||
context.addFilter(wsufHolder, "/*", EnumSet.of(DispatcherType.REQUEST));
|
||||
|
||||
server.start();
|
||||
|
||||
return server;
|
||||
}
|
||||
}});
|
||||
|
||||
// Embedded WSUF, added as filter, apply app-ws configuration via ServletContextListener
|
||||
|
||||
cases.add(new Object[]{"wsuf.configureContext/ServletContextListener configure", new ServerProvider()
|
||||
|
@ -291,7 +322,7 @@ public class WebSocketUpgradeFilterTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testConfiguration() throws Exception
|
||||
public void testNormalConfiguration() throws Exception
|
||||
{
|
||||
URI destUri = serverUri.resolve("/info/");
|
||||
|
||||
|
@ -311,4 +342,45 @@ public class WebSocketUpgradeFilterTest
|
|||
assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStopStartOfHandler() throws Exception
|
||||
{
|
||||
URI destUri = serverUri.resolve("/info/");
|
||||
|
||||
try (BlockheadClient client = new BlockheadClient(destUri))
|
||||
{
|
||||
client.connect();
|
||||
client.sendStandardRequest();
|
||||
client.expectUpgradeResponse();
|
||||
|
||||
client.write(new TextFrame().setPayload("hello 1"));
|
||||
|
||||
EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
|
||||
String payload = frames.poll().getPayloadAsUTF8();
|
||||
|
||||
// If we can connect and send a text message, we know that the endpoint was
|
||||
// added properly, and the response will help us verify the policy configuration too
|
||||
assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
|
||||
}
|
||||
|
||||
server.getHandler().stop();
|
||||
server.getHandler().start();
|
||||
|
||||
try (BlockheadClient client = new BlockheadClient(destUri))
|
||||
{
|
||||
client.connect();
|
||||
client.sendStandardRequest();
|
||||
client.expectUpgradeResponse();
|
||||
|
||||
client.write(new TextFrame().setPayload("hello 2"));
|
||||
|
||||
EventQueue<WebSocketFrame> frames = client.readFrames(1, 1000, TimeUnit.MILLISECONDS);
|
||||
String payload = frames.poll().getPayloadAsUTF8();
|
||||
|
||||
// If we can connect and send a text message, we know that the endpoint was
|
||||
// added properly, and the response will help us verify the policy configuration too
|
||||
assertThat("payload", payload, containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue