Search in sources :

Example 76 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.

the class TestWsWebSocketContainerSSL method testConnectToServerEndpointSslLegacy.

@SuppressWarnings("removal")
@Test
public void testConnectToServerEndpointSslLegacy() 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();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(org.apache.tomcat.websocket.Constants.SSL_TRUSTSTORE_PROPERTY, TesterSupport.CA_JKS);
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + 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 : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 77 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.

the class TestWsWebSocketContainerSSL 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");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Build the SSLContext
    SSLContext sslContext = SSLContext.getInstance("TLS");
    File trustStoreFile = new File(TesterSupport.CA_JKS);
    KeyStore ks = KeyStore.getInstance("JKS");
    try (InputStream is = new FileInputStream(trustStoreFile)) {
        KeyStoreUtil.load(ks, is, TesterSupport.JKS_PASS.toCharArray());
    }
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ks);
    sslContext.init(null, tmf.getTrustManagers(), null);
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().sslContext(sslContext).build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + 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 : SSLContext(javax.net.ssl.SSLContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLContext(javax.net.ssl.SSLContext) CountDownLatch(java.util.concurrent.CountDownLatch) KeyStore(java.security.KeyStore) URI(java.net.URI) FileInputStream(java.io.FileInputStream) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) File(java.io.File) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 78 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.

the class TestWsWebSocketContainerSessionExpirySession method testSessionExpirySession.

@Test
public void testSessionExpirySession() 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();
    // Need access to implementation methods for configuring unit tests
    WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer();
    // 5 second timeout
    wsContainer.setDefaultMaxSessionIdleTimeout(5000);
    wsContainer.setProcessPeriod(1);
    EndpointA endpointA = new EndpointA();
    Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s1a.setMaxIdleTimeout(3000);
    Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s2a.setMaxIdleTimeout(6000);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s3a.setMaxIdleTimeout(9000);
    // Check all three sessions are open
    Set<Session> setA = s3a.getOpenSessions();
    int expected = 3;
    while (expected > 0) {
        Assert.assertEquals(expected, getOpenCount(setA));
        int count = 0;
        while (getOpenCount(setA) == expected && count < 50) {
            count++;
            Thread.sleep(100);
        }
        expected--;
    }
    Assert.assertEquals(0, getOpenCount(setA));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) EndpointA(org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 79 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat by apache.

the class TestEncodingDecoding method testBatchedEndPoints.

@Test
public void testBatchedEndPoints() throws Exception {
    // Set up utility classes
    BatchedServer server = new BatchedServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(BatchedServer.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();
    StringClient client = new StringClient();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_BATCHED_EP);
    Session session = wsContainer.connectToServer(client, uri);
    session.getBasicRemote().sendText(MESSAGE_ONE);
    // Should not take very long
    int i = 0;
    while (i++ < WAIT_LOOPS) {
        if (server.received.size() > 0 && client.received.size() > 1) {
            break;
        }
        i++;
        Thread.sleep(WAIT_DELAY);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(2, client.received.size());
    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE, server.received.peek());
    session.close();
    Assert.assertNull(server.t);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 80 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet 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 < WAIT_LOOPS) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(WAIT_DELAY);
    }
    // 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(jakarta.websocket.WebSocketContainer) URI(java.net.URI) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

DefaultServlet (org.apache.catalina.servlets.DefaultServlet)93 Context (org.apache.catalina.Context)91 Tomcat (org.apache.catalina.startup.Tomcat)91 Test (org.junit.Test)70 URI (java.net.URI)59 Session (jakarta.websocket.Session)39 WebSocketContainer (jakarta.websocket.WebSocketContainer)36 Session (javax.websocket.Session)30 WebSocketContainer (javax.websocket.WebSocketContainer)27 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)27 CountDownLatch (java.util.concurrent.CountDownLatch)24 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)24 ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)23 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)19 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)19 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)18 Endpoint (jakarta.websocket.Endpoint)15 ServerEndpoint (jakarta.websocket.server.ServerEndpoint)15 ServerEndpoint (javax.websocket.server.ServerEndpoint)14 Endpoint (javax.websocket.Endpoint)13