Search in sources :

Example 1 with EndpointConfig

use of javax.websocket.EndpointConfig 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 EndpointConfig

use of javax.websocket.EndpointConfig 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 EndpointConfig

use of javax.websocket.EndpointConfig in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testTextAsync.

@org.junit.Test
public void testTextAsync() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Partial<String>() {

                StringBuilder sb = new StringBuilder();

                @Override
                public void onMessage(String message, boolean last) {
                    sb.append(message);
                    if (!last) {
                        return;
                    }
                    session.getAsyncRemote().sendText(sb.toString());
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 4 with EndpointConfig

use of javax.websocket.EndpointConfig in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testTextUsingWriter.

@org.junit.Test
public void testTextUsingWriter() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<String>() {

                @Override
                public void onMessage(final String message) {
                    DefaultServer.getWorker().execute(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                Writer writer = session.getBasicRemote().getSendWriter();
                                writer.write(message);
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                                cause.set(e);
                                latch.setException(e);
                            }
                        }
                    });
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Writer(java.io.Writer) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 5 with EndpointConfig

use of javax.websocket.EndpointConfig in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testTextByFuture.

@org.junit.Test
public void testTextByFuture() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Future<Void>> sendResult = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<String>() {

                @Override
                public void onMessage(String message) {
                    sendResult.set(session.getAsyncRemote().sendText(message));
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    sendResult.get();
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) Future(java.util.concurrent.Future) IoFuture(org.xnio.IoFuture) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Aggregations

Endpoint (javax.websocket.Endpoint)20 EndpointConfig (javax.websocket.EndpointConfig)20 Session (javax.websocket.Session)20 Test (org.junit.Test)19 URI (java.net.URI)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)14 UndertowSession (io.undertow.websockets.jsr.UndertowSession)14 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)14 FrameChecker (io.undertow.websockets.utils.FrameChecker)14 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)14 FutureResult (org.xnio.FutureResult)14 MessageHandler (javax.websocket.MessageHandler)11 IOException (java.io.IOException)7 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)6 ByteBuffer (java.nio.ByteBuffer)6 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5 CountDownLatch (java.util.concurrent.CountDownLatch)5