Search in sources :

Example 1 with ServletPathSpec

use of org.eclipse.jetty.websocket.server.pathmap.ServletPathSpec 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;
}
Also used : ServletPathSpec(org.eclipse.jetty.websocket.server.pathmap.ServletPathSpec) WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)1 ServletPathSpec (org.eclipse.jetty.websocket.server.pathmap.ServletPathSpec)1 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)1