Search in sources :

Example 1 with WebSocketServerFactory

use of org.eclipse.jetty.websocket.server.WebSocketServerFactory in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method start.

@Override
public void start() {
    synchronized (this.lifecycleMonitor) {
        if (!isRunning() && this.servletContext != null) {
            this.running = true;
            try {
                this.factory = new WebSocketServerFactory(this.servletContext);
                this.factory.setCreator((request, response) -> {
                    WebSocketHandlerContainer container = adapterHolder.get();
                    String protocol = container.getProtocol().orElse(null);
                    if (protocol != null) {
                        response.setAcceptedSubProtocol(protocol);
                    }
                    return container.getAdapter();
                });
                this.factory.start();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to start WebSocketServerFactory", ex);
            }
        }
    }
}
Also used : WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory)

Example 2 with WebSocketServerFactory

use of org.eclipse.jetty.websocket.server.WebSocketServerFactory in project CQ-Actions by Cognifide.

the class MessageWebsocketServlet method activate.

@Activate
public void activate() throws Exception {
    WebSocketBehavior behavior = WebSocketBehavior.SERVER;
    WebSocketPolicy policy = new WebSocketPolicy(behavior);
    webSocketFactory = new WebSocketServerFactory(policy);
    webSocketFactory.register(MessageSocket.class);
    webSocketFactory.setCreator(this);
    webSocketFactory.start();
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) WebSocketBehavior(org.eclipse.jetty.websocket.api.WebSocketBehavior) Activate(org.apache.felix.scr.annotations.Activate)

Example 3 with WebSocketServerFactory

use of org.eclipse.jetty.websocket.server.WebSocketServerFactory 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 4 with WebSocketServerFactory

use of org.eclipse.jetty.websocket.server.WebSocketServerFactory in project spring-framework by spring-projects.

the class JettyRequestUpgradeStrategy method start.

@Override
public void start() {
    if (!isRunning()) {
        this.running = true;
        try {
            if (this.factory == null) {
                this.factory = new WebSocketServerFactory(servletContext, this.policy);
            }
            this.factory.setCreator(new WebSocketCreator() {

                @Override
                public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
                    WebSocketHandlerContainer container = containerHolder.get();
                    Assert.state(container != null, "Expected WebSocketHandlerContainer");
                    response.setAcceptedSubProtocol(container.getSelectedProtocol());
                    response.setExtensions(container.getExtensionConfigs());
                    return container.getHandler();
                }
            });
            this.factory.start();
        } catch (Throwable ex) {
            throw new IllegalStateException("Unable to start Jetty WebSocketServerFactory", ex);
        }
    }
}
Also used : WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)

Example 5 with WebSocketServerFactory

use of org.eclipse.jetty.websocket.server.WebSocketServerFactory in project ma-core-public by infiniteautomation.

the class MangoWebSocketConfigurer method handshakeHandler.

@Bean
public DefaultHandshakeHandler handshakeHandler() {
    WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    policy.setInputBufferSize(8192);
    // We don't want timeouts..
    policy.setIdleTimeout(Integer.MAX_VALUE);
    WebSocketServerFactory factory = new WebSocketServerFactory(policy);
    return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy(factory));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) JettyRequestUpgradeStrategy(org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) Bean(org.springframework.context.annotation.Bean)

Aggregations

WebSocketServerFactory (org.eclipse.jetty.websocket.server.WebSocketServerFactory)5 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)2 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)2 HashMap (java.util.HashMap)1 ServletContext (javax.servlet.ServletContext)1 Activate (org.apache.felix.scr.annotations.Activate)1 PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)1 WebSocketBehavior (org.eclipse.jetty.websocket.api.WebSocketBehavior)1 NativeWebSocketConfiguration (org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration)1 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)1 ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)1 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Bean (org.springframework.context.annotation.Bean)1 JettyRequestUpgradeStrategy (org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy)1 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)1