Search in sources :

Example 6 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator 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);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) HashMap(java.util.HashMap) PathSpec(org.eclipse.jetty.websocket.server.pathmap.PathSpec) PathMappings(org.eclipse.jetty.websocket.server.pathmap.PathMappings) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 7 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator 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);
}
Also used : WebSocketUpgradeFilter(org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter) WebSocketServerFactory(org.eclipse.jetty.websocket.server.WebSocketServerFactory) HashMap(java.util.HashMap) PathSpec(org.eclipse.jetty.websocket.server.pathmap.PathSpec) PathMappings(org.eclipse.jetty.websocket.server.pathmap.PathMappings) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator in project chassis by Kixeye.

the class WebSocketTransportConfiguration method webSocketServer.

@Bean(initMethod = "start", destroyMethod = "stop")
@Order(0)
public Server webSocketServer(@Value("${websocket.enabled:false}") boolean websocketEnabled, @Value("${websocket.hostname:}") String websocketHostname, @Value("${websocket.port:-1}") int websocketPort, @Value("${secureWebsocket.enabled:false}") boolean secureWebsocketEnabled, @Value("${secureWebsocket.hostname:}") String secureWebsocketHostname, @Value("${secureWebsocket.port:-1}") int secureWebsocketPort, @Value("${secureWebsocket.selfSigned:false}") boolean selfSigned, @Value("${secureWebsocket.mutualSsl:false}") boolean mutualSsl, @Value("${secureWebsocket.keyStorePath:}") String keyStorePath, @Value("${secureWebsocket.keyStoreData:}") String keyStoreData, @Value("${secureWebsocket.keyStorePassword:}") String keyStorePassword, @Value("${secureWebsocket.keyManagerPassword:}") String keyManagerPassword, @Value("${secureWebsocket.trustStorePath:}") String trustStorePath, @Value("${secureWebsocket.trustStoreData:}") String trustStoreData, @Value("${secureWebsocket.trustStorePassword:}") String trustStorePassword, @Value("${securewebsocket.excludedCipherSuites:}") String[] excludedCipherSuites) throws Exception {
    // set up servlets
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY);
    context.setErrorHandler(null);
    context.setWelcomeFiles(new String[] { "/" });
    for (final MessageSerDe serDe : serDes) {
        // create the websocket creator
        final WebSocketCreator webSocketCreator = new WebSocketCreator() {

            public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
                // this will have spring construct a new one for every session
                ActionInvokingWebSocket webSocket = forwardingWebSocket();
                webSocket.setSerDe(serDe);
                webSocket.setUpgradeRequest(req);
                webSocket.setUpgradeResponse(resp);
                return webSocket;
            }
        };
        // configure the websocket servlet
        ServletHolder webSocketServlet = new ServletHolder(new WebSocketServlet() {

            private static final long serialVersionUID = -3022799271546369505L;

            @Override
            public void configure(WebSocketServletFactory factory) {
                factory.setCreator(webSocketCreator);
            }
        });
        Map<String, String> webSocketProperties = new HashMap<>();
        AbstractConfiguration config = ConfigurationManager.getConfigInstance();
        Iterator<String> webSocketPropertyKeys = config.getKeys("websocket");
        while (webSocketPropertyKeys.hasNext()) {
            String key = webSocketPropertyKeys.next();
            webSocketProperties.put(key.replaceFirst(Pattern.quote("websocket."), ""), config.getString(key));
        }
        webSocketServlet.setInitParameters(webSocketProperties);
        context.addServlet(webSocketServlet, "/" + serDe.getMessageFormatName() + "/*");
    }
    // create the server
    Server server;
    if (metricRegistry == null || !monitorThreadpool) {
        server = new Server();
        server.setHandler(context);
    } else {
        server = new Server(new InstrumentedQueuedThreadPool(metricRegistry));
        InstrumentedHandler instrumented = new InstrumentedHandler(metricRegistry);
        instrumented.setHandler(context);
        server.setHandler(instrumented);
    }
    // set up connectors
    if (websocketEnabled) {
        InetSocketAddress address = StringUtils.isBlank(websocketHostname) ? new InetSocketAddress(websocketPort) : new InetSocketAddress(websocketHostname, websocketPort);
        JettyConnectorRegistry.registerHttpConnector(server, address);
    }
    if (secureWebsocketEnabled) {
        InetSocketAddress address = StringUtils.isBlank(secureWebsocketHostname) ? new InetSocketAddress(secureWebsocketPort) : new InetSocketAddress(secureWebsocketHostname, secureWebsocketPort);
        JettyConnectorRegistry.registerHttpsConnector(server, address, selfSigned, mutualSsl, keyStorePath, keyStoreData, keyStorePassword, keyManagerPassword, trustStorePath, trustStoreData, trustStorePassword, excludedCipherSuites);
    }
    return server;
}
Also used : InstrumentedHandler(com.kixeye.chassis.transport.http.InstrumentedHandler) WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) InetSocketAddress(java.net.InetSocketAddress) MessageSerDe(com.kixeye.chassis.transport.serde.MessageSerDe) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) AbstractConfiguration(org.apache.commons.configuration.AbstractConfiguration) InstrumentedQueuedThreadPool(com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) Order(org.springframework.core.annotation.Order) Bean(org.springframework.context.annotation.Bean)

Example 9 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator in project jetty.project by eclipse.

the class NativeWebSocketConfiguration method removeMapping.

@Override
public boolean removeMapping(String rawspec) {
    PathSpec pathSpec = toPathSpec(rawspec);
    boolean removed = false;
    for (Iterator<MappedResource<WebSocketCreator>> iterator = mappings.iterator(); iterator.hasNext(); ) {
        MappedResource<WebSocketCreator> mapping = iterator.next();
        if (mapping.getPathSpec().equals(pathSpec)) {
            iterator.remove();
            removed = true;
        }
    }
    return removed;
}
Also used : WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) RegexPathSpec(org.eclipse.jetty.http.pathmap.RegexPathSpec) ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) MappedResource(org.eclipse.jetty.http.pathmap.MappedResource)

Example 10 with WebSocketCreator

use of org.eclipse.jetty.websocket.servlet.WebSocketCreator in project jetty.project by eclipse.

the class WebSocketUpgradeHandlerWrapper method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (configuration.getFactory().isUpgradeRequest(request, response)) {
        MappedResource<WebSocketCreator> resource = configuration.getMatch(target);
        if (resource == null) {
            // no match.
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "No websocket endpoint matching path: " + target);
            return;
        }
        WebSocketCreator creator = resource.getResource();
        // Store PathSpec resource mapping as request attribute
        request.setAttribute(PathSpec.class.getName(), resource);
        // We have an upgrade request
        if (configuration.getFactory().acceptWebSocket(creator, request, response)) {
            // We have a socket instance created
            return;
        }
        // due to incoming request constraints (controlled by WebSocketCreator)
        if (response.isCommitted()) {
            // not much we can do at this point.
            return;
        }
    }
    super.handle(target, baseRequest, request, response);
}
Also used : WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec)

Aggregations

WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)12 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)3 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)3 WebSocketUpgradeFilter (org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter)3 WebSocketServletFactory (org.eclipse.jetty.websocket.servlet.WebSocketServletFactory)3 Server (org.eclipse.jetty.server.Server)2 WebSocketServerFactory (org.eclipse.jetty.websocket.server.WebSocketServerFactory)2 PathMappings (org.eclipse.jetty.websocket.server.pathmap.PathMappings)2 PathSpec (org.eclipse.jetty.websocket.server.pathmap.PathSpec)2 ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)2 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)2 WebSocketServlet (org.eclipse.jetty.websocket.servlet.WebSocketServlet)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)1 BuckEventExternalInterface (com.facebook.buck.event.external.events.BuckEventExternalInterface)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Maps (com.google.common.collect.Maps)1 InstrumentedHandler (com.kixeye.chassis.transport.http.InstrumentedHandler)1