Search in sources :

Example 61 with Session

use of javax.websocket.Session in project tomcat by apache.

the class TestWsWebSocketContainer method testConnectToServerEndpointSSL.

@Test
public void testConnectToServerEndpointSSL() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(org.apache.tomcat.websocket.Constants.SSL_TRUSTSTORE_PROPERTY, "test/org/apache/tomcat/util/net/ca.jks");
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(MESSAGE_STRING_1, messages.peek());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 62 with Session

use of javax.websocket.Session in project tomcat by apache.

the class TestWsWebSocketContainer method doTestWriteTimeoutServer.

private void doTestWriteTimeoutServer(boolean setTimeoutOnContainer) throws Exception {
    /*
         * Note: There are all sorts of horrible uses of statics in this test
         *       because the API uses classes and the tests really need access
         *       to the instances which simply isn't possible.
         */
    timeoutOnContainer = setTimeoutOnContainer;
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ConstantTxConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + ConstantTxConfig.PATH));
    wsSession.addMessageHandler(new BlockingBinaryHandler());
    int loops = 0;
    while (loops < 15) {
        Thread.sleep(1000);
        if (!ConstantTxEndpoint.getRunning()) {
            break;
        }
        loops++;
    }
    // Close the client session, primarily to allow the
    // BackgroundProcessManager to shut down.
    wsSession.close();
    // Check the right exception was thrown
    Assert.assertNotNull(ConstantTxEndpoint.getException());
    Assert.assertEquals(ExecutionException.class, ConstantTxEndpoint.getException().getClass());
    Assert.assertNotNull(ConstantTxEndpoint.getException().getCause());
    Assert.assertEquals(SocketTimeoutException.class, ConstantTxEndpoint.getException().getCause().getClass());
    // Check correct time passed
    Assert.assertTrue(ConstantTxEndpoint.getTimeout() >= TIMEOUT_MS);
    // Check the timeout wasn't too long
    Assert.assertTrue(ConstantTxEndpoint.getTimeout() < TIMEOUT_MS * 2);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Session(javax.websocket.Session)

Example 63 with Session

use of javax.websocket.Session in project tomcat by apache.

the class TestWsWebSocketContainer method doBufferTest.

private void doBufferTest(boolean isTextBuffer, boolean isServerBuffer, boolean isTextMessage, boolean pass) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    if (isServerBuffer) {
        if (isTextBuffer) {
            ctx.addParameter(org.apache.tomcat.websocket.server.Constants.TEXT_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM, "1024");
        } else {
            ctx.addParameter(org.apache.tomcat.websocket.server.Constants.BINARY_BUFFER_SIZE_SERVLET_CONTEXT_INIT_PARAM, "1024");
        }
    } else {
        if (isTextBuffer) {
            wsContainer.setDefaultMaxTextMessageBufferSize(1024);
        } else {
            wsContainer.setDefaultMaxBinaryMessageBufferSize(1024);
        }
    }
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_BASIC));
    BasicHandler<?> handler;
    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
    tep.setLatch(latch);
    if (isTextMessage) {
        handler = new BasicText(latch);
    } else {
        handler = new BasicBinary(latch);
    }
    wsSession.addMessageHandler(handler);
    try {
        if (isTextMessage) {
            wsSession.getBasicRemote().sendText(MESSAGE_TEXT_4K);
        } else {
            wsSession.getBasicRemote().sendBinary(ByteBuffer.wrap(MESSAGE_BINARY_4K));
        }
    } catch (IOException ioe) {
    // Some messages sends are expected to fail. Assertions further on
    // in this method will check for the correct behaviour so ignore any
    // exception here.
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Queue<?> messages = handler.getMessages();
    if (pass) {
        Assert.assertEquals(1, messages.size());
        if (isTextMessage) {
            Assert.assertEquals(MESSAGE_TEXT_4K, messages.peek());
        } else {
            Assert.assertEquals(ByteBuffer.wrap(MESSAGE_BINARY_4K), messages.peek());
        }
    } else {
        // give the session a chance to complete the close process.
        for (int i = 0; i < 500; i++) {
            if (!wsSession.isOpen()) {
                break;
            }
            Thread.sleep(10);
        }
        Assert.assertFalse(wsSession.isOpen());
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) BasicBinary(org.apache.tomcat.websocket.TesterMessageCountClient.BasicBinary) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session)

Example 64 with Session

use of javax.websocket.Session in project tomcat by apache.

the class TestWsWebSocketContainer method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Set this artificially small to trigger
    // https://bz.apache.org/bugzilla/show_bug.cgi?id=57054
    wsContainer.setDefaultMaxBinaryMessageBufferSize(64);
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(MESSAGE_STRING_1, messages.peek());
    ((WsWebSocketContainer) wsContainer).destroy();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test)

Example 65 with Session

use of javax.websocket.Session in project tomcat by apache.

the class TestEncodingDecoding method testAnnotatedEndPoints.

@Test
public void testAnnotatedEndPoints() throws Exception {
    // Set up utility classes
    Server server = new Server();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(Server.class);
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_ANNOTATED_EP);
    Session session = wsContainer.connectToServer(client, uri);
    MsgString msg1 = new MsgString();
    msg1.setData(MESSAGE_ONE);
    session.getBasicRemote().sendObject(msg1);
    // Should not take very long
    int i = 0;
    while (i < 20) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(100);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(1, client.received.size());
    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE, ((MsgString) server.received.peek()).getData());
    Assert.assertEquals(MESSAGE_ONE, ((MsgString) client.received.peek()).getData());
    session.close();
    // Should not take very long but some failures have been seen
    i = testEvent(MsgStringEncoder.class.getName() + ":init", 0);
    i = testEvent(MsgStringDecoder.class.getName() + ":init", i);
    i = testEvent(MsgByteEncoder.class.getName() + ":init", i);
    i = testEvent(MsgByteDecoder.class.getName() + ":init", i);
    i = testEvent(MsgStringEncoder.class.getName() + ":destroy", i);
    i = testEvent(MsgStringDecoder.class.getName() + ":destroy", i);
    i = testEvent(MsgByteEncoder.class.getName() + ":destroy", i);
    i = testEvent(MsgByteDecoder.class.getName() + ":destroy", i);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Session (javax.websocket.Session)115 Test (org.junit.Test)92 URI (java.net.URI)73 WebSocketContainer (javax.websocket.WebSocketContainer)42 Endpoint (javax.websocket.Endpoint)39 CountDownLatch (java.util.concurrent.CountDownLatch)34 Context (org.apache.catalina.Context)31 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)31 Tomcat (org.apache.catalina.startup.Tomcat)31 EndpointConfig (javax.websocket.EndpointConfig)29 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)27 UndertowSession (io.undertow.websockets.jsr.UndertowSession)25 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)23 ServerEndpoint (javax.websocket.server.ServerEndpoint)20 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)16 IOException (java.io.IOException)16 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 FrameChecker (io.undertow.websockets.utils.FrameChecker)14