Search in sources :

Example 36 with DefaultServlet

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

the class TestWebSocketFrameClient method testConnectToRootEndpoint.

@Test
public void testConnectToRootEndpoint() 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");
    Context ctx2 = tomcat.addContext("/foo", null);
    ctx2.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx2, "default", new DefaultServlet());
    ctx2.addServletMappingDecoded("/", "default");
    tomcat.start();
    echoTester("", null);
    echoTester("/", null);
    // This will trigger a redirect so there will be 5 requests logged
    echoTester("/foo", null);
    echoTester("/foo/", null);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Test(org.junit.Test)

Example 37 with DefaultServlet

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

the class TestWebSocketFrameClient method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.ConfigInline.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // BZ 62596
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Dummy", Collections.singletonList(String.join("", Collections.nCopies(4000, "A"))));
            super.beforeRequest(headers);
        }
    }).build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterFirehoseServer.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(jakarta.websocket.WebSocketContainer) Configurator(jakarta.websocket.ClientEndpointConfig.Configurator) 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) Map(java.util.Map) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 38 with DefaultServlet

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

the class TestWebSocketFrameClient method testConnectToBasicEndpoint.

@Test
public void testConnectToBasicEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext(URI_PROTECTED, null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded("/");
    String utf8User = "test";
    // pound sign
    String utf8Pass = "123\u00A3";
    tomcat.addUser(utf8User, utf8Pass);
    tomcat.addRole(utf8User, ROLE);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctx.addConstraint(sc);
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("BASIC");
    ctx.setLoginConfig(lc);
    AuthenticatorBase basicAuthenticator = new org.apache.catalina.authenticator.BasicAuthenticator();
    ctx.getPipeline().addValve(basicAuthenticator);
    tomcat.start();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_USER_NAME, utf8User);
    clientEndpointConfig.getUserProperties().put(Constants.WS_AUTHENTICATION_PASSWORD, utf8Pass);
    echoTester(URI_PROTECTED, clientEndpointConfig);
}
Also used : Context(org.apache.catalina.Context) AuthenticatorBase(org.apache.catalina.authenticator.AuthenticatorBase) Tomcat(org.apache.catalina.startup.Tomcat) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection) Test(org.junit.Test)

Example 39 with DefaultServlet

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

the class TestEncodingDecoding method testMessagesEndPoints.

@Test
public void testMessagesEndPoints() throws Exception {
    // Set up utility classes
    MessagesServer server = new MessagesServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(MessagesServer.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_MESSAGES_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 40 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project tomcat 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.addServletMappingDecoded("/", "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<>(2);
    list.add("str1");
    list.add("str2");
    session.getBasicRemote().sendObject(list);
    // 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(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(jakarta.websocket.WebSocketContainer) ArrayList(java.util.ArrayList) 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