Search in sources :

Example 1 with DefaultServlet

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

the class TestWsWebSocketContainer 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) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) 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) Test(org.junit.Test)

Example 2 with DefaultServlet

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

the class TestWsWebSocketContainer method doTestWriteTimeoutClient.

private void doTestWriteTimeoutClient(boolean setTimeoutOnContainer) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(BlockingConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Set the async timeout
    if (setTimeoutOnContainer) {
        wsContainer.setAsyncSendTimeout(TIMEOUT_MS);
    }
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + BlockingConfig.PATH));
    if (!setTimeoutOnContainer) {
        wsSession.getAsyncRemote().setSendTimeout(TIMEOUT_MS);
    }
    long lastSend = 0;
    // Should send quickly until the network buffers fill up and then block
    // until the timeout kicks in
    Exception exception = null;
    try {
        while (true) {
            lastSend = System.currentTimeMillis();
            Future<Void> f = wsSession.getAsyncRemote().sendBinary(ByteBuffer.wrap(MESSAGE_BINARY_4K));
            f.get();
        }
    } catch (Exception e) {
        exception = e;
    }
    long timeout = System.currentTimeMillis() - lastSend;
    // Clear the server side block and prevent further blocks to allow the
    // server to shutdown cleanly
    BlockingPojo.clearBlock();
    // Close the client session, primarily to allow the
    // BackgroundProcessManager to shut down.
    wsSession.close();
    String msg = "Time out was [" + timeout + "] ms";
    // Check correct time passed
    Assert.assertTrue(msg, timeout >= TIMEOUT_MS - MARGIN);
    // Check the timeout wasn't too long
    Assert.assertTrue(msg, timeout < TIMEOUT_MS * 2);
    Assert.assertNotNull(exception);
}
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) DeploymentException(javax.websocket.DeploymentException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Session(javax.websocket.Session)

Example 3 with DefaultServlet

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

the class TestWsWebSocketContainer method testSessionExpiryContainer.

@Test
public void testSessionExpiryContainer() 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();
    connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    // Check all three sessions are open
    Set<Session> setA = s3a.getOpenSessions();
    Assert.assertEquals(3, setA.size());
    int count = 0;
    boolean isOpen = true;
    while (isOpen && count < 8) {
        count++;
        Thread.sleep(1000);
        isOpen = false;
        for (Session session : setA) {
            if (session.isOpen()) {
                isOpen = true;
                break;
            }
        }
    }
    if (isOpen) {
        for (Session session : setA) {
            if (session.isOpen()) {
                System.err.println("Session with ID [" + session.getId() + "] is open");
            }
        }
        Assert.fail("There were open sessions");
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) 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) Test(org.junit.Test)

Example 4 with DefaultServlet

use of org.apache.catalina.servlets.DefaultServlet in project ofbiz-framework by apache.

the class SeoControlServlet method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String uri = URLEncoder.encode(request.getRequestURI(), "UTF-8");
    if (request.getAttribute(REQUEST_IN_ALLOW_LIST) != null || request.getAttribute("_jsp_" + uri) != null) {
        if (request.getRequestURI().toLowerCase(Locale.getDefault()).endsWith(".jsp") || request.getRequestURI().toLowerCase(Locale.getDefault()).endsWith(".jspx")) {
            JspServlet jspServlet = new JspServlet();
            jspServlet.init(this.getServletConfig());
            jspServlet.service(request, response);
        } else {
            DefaultServlet defaultServlet = new DefaultServlet();
            defaultServlet.init(this.getServletConfig());
            defaultServlet.service(request, response);
        }
        return;
    }
    super.doGet(request, response);
}
Also used : JspServlet(org.apache.jasper.servlet.JspServlet) DefaultServlet(org.apache.catalina.servlets.DefaultServlet)

Example 5 with DefaultServlet

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

the class TestWsWebSocketContainer method doTestWriteTimeoutServer.

private void doTestWriteTimeoutServer(boolean setTimeoutOnContainer) throws Exception {
    // This will never work for BIO
    Assume.assumeFalse("Skipping test. This feature will never work for BIO connector.", getProtocol().equals(Http11Protocol.class.getName()));
    /*
         * 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.addServletMapping("/", "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)

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