use of org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter in project spark by perwendel.
the class WebSocketServletContextHandlerFactory method create.
/**
* Creates a new websocket servlet context handler.
*
* @param webSocketHandlers webSocketHandlers
* @param webSocketIdleTimeoutMillis webSocketIdleTimeoutMillis
* @return a new websocket servlet context handler or 'null' if creation failed.
*/
public static ServletContextHandler create(Map<String, WebSocketHandlerWrapper> webSocketHandlers, Optional<Integer> webSocketIdleTimeoutMillis) {
ServletContextHandler webSocketServletContextHandler = null;
if (webSocketHandlers != null) {
try {
webSocketServletContextHandler = new ServletContextHandler(null, "/", true, false);
WebSocketUpgradeFilter webSocketUpgradeFilter = WebSocketUpgradeFilter.configureContext(webSocketServletContextHandler);
if (webSocketIdleTimeoutMillis.isPresent()) {
webSocketUpgradeFilter.getFactory().getPolicy().setIdleTimeout(webSocketIdleTimeoutMillis.get());
}
for (String path : webSocketHandlers.keySet()) {
WebSocketCreator webSocketCreator = WebSocketCreatorFactory.create(webSocketHandlers.get(path));
webSocketUpgradeFilter.addMapping(new ServletPathSpec(path), webSocketCreator);
}
} catch (Exception ex) {
logger.error("creation of websocket context handler failed.", ex);
webSocketServletContextHandler = null;
}
}
return webSocketServletContextHandler;
}
use of org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter in project spark by perwendel.
the class WebSocketServletContextHandlerFactoryTest method testCreate_whenNoIdleTimeoutIsPresent.
@Test
public void testCreate_whenNoIdleTimeoutIsPresent() throws Exception {
Map<String, WebSocketHandlerWrapper> webSocketHandlers = new HashMap<>();
webSocketHandlers.put(webSocketPath, new WebSocketHandlerClassWrapper(WebSocketTestHandler.class));
servletContextHandler = WebSocketServletContextHandlerFactory.create(webSocketHandlers, Optional.empty());
WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContextHandler.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
PathMappings.MappedResource<WebSocketCreator> mappedResource = webSocketUpgradeFilter.getMappings().getMatch("/websocket");
WebSocketCreatorFactory.SparkWebSocketCreator sc = (WebSocketCreatorFactory.SparkWebSocketCreator) mappedResource.getResource();
PathSpec pathSpec = (PathSpec) mappedResource.getPathSpec();
assertEquals("Should return the WebSocket path specified when contexst handler was created", webSocketPath, pathSpec.getPathSpec());
assertTrue("Should return true because handler should be an instance of the one we passed when it was created", sc.getHandler() instanceof WebSocketTestHandler);
}
use of org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter in project spark by perwendel.
the class WebSocketServletContextHandlerFactoryTest method testCreate_whenTimeoutIsPresent.
@Test
public void testCreate_whenTimeoutIsPresent() throws Exception {
final Integer timeout = Integer.valueOf(1000);
Map<String, WebSocketHandlerWrapper> webSocketHandlers = new HashMap<>();
webSocketHandlers.put(webSocketPath, new WebSocketHandlerClassWrapper(WebSocketTestHandler.class));
servletContextHandler = WebSocketServletContextHandlerFactory.create(webSocketHandlers, Optional.of(timeout));
WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContextHandler.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
WebSocketServerFactory webSocketServerFactory = webSocketUpgradeFilter.getFactory();
assertEquals("Timeout value should be the same as the timeout specified when context handler was created", timeout.longValue(), webSocketServerFactory.getPolicy().getIdleTimeout());
PathMappings.MappedResource<WebSocketCreator> mappedResource = webSocketUpgradeFilter.getMappings().getMatch("/websocket");
WebSocketCreatorFactory.SparkWebSocketCreator sc = (WebSocketCreatorFactory.SparkWebSocketCreator) mappedResource.getResource();
PathSpec pathSpec = (PathSpec) mappedResource.getPathSpec();
assertEquals("Should return the WebSocket path specified when context handler was created", webSocketPath, pathSpec.getPathSpec());
assertTrue("Should return true because handler should be an instance of the one we passed when it was created", sc.getHandler() instanceof WebSocketTestHandler);
}
use of org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter in project jetty.project by eclipse.
the class WebSocketServerContainerInitializer method configureContext.
/**
* Embedded Jetty approach for non-bytecode scanning.
*/
public static ServerContainer configureContext(ServletContextHandler context) throws ServletException {
// Create Basic components
NativeWebSocketConfiguration nativeWebSocketConfiguration = NativeWebSocketServletContainerInitializer.getDefaultFrom(context.getServletContext());
// Create the Jetty ServerContainer implementation
ServerContainer jettyContainer = new ServerContainer(nativeWebSocketConfiguration, context.getServer().getThreadPool());
context.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);
// Create Filter
if (isEnabledViaContext(context.getServletContext(), ADD_DYNAMIC_FILTER_KEY, true)) {
String instanceKey = WebSocketUpgradeFilter.class.getName() + ".SCI";
if (context.getAttribute(instanceKey) == null) {
if (LOG.isDebugEnabled())
LOG.debug("Dynamic filter add to support JSR356/javax.websocket.server: {}", WebSocketUpgradeFilter.class.getName());
WebSocketUpgradeFilter wsuf = WebSocketUpgradeFilter.configureContext(context);
context.setAttribute(instanceKey, wsuf);
}
}
return jettyContainer;
}
Aggregations