Search in sources :

Example 51 with AsyncContext

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

the class SecurityContextHolderAwareRequestFilterTests method startAsyncWithRequestResponseStart.

@Test
public void startAsyncWithRequestResponseStart() 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);
    when(this.request.startAsync(this.request, this.response)).thenReturn(asyncContext);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
        }
    };
    wrappedRequest().startAsync(this.request, this.response).start(runnable);
    verifyZeroInteractions(this.authenticationManager, this.logoutHandler);
    verify(asyncContext).start(runnableCaptor.capture());
    DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
    assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, "delegateSecurityContext")).isEqualTo(context);
    assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, "delegate")).isEqualTo(runnable);
}
Also used : DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) SecurityContext(org.springframework.security.core.context.SecurityContext) AsyncContext(javax.servlet.AsyncContext) DelegatingSecurityContextRunnable(org.springframework.security.concurrent.DelegatingSecurityContextRunnable) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 52 with AsyncContext

use of javax.servlet.AsyncContext in project atmosphere by Atmosphere.

the class Servlet30CometSupport method suspend.

/**
     * Suspend the connection by invoking {@link AtmosphereRequestImpl#startAsync()}
     *
     * @param action The {@link org.atmosphere.runtime.Action}
     * @param req    the {@link AtmosphereRequest}
     * @param res    the {@link AtmosphereResponse}
     * @throws java.io.IOException
     * @throws javax.servlet.ServletException
     */
private void suspend(Action action, AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException {
    if (!req.isAsyncStarted() && !Utils.webSocketEnabled(req)) {
        AsyncContext asyncContext = req.startAsync(req, res);
        asyncContext.addListener(new CometListener(this, res.uuid()));
        // Do nothing except setting the times out
        if (action.timeout() != -1) {
            asyncContext.setTimeout(action.timeout());
        } else {
            // Jetty 8 does something really weird if you set it to
            // Long.MAX_VALUE, which is to resume automatically.
            asyncContext.setTimeout(Integer.MAX_VALUE);
        }
        req.setAttribute(FrameworkConfig.ASYNC_CONTEXT, asyncContext);
    }
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 53 with AsyncContext

use of javax.servlet.AsyncContext in project atmosphere by Atmosphere.

the class AtmosphereRequestImpl method startAsync.

@Override
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
    AsyncContext ac;
    if (AtmosphereResource.TRANSPORT.WEBSOCKET == resource().transport()) {
        noopsAsyncContextStarted = true;
        ac = new NoOpsAsyncContext(request, response);
    } else {
        ac = b.request.startAsync(request, response);
    }
    return isCompletionAware() ? new CompletionAwareAsyncContext(ac, (CompletionAware) resource().getResponse()) : ac;
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 54 with AsyncContext

use of javax.servlet.AsyncContext in project atmosphere by Atmosphere.

the class AtmosphereResourceTest method verifyTestCompletionAwareForGetAsync.

private void verifyTestCompletionAwareForGetAsync(boolean aware) throws IOException {
    if (aware) {
        framework.addInitParameter(ApplicationConfig.RESPONSE_COMPLETION_AWARE, "true");
    }
    AtmosphereRequest request = AtmosphereRequestImpl.newInstance();
    AtmosphereResponseImpl response = mock(AtmosphereResponseImpl.class);
    AtmosphereResourceImpl res = new AtmosphereResourceImpl();
    res.initialize(framework.getAtmosphereConfig(), framework.getBroadcasterFactory().get(), request, response, null, null);
    res.transport(AtmosphereResource.TRANSPORT.WEBSOCKET);
    request.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, res);
    AsyncContext ac = request.getAsyncContext();
    verify(response, times(0)).onComplete();
    ac.complete();
    verify(response, times(aware ? 1 : 0)).onComplete();
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 55 with AsyncContext

use of javax.servlet.AsyncContext in project wildfly by wildfly.

the class AsyncServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    AtomicInteger value = (AtomicInteger) session.getAttribute(ATTRIBUTE);
    if (value == null) {
        value = new AtomicInteger(0);
        session.setAttribute(ATTRIBUTE, value);
    }
    AsyncContext context = request.startAsync(request, response);
    context.start(new AsyncTask(context));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpSession(javax.servlet.http.HttpSession) AsyncContext(javax.servlet.AsyncContext)

Aggregations

AsyncContext (javax.servlet.AsyncContext)120 IOException (java.io.IOException)61 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 ServletException (javax.servlet.ServletException)52 HttpServletResponse (javax.servlet.http.HttpServletResponse)50 Test (org.junit.Test)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 HttpServlet (javax.servlet.http.HttpServlet)32 InterruptedIOException (java.io.InterruptedIOException)24 ServletOutputStream (javax.servlet.ServletOutputStream)20 ReadListener (javax.servlet.ReadListener)19 ServletInputStream (javax.servlet.ServletInputStream)19 AsyncEvent (javax.servlet.AsyncEvent)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 AsyncListener (javax.servlet.AsyncListener)15 UncheckedIOException (java.io.UncheckedIOException)14 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)14 Request (org.eclipse.jetty.server.Request)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 WriteListener (javax.servlet.WriteListener)11