Search in sources :

Example 6 with ServletUpgradeResponse

use of org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse in project traccar by traccar.

the class AsyncSocketServlet method configure.

@Override
public void configure(WebSocketServletFactory factory) {
    factory.getPolicy().setIdleTimeout(Context.getConfig().getLong("web.timeout", ASYNC_TIMEOUT));
    factory.setCreator(new WebSocketCreator() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
            if (req.getSession() != null) {
                long userId = (Long) req.getSession().getAttribute(SessionResource.USER_ID_KEY);
                return new AsyncSocket(userId);
            } else {
                return null;
            }
        }
    });
}
Also used : WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)

Example 7 with ServletUpgradeResponse

use of org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse 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 8 with ServletUpgradeResponse

use of org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse 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 9 with ServletUpgradeResponse

use of org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse in project joynr by bmwcarit.

the class WebSocketJettyServer method start.

@Override
public void start() {
    if (server != null && server.isRunning()) {
        return;
    }
    server = new Server();
    ServerConnector connector;
    if (address.getProtocol().equals(WebSocketProtocol.WSS)) {
        SslContextFactory sslContextFactory = new SslContextFactory();
        SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
        connector = new ServerConnector(server, sslConnectionFactory);
    } else {
        connector = new ServerConnector(server);
    }
    connector.setPort(address.getPort());
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    ServletHolder holderEvents = new ServletHolder("ws-events", new WebSocketServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        public void configure(WebSocketServletFactory webSocketServletFactory) {
            webSocketServletFactory.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
            webSocketServletFactory.getPolicy().setMaxTextMessageSize(maxMessageSize);
            webSocketServletFactory.setCreator(new WebSocketCreator() {

                @Override
                public Object createWebSocket(ServletUpgradeRequest servletUpgradeRequest, ServletUpgradeResponse servletUpgradeResponse) {
                    CCWebSocketMessagingSkeletonSocket socket = new CCWebSocketMessagingSkeletonSocket(WebSocketJettyServer.this);
                    openSockets.add(socket);
                    return socket;
                }
            });
        }
    });
    context.addServlet(holderEvents, address.getPath());
    try {
        server.start();
    } catch (Exception t) {
        logger.error("Error while starting websocket server: ", t);
    }
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) WebSocketServlet(org.eclipse.jetty.websocket.servlet.WebSocketServlet) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) IOException(java.io.IOException) JoynrDelayMessageException(io.joynr.exceptions.JoynrDelayMessageException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)

Aggregations

ServletUpgradeRequest (org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest)9 ServletUpgradeResponse (org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)9 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)6 WebSocketServletFactory (org.eclipse.jetty.websocket.servlet.WebSocketServletFactory)3 IOException (java.io.IOException)2 HttpCookie (java.net.HttpCookie)2 List (java.util.List)2 Map (java.util.Map)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 Endpoint (javax.websocket.Endpoint)2 EndpointConfig (javax.websocket.EndpointConfig)2 Session (javax.websocket.Session)2 WebSocketContainer (javax.websocket.WebSocketContainer)2 Server (org.eclipse.jetty.server.Server)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 WebSocketServlet (org.eclipse.jetty.websocket.servlet.WebSocketServlet)2 Test (org.junit.Test)2 InstrumentedQueuedThreadPool (com.codahale.metrics.jetty9.InstrumentedQueuedThreadPool)1 InstrumentedHandler (com.kixeye.chassis.transport.http.InstrumentedHandler)1