Search in sources :

Example 1 with AsyncContext

use of jakarta.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.cpr.Action}
 * @param req    the {@link AtmosphereRequest}
 * @param res    the {@link AtmosphereResponse}
 */
private void suspend(Action action, AtmosphereRequest req, AtmosphereResponse res) {
    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(jakarta.servlet.AsyncContext)

Example 2 with AsyncContext

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

the class AtmosphereRequestImpl method startAsync.

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

Example 3 with AsyncContext

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

the class AtmosphereRequestImpl method getAsyncContext.

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

Example 4 with AsyncContext

use of jakarta.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);
    given(this.request.startAsync(this.request, this.response)).willReturn(asyncContext);
    Runnable runnable = () -> {
    };
    wrappedRequest().startAsync(this.request, this.response).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)

Example 5 with AsyncContext

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

the class WebLogicRequestUpgradeStrategy method handleSuccess.

@Override
protected void handleSuccess(HttpServletRequest request, HttpServletResponse response, UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException {
    response.setStatus(upgradeResponse.getStatus());
    upgradeResponse.getHeaders().forEach((key, value) -> response.addHeader(key, Utils.getHeaderFromList(value)));
    AsyncContext asyncContext = request.startAsync();
    asyncContext.setTimeout(-1L);
    Object nativeRequest = getNativeRequest(request);
    BeanWrapper beanWrapper = new BeanWrapperImpl(nativeRequest);
    Object httpSocket = beanWrapper.getPropertyValue("connection.connectionHandler.rawConnection");
    Object webSocket = webSocketHelper.newInstance(request, httpSocket);
    webSocketHelper.upgrade(webSocket, httpSocket, request.getServletContext());
    response.flushBuffer();
    boolean isProtected = request.getUserPrincipal() != null;
    Writer servletWriter = servletWriterHelper.newInstance(webSocket, isProtected);
    Connection connection = upgradeInfo.createConnection(servletWriter, noOpCloseListener);
    new BeanWrapperImpl(webSocket).setPropertyValue("connection", connection);
    new BeanWrapperImpl(servletWriter).setPropertyValue("connection", connection);
    webSocketHelper.registerForReadEvent(webSocket);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Connection(org.glassfish.tyrus.spi.Connection) AsyncContext(jakarta.servlet.AsyncContext) Writer(org.glassfish.tyrus.spi.Writer)

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