Testing / UntrustedWSServer improvements

This commit is contained in:
Joakim Erdfelt 2017-06-15 13:35:40 -07:00
parent fb3124359b
commit 41d9245d9a
1 changed files with 38 additions and 20 deletions

View File

@ -23,6 +23,7 @@ import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -55,10 +56,11 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
private URI wsUri; private URI wsUri;
private boolean ssl = false; private boolean ssl = false;
private SslContextFactory sslContextFactory; private SslContextFactory sslContextFactory;
private Consumer<Server> serverConsumer;
private Map<URI, CompletableFuture<UntrustedWSSession>> onOpenFutures = new ConcurrentHashMap<>(); private Map<URI, CompletableFuture<UntrustedWSSession>> onOpenFutures = new ConcurrentHashMap<>();
private final ServletContextHandler context = new ServletContextHandler(); private final ServletContextHandler context = new ServletContextHandler();
@Override @Override
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
@ -66,7 +68,7 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
String name = "qtp-untrustedWSServer-" + hashCode(); String name = "qtp-untrustedWSServer-" + hashCode();
threadPool.setName(name); threadPool.setName(name);
threadPool.setDaemon(true); threadPool.setDaemon(true);
// Configure Server // Configure Server
server = new Server(threadPool); server = new Server(threadPool);
if (ssl) if (ssl)
@ -80,7 +82,7 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
http_config.setResponseHeaderSize(8192); http_config.setResponseHeaderSize(8192);
http_config.setSendServerVersion(true); http_config.setSendServerVersion(true);
http_config.setSendDateHeader(false); http_config.setSendDateHeader(false);
sslContextFactory = new SslContextFactory(); sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath()); sslContextFactory.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd"); sslContextFactory.setKeyStorePassword("storepwd");
@ -88,11 +90,11 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA",
"SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA",
"SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA"); "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
// SSL HTTP Configuration // SSL HTTP Configuration
HttpConfiguration https_config = new HttpConfiguration(http_config); HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer()); https_config.addCustomizer(new SecureRequestCustomizer());
// SSL Connector // SSL Connector
connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config)); connector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config));
connector.setPort(0); connector.setPort(0);
@ -104,58 +106,74 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
connector.setPort(0); connector.setPort(0);
} }
server.addConnector(connector); server.addConnector(connector);
context.setContextPath("/"); context.setContextPath("/");
server.setHandler(context); server.setHandler(context);
// Serve untrusted endpoint // Serve untrusted endpoint
context.addServlet(UntrustedWSServlet.class, "/untrusted/*").setInitOrder(1); context.addServlet(UntrustedWSServlet.class, "/untrusted/*").setInitOrder(1);
// Allow for server customization
if (serverConsumer != null)
{
serverConsumer.accept(server);
}
// Start Server // Start Server
addBean(server); addBean(server);
super.doStart(); super.doStart();
// Wireup Context related things // Wireup Context related things
UntrustedWSSessionFactory sessionFactory = (UntrustedWSSessionFactory) context.getServletContext().getAttribute(UntrustedWSSessionFactory.class.getName()); UntrustedWSSessionFactory sessionFactory = (UntrustedWSSessionFactory) context.getServletContext().getAttribute(UntrustedWSSessionFactory.class.getName());
sessionFactory.addListener(this); sessionFactory.addListener(this);
// Establish the Server URI // Establish the Server URI
URI serverUri = server.getURI(); URI serverUri = server.getURI();
wsUri = WSURI.toWebsocket(serverUri).resolve("/"); wsUri = WSURI.toWebsocket(serverUri).resolve("/");
// Some debugging // Some debugging
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
{ {
LOG.debug("WebSocket Server URI: " + wsUri.toASCIIString()); LOG.debug("WebSocket Server URI: " + wsUri.toASCIIString());
server.dump(); server.dump();
} }
super.doStart(); super.doStart();
} }
public void setServerCustomizer(Consumer<Server> customizer)
{
this.serverConsumer = customizer;
}
public void join() throws InterruptedException
{
server.join();
}
public URI getWsUri() public URI getWsUri()
{ {
return wsUri; return wsUri;
} }
public URI getUntrustedWsUri(Class<?> clazz, TestName testname) public URI getUntrustedWsUri(Class<?> clazz, TestName testname)
{ {
return wsUri.resolve("/untrusted/" + clazz.getSimpleName() + "/" + testname.getMethodName()); return wsUri.resolve("/untrusted/" + clazz.getSimpleName() + "/" + testname.getMethodName());
} }
public void registerHttpService(String urlPattern, BiConsumer<HttpServletRequest, HttpServletResponse> serviceConsumer) public void registerHttpService(String urlPattern, BiConsumer<HttpServletRequest, HttpServletResponse> serviceConsumer)
{ {
ServletHolder holder = new ServletHolder(new BiConsumerServiceServlet(serviceConsumer)); ServletHolder holder = new ServletHolder(new BiConsumerServiceServlet(serviceConsumer));
context.addServlet(holder, urlPattern); context.addServlet(holder, urlPattern);
} }
public void registerWebSocket(String urlPattern, WebSocketCreator creator) public void registerWebSocket(String urlPattern, WebSocketCreator creator)
{ {
ServletHolder holder = new ServletHolder(new UntrustedWSServlet(creator)); ServletHolder holder = new ServletHolder(new UntrustedWSServlet(creator));
context.addServlet(holder, urlPattern); context.addServlet(holder, urlPattern);
} }
@Override @Override
public void onSessionCreate(UntrustedWSSession session, URI requestURI) public void onSessionCreate(UntrustedWSSession session, URI requestURI)
{ {
@ -167,7 +185,7 @@ public class UntrustedWSServer extends ContainerLifeCycle implements UntrustedWS
this.onOpenFutures.put(requestURI, sessionFuture); this.onOpenFutures.put(requestURI, sessionFuture);
} }
} }
public void registerOnOpenFuture(URI uri, CompletableFuture<UntrustedWSSession> sessionFuture) public void registerOnOpenFuture(URI uri, CompletableFuture<UntrustedWSSession> sessionFuture)
{ {
this.onOpenFutures.put(uri, sessionFuture); this.onOpenFutures.put(uri, sessionFuture);