Search in sources :

Example 61 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat70 by apache.

the class TestWebSocketFrameClientSSL method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    URL truststoreUrl = this.getClass().getClassLoader().getResource(TesterSupport.CA_JKS);
    File truststoreFile = new File(truststoreUrl.toURI());
    clientEndpointConfig.getUserProperties().put(WsWebSocketContainer.SSL_TRUSTSTORE_PROPERTY, truststoreFile.getAbsolutePath());
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
    CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    // Ignore the latch result as the message count test below will tell us
    // if the right number of messages arrived
    handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
    for (String message : messages) {
        Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) URL(java.net.URL) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) File(java.io.File) Session(javax.websocket.Session) Test(org.junit.Test)

Example 62 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat70 by apache.

the class TestWsPingPongMessages method testPingPongMessages.

@Test
public void testPingPongMessages() 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.addServletMapping("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
    tep.setLatch(latch);
    PongMessageHandler handler = new PongMessageHandler(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendPing(applicationData);
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Assert.assertArrayEquals(applicationData.array(), (handler.getMessages().peek()).getApplicationData().array());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Example 63 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat70 by apache.

the class TestEncodingDecoding method testGenericsCoders.

@Test
public void testGenericsCoders() throws Exception {
    // Set up utility classes
    GenericsServer server = new GenericsServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(GenericsServer.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.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    GenericsClient client = new GenericsClient();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_GENERICS_EP);
    Session session = wsContainer.connectToServer(client, uri);
    ArrayList<String> list = new ArrayList<String>(2);
    list.add("str1");
    list.add("str2");
    session.getBasicRemote().sendObject(list);
    // Should not take very long
    int i = 0;
    while (i < 20) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        Thread.sleep(100);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(server.received.peek().toString(), "[str1, str2]");
    Assert.assertEquals(1, client.received.size());
    Assert.assertEquals(client.received.peek().toString(), "[str1, str2]");
    session.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ArrayList(java.util.ArrayList) 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)

Example 64 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat70 by apache.

the class TestPojoMethodMapping method test.

@Test
public void test() 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.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/" + PARAM_ONE + "/" + PARAM_TWO + "/" + PARAM_THREE);
    Session session = wsContainer.connectToServer(client, uri);
    session.getBasicRemote().sendText("NO-OP");
    session.close();
    // Give server 20s to close. 5s should be plenty but the Gump VM is slow
    int count = 0;
    while (count < 200) {
        if (server.isClosed()) {
            break;
        }
        count++;
        Thread.sleep(100);
    }
    if (count == 50) {
        Assert.fail("Server did not process an onClose event within 5 " + "seconds of the client sending a close message");
    }
    // Check no errors
    List<String> errors = server.getErrors();
    for (String error : errors) {
        System.err.println(error);
    }
    Assert.assertEquals("Found errors", 0, errors.size());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) ServerEndpoint(javax.websocket.server.ServerEndpoint) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 65 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat70 by apache.

the class TestWsServerContainer method testBug58232.

@Test
public void testBug58232() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/echoBasic");
    Session session = null;
    try {
        session = wsContainer.connectToServer(client, uri);
        CountDownLatch latch = new CountDownLatch(1);
        BasicText handler = new BasicText(latch);
        session.addMessageHandler(handler);
        session.getBasicRemote().sendText("echoBasic");
        boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
        Assert.assertTrue(latchResult);
        Queue<String> messages = handler.getMessages();
        Assert.assertEquals(1, messages.size());
        for (String message : messages) {
            Assert.assertEquals("echoBasic", message);
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)85 URI (java.net.URI)52 Session (javax.websocket.Session)52 Test (org.junit.Test)51 Context (org.apache.catalina.Context)29 Tomcat (org.apache.catalina.startup.Tomcat)29 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)27 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 DeploymentException (javax.websocket.DeploymentException)13 Endpoint (javax.websocket.Endpoint)13 EndpointConfig (javax.websocket.EndpointConfig)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)10 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)10 IOException (java.io.IOException)8 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 PrintWriter (java.io.PrintWriter)6