Search in sources :

Example 1 with ServletUpgradeRequest

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

the class CookiesTest method testCookiesAreSentToClient.

@Test
public void testCookiesAreSentToClient() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieDomain = "domain";
    final String cookiePath = "/path";
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            String cookieString = cookieName + "=" + cookieValue + ";Domain=" + cookieDomain + ";Path=" + cookiePath;
            response.getHeaders().put("Set-Cookie", Collections.singletonList(cookieString));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void afterResponse(HandshakeResponse response) {
            Map<String, List<String>> headers = response.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("set-cookie"));
            List<String> values = headers.get("Set-Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            List<HttpCookie> cookies = HttpCookie.parse(values.get(0));
            Assert.assertEquals(1, cookies.size());
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Assert.assertEquals(cookieDomain, cookie.getDomain());
            Assert.assertEquals(cookiePath, cookie.getPath());
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) HandshakeResponse(javax.websocket.HandshakeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Map(java.util.Map) HttpCookie(java.net.HttpCookie) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 2 with ServletUpgradeRequest

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

the class CookiesTest method testCookiesAreSentToServer.

@Test
public void testCookiesAreSentToServer() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieString = cookieName + "=" + cookieValue;
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            List<HttpCookie> cookies = request.getCookies();
            assertThat("Cookies", cookies, notNullValue());
            assertThat("Cookies", cookies.size(), is(1));
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Map<String, List<String>> headers = request.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("cookie"));
            List<String> values = headers.get("Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            Assert.assertEquals(cookieString, values.get(0));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Cookie", Collections.singletonList(cookieString));
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) HttpCookie(java.net.HttpCookie) Map(java.util.Map) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 3 with ServletUpgradeRequest

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

the class WebSocketServerFactory method acceptWebSocket.

@Override
public boolean acceptWebSocket(WebSocketCreator creator, HttpServletRequest request, HttpServletResponse response) throws IOException {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(contextClassloader);
        // Create Servlet Specific Upgrade Request/Response objects
        ServletUpgradeRequest sockreq = new ServletUpgradeRequest(request);
        ServletUpgradeResponse sockresp = new ServletUpgradeResponse(response);
        Object websocketPojo = creator.createWebSocket(sockreq, sockresp);
        // Handle response forbidden (and similar paths)
        if (sockresp.isCommitted()) {
            return false;
        }
        if (websocketPojo == null) {
            // no creation, sorry
            sockresp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Endpoint Creation Failed");
            return false;
        }
        // Allow Decorators to do their thing
        websocketPojo = getObjectFactory().decorate(websocketPojo);
        // Get the original HTTPConnection
        HttpConnection connection = (HttpConnection) request.getAttribute("org.eclipse.jetty.server.HttpConnection");
        // Send the upgrade
        EventDriver driver = eventDriverFactory.wrap(websocketPojo);
        return upgrade(connection, sockreq, sockresp, driver);
    } catch (URISyntaxException e) {
        throw new IOException("Unable to accept websocket due to mangled URI", e);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : EventDriver(org.eclipse.jetty.websocket.common.events.EventDriver) HttpConnection(org.eclipse.jetty.server.HttpConnection) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse)

Example 4 with ServletUpgradeRequest

use of org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest in project jphp by jphp-compiler.

the class PHttpServer method addWebSocket.

@Signature
public void addWebSocket(Environment env, String path, ArrayMemory _handlers) {
    WebSocketParam param = _handlers.toBean(env, WebSocketParam.class);
    ContextHandler contextHandler = new ContextHandler(path);
    contextHandler.setHandler(new WebSocketHandler() {

        @Override
        public void configure(WebSocketServletFactory factory) {
            factory.setCreator(new WebSocketCreator() {

                @Override
                public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
                    return new WebSocket(env, param);
                }
            });
        }
    });
    handlers.addHandler(contextHandler);
}
Also used : WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) WebSocketHandler(org.eclipse.jetty.websocket.server.WebSocketHandler) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Signature(php.runtime.annotation.Reflection.Signature)

Example 5 with ServletUpgradeRequest

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

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)

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