Fixes #3139 - NPE in WebSocketContainerInitializer

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2018-11-27 14:42:30 -06:00
parent 8c4ee8496f
commit a703fc626b
3 changed files with 17 additions and 3 deletions

View File

@ -23,6 +23,8 @@ import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.server.Server;
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.jsr356.server.ServerContainer;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
@ -49,10 +51,12 @@ public class WebSocketJsrServer
{
Server server = new Server(8080);
HandlerList handlers = new HandlerList();
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
handlers.addHandler(context);
// Enable javax.websocket configuration for the context
ServerContainer wsContainer = WebSocketServerContainerInitializer
@ -61,6 +65,9 @@ public class WebSocketJsrServer
// Add your websockets to the container
wsContainer.addEndpoint(EchoJsrSocket.class);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
server.start();
context.dumpStdErr();
server.join();

View File

@ -143,7 +143,7 @@ public class WebSocketServerContainerInitializer implements ServletContainerInit
// Build HttpClient
HttpClient httpClient = (HttpClient) context.getServletContext().getAttribute(HTTPCLIENT_ATTRIBUTE);
if(httpClient == null)
if ((httpClient == null) && (context.getServer() != null))
{
httpClient = (HttpClient) context.getServer().getAttribute(HTTPCLIENT_ATTRIBUTE);
}

View File

@ -58,6 +58,8 @@ import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
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.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -403,9 +405,11 @@ public class ConfiguratorTest
connector.setPort(0);
server.addConnector(connector);
HandlerList handlers = new HandlerList();
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
handlers.addHandler(context);
ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
container.addEndpoint(CaptureHeadersSocket.class);
@ -423,6 +427,9 @@ public class ConfiguratorTest
.build();
container.addEndpoint(overrideEndpointConfig);
handlers.addHandler(new DefaultHandler());
server.setHandler(handlers);
server.start();
String host = connector.getHost();
if (host == null)