Search in sources :

Example 11 with AsyncContext

use of jakarta.servlet.AsyncContext in project tomcat by apache.

the class TestAsyncContextStateChanges method testAsync.

@Test
public void testAsync() throws Exception {
    dispatch = false;
    servletRequest = null;
    asyncContext = null;
    // Initialise tracking fields
    failed.set(true);
    servletStartLatch = new CountDownLatch(1);
    threadCompleteLatch = new CountDownLatch(1);
    clientDisconnectLatch = new CountDownLatch(1);
    endLatch = new CountDownLatch(1);
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    AsyncServlet bug63816Servlet = new AsyncServlet();
    Wrapper wrapper = Tomcat.addServlet(ctx, "bug63816Servlet", bug63816Servlet);
    wrapper.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/*", "bug63816Servlet");
    tomcat.start();
    Client client = new Client();
    client.setPort(getPort());
    client.setRequest(new String[] { "GET / HTTP/1.1" + SimpleHttpClient.CRLF + "Host: localhost:" + SimpleHttpClient.CRLF + SimpleHttpClient.CRLF });
    client.connect();
    client.sendRequest();
    // Wait for Servlet to start processing request
    servletStartLatch.await();
    if (asyncEnd.isError()) {
        client.disconnect();
        clientDisconnectLatch.countDown();
        try {
            endLatch.await();
        } catch (InterruptedException e) {
        // Ignore
        }
    } else {
        client.setUseContentLength(true);
        client.readResponse(true);
    }
    Assert.assertFalse(failed.get());
}
Also used : Context(org.apache.catalina.Context) AsyncContext(jakarta.servlet.AsyncContext) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) CountDownLatch(java.util.concurrent.CountDownLatch) SimpleHttpClient(org.apache.catalina.startup.SimpleHttpClient) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 12 with AsyncContext

use of jakarta.servlet.AsyncContext in project tomcat by apache.

the class Async2 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                Thread.currentThread().setName("Async2-Thread");
                log.info("Putting AsyncThread to sleep");
                Thread.sleep(2 * 1000);
                log.info("Writing data.");
                Date date = new Date(System.currentTimeMillis());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
                actx.getResponse().getWriter().write("Output from background thread. Time: " + sdf.format(date) + "\n");
                actx.complete();
            } catch (InterruptedException x) {
                log.error("Async2", x);
            } catch (IllegalStateException x) {
                log.error("Async2", x);
            } catch (IOException x) {
                log.error("Async2", x);
            }
        }
    };
    Thread t = new Thread(run);
    t.start();
}
Also used : AsyncContext(jakarta.servlet.AsyncContext) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 13 with AsyncContext

use of jakarta.servlet.AsyncContext in project tomcat by apache.

the class Async1 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    Runnable run = new Runnable() {

        @Override
        public void run() {
            try {
                String path = "/jsp/async/async1.jsp";
                Thread.currentThread().setName("Async1-Thread");
                log.info("Putting AsyncThread to sleep");
                Thread.sleep(2 * 1000);
                log.info("Dispatching to " + path);
                actx.dispatch(path);
            } catch (InterruptedException x) {
                log.error("Async1", x);
            } catch (IllegalStateException x) {
                log.error("Async1", x);
            }
        }
    };
    Thread t = new Thread(run);
    t.start();
}
Also used : AsyncContext(jakarta.servlet.AsyncContext)

Example 14 with AsyncContext

use of jakarta.servlet.AsyncContext in project tomcat by apache.

the class Async3 method service.

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext actx = req.startAsync();
    actx.setTimeout(30 * 1000);
    actx.dispatch("/jsp/async/async3.jsp");
}
Also used : AsyncContext(jakarta.servlet.AsyncContext)

Example 15 with AsyncContext

use of jakarta.servlet.AsyncContext in project spring-security by spring-projects.

the class SecurityContextHolderAwareRequestFilterTests method startAsyncStart.

@Test
public void startAsyncStart() throws Exception {
    ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password", "ROLE_USER");
    context.setAuthentication(expectedAuth);
    SecurityContextHolder.setContext(context);
    AsyncContext asyncContext = mock(AsyncContext.class);
    given(this.request.startAsync()).willReturn(asyncContext);
    Runnable runnable = () -> {
    };
    wrappedRequest().startAsync().start(runnable);
    verifyZeroInteractions(this.authenticationManager, this.logoutHandler);
    verify(asyncContext).start(runnableCaptor.capture());
    DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
    assertThat(ReflectionTestUtils.getField(wrappedRunnable, "delegateSecurityContext")).isEqualTo(context);
    assertThat(ReflectionTestUtils.getField(wrappedRunnable, "delegate"));
}
Also used : DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) SecurityContext(org.springframework.security.core.context.SecurityContext) AsyncContext(jakarta.servlet.AsyncContext) DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncContext (jakarta.servlet.AsyncContext)19 Test (org.junit.jupiter.api.Test)3 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)3 DelegatingSecurityContextRunnable (org.springframework.security.concurrent.DelegatingSecurityContextRunnable)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Context (org.apache.catalina.Context)2 Tomcat (org.apache.catalina.startup.Tomcat)2 AsyncListener (jakarta.servlet.AsyncListener)1 ServletException (jakarta.servlet.ServletException)1 ServletInputStream (jakarta.servlet.ServletInputStream)1 ServletOutputStream (jakarta.servlet.ServletOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 URI (java.net.URI)1