Search in sources :

Example 1 with NativeWebSocketConfiguration

use of org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration in project jetty.project by eclipse.

the class ExtensionStackProcessingTest method assumeDeflateFrameAvailable.

private void assumeDeflateFrameAvailable() {
    NativeWebSocketConfiguration configuration = (NativeWebSocketConfiguration) servletContextHandler.getServletContext().getAttribute(NativeWebSocketConfiguration.class.getName());
    ExtensionFactory serverExtensionFactory = configuration.getFactory().getExtensionFactory();
    Assume.assumeTrue("Server has permessage-deflate extension registered", serverExtensionFactory.isAvailable("permessage-deflate"));
}
Also used : NativeWebSocketConfiguration(org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration) ExtensionFactory(org.eclipse.jetty.websocket.api.extensions.ExtensionFactory)

Example 2 with NativeWebSocketConfiguration

use of org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration 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));
    ServletContext servletContext = servletContextHandler.getServletContext();
    WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContext.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
    assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
    NativeWebSocketConfiguration webSocketConfiguration = (NativeWebSocketConfiguration) servletContext.getAttribute(NativeWebSocketConfiguration.class.getName());
    WebSocketServerFactory webSocketServerFactory = webSocketConfiguration.getFactory();
    assertEquals("Timeout value should be the same as the timeout specified when context handler was created", timeout.longValue(), webSocketServerFactory.getPolicy().getIdleTimeout());
    MappedResource<WebSocketCreator> mappedResource = webSocketConfiguration.getMatch("/websocket");
    PathSpec pathSpec = mappedResource.getPathSpec();
    assertEquals("Should return the WebSocket path specified when context handler was created", webSocketPath, pathSpec.getDeclaration());
// Because spark works on a non-initialized / non-started ServletContextHandler and WebSocketUpgradeFilter
// the stored WebSocketCreator is wrapped for persistence through the start/stop of those contexts.
// You cannot unwrap or cast to that WebSocketTestHandler this way.
// Only websockets that are added during a live context can be cast this way.
// WebSocketCreator sc = mappedResource.getResource();
// assertTrue("Should return true because handler should be an instance of the one we passed when it was created",
// sc.getHandler() instanceof WebSocketTestHandler);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) HashMap(java.util.HashMap) NativeWebSocketConfiguration(org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) ServletContext(javax.servlet.ServletContext) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with NativeWebSocketConfiguration

use of org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration 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());
    ServletContext servletContext = servletContextHandler.getServletContext();
    WebSocketUpgradeFilter webSocketUpgradeFilter = (WebSocketUpgradeFilter) servletContext.getAttribute("org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter");
    assertNotNull("Should return a WebSocketUpgradeFilter because we configured it to have one", webSocketUpgradeFilter);
    NativeWebSocketConfiguration webSocketConfiguration = (NativeWebSocketConfiguration) servletContext.getAttribute(NativeWebSocketConfiguration.class.getName());
    MappedResource<WebSocketCreator> mappedResource = webSocketConfiguration.getMatch("/websocket");
    PathSpec pathSpec = mappedResource.getPathSpec();
    assertEquals("Should return the WebSocket path specified when context handler was created", webSocketPath, pathSpec.getDeclaration());
// Because spark works on a non-initialized / non-started ServletContextHandler and WebSocketUpgradeFilter
// the stored WebSocketCreator is wrapped for persistence through the start/stop of those contexts.
// You cannot unwrap or cast to that WebSocketTestHandler this way.
// Only websockets that are added during a live context can be cast this way.
// WebSocketCreator sc = mappedResource.getResource();
// assertTrue("Should return true because handler should be an instance of the one we passed when it was created",
// sc.getHandler() instanceof WebSocketTestHandler);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) HashMap(java.util.HashMap) NativeWebSocketConfiguration(org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration) ServletContext(javax.servlet.ServletContext) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with NativeWebSocketConfiguration

use of org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration 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());
            }
            // Since we are configuring WebSockets before the ServletContextHandler and WebSocketUpgradeFilter is
            // even initialized / started, then we have to pre-populate the configuration that will eventually
            // be used by Jetty's WebSocketUpgradeFilter.
            NativeWebSocketConfiguration webSocketConfiguration = (NativeWebSocketConfiguration) webSocketServletContextHandler.getServletContext().getAttribute(NativeWebSocketConfiguration.class.getName());
            for (String path : webSocketHandlers.keySet()) {
                WebSocketCreator webSocketCreator = WebSocketCreatorFactory.create(webSocketHandlers.get(path));
                webSocketConfiguration.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.http.pathmap.ServletPathSpec) WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) NativeWebSocketConfiguration(org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 5 with NativeWebSocketConfiguration

use of org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration 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;
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) NativeWebSocketConfiguration(org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration) ServerContainer(org.eclipse.jetty.websocket.jsr356.server.ServerContainer)

Aggregations

NativeWebSocketConfiguration (org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration)5 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)4 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)3 HashMap (java.util.HashMap)2 ServletContext (javax.servlet.ServletContext)2 PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ServletPathSpec (org.eclipse.jetty.http.pathmap.ServletPathSpec)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ExtensionFactory (org.eclipse.jetty.websocket.api.extensions.ExtensionFactory)1 ServerContainer (org.eclipse.jetty.websocket.jsr356.server.ServerContainer)1 WebSocketServerFactory (org.eclipse.jetty.websocket.server.WebSocketServerFactory)1