Example usage for javax.servlet.http HttpServletRequest getAsyncContext

List of usage examples for javax.servlet.http HttpServletRequest getAsyncContext

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getAsyncContext.

Prototype

public AsyncContext getAsyncContext();

Source Link

Document

Gets the AsyncContext that was created or reinitialized by the most recent invocation of #startAsync or #startAsync(ServletRequest,ServletResponse) on this request.

Usage

From source file:org.synchronoss.cloud.nio.multipart.example.web.MultipartController.java

static AsyncContext switchRequestToAsyncIfNeeded(final HttpServletRequest request) {
    if (request.isAsyncStarted()) {
        if (log.isDebugEnabled())
            log.debug("Async context already started. Return it");
        return request.getAsyncContext();
    } else {/*from   ww  w .  j av  a 2 s.co m*/
        if (log.isDebugEnabled())
            log.info("Start async context and return it.");
        return request.startAsync();
    }
}

From source file:com.boylesoftware.web.impl.view.DispatchViewSender.java

@Override
public void send(final String viewId, final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException {

    if (request.isAsyncStarted()) {
        if (this.log.isDebugEnabled())
            this.log.debug("dispatching to " + viewId + " using async context");
        request.getAsyncContext().dispatch(viewId);
    } else {/*  www  . ja v a2 s  .  co  m*/
        if (this.log.isDebugEnabled())
            this.log.debug("dispatching to " + viewId + " using request dispatcher");
        request.getRequestDispatcher(viewId).forward(request, response);
    }
}

From source file:com.kurento.kmf.content.internal.base.AbstractHttpContentSession.java

private void answerActivateMediaRequest4SimpleHttpConfigurationWithTunnel(String url) {
    try {/*w  ww .  j  a va  2  s.  co m*/
        HttpServletResponse response = (HttpServletResponse) initialAsyncCtx.getResponse();
        final HttpServletRequest request = (HttpServletRequest) initialAsyncCtx.getRequest();
        getLogger().info("Activating tunneling proxy to " + url);
        tunnellingProxyFuture = proxy.tunnelTransaction(request, response, url, new StreamingProxyListener() {

            @Override
            public void onProxySuccess() {
                tunnellingProxyFuture = null;
                // Parameters no matter, no answer will be sent
                // given that we are already in ACTIVE state
                terminate(0, "");
                request.getAsyncContext().complete();
            }

            @Override
            public void onProxyError(String message, int errorCode) {
                tunnellingProxyFuture = null;
                // Parameters no matter, no answer will be sent
                // given that we are already in ACTIVE state
                terminate(errorCode, message);
                request.getAsyncContext().complete();
            }
        });

    } catch (Throwable t) {
        throw new KurentoMediaFrameworkException(t.getMessage(), t, 20013);
    } finally {
        initialAsyncCtx = null;
    }
}

From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContext.java

protected AsyncContext saveResponse(final BoshRequest boshRequest, final BoshResponse boshResponse) {
    if (boshResponse == null)
        throw new IllegalArgumentException("boshResponse cannot be null");
    final HttpServletRequest request = boshRequest.getHttpServletRequest();
    final AsyncContext asyncContext = request.getAsyncContext();
    request.setAttribute(BOSH_RESPONSE_ATTRIBUTE, boshResponse);
    return asyncContext;
}

From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java

@Test
public void testWrite0() {
    HttpServletRequest httpServletRequest = mocksControl.createMock(HttpServletRequest.class);
    AsyncContext asyncContext = mocksControl.createMock(AsyncContext.class);
    expect(httpServletRequest.startAsync()).andReturn(asyncContext);
    expectLastCall().atLeastOnce();//ww  w.  ja v a  2  s .  c  om
    expect(httpServletRequest.getAsyncContext()).andReturn(asyncContext);
    expectLastCall().atLeastOnce();
    asyncContext.setTimeout(anyLong());
    asyncContext.dispatch();
    expectLastCall().atLeastOnce();
    httpServletRequest.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>notNull());
    asyncContext.addListener(EasyMock.<AsyncListener>anyObject());
    Capture<BoshResponse> captured = new Capture<BoshResponse>();
    httpServletRequest.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE), EasyMock.<BoshResponse>capture(captured));
    mocksControl.replay();

    BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null,
            inactivityChecker);
    Stanza body = BoshStanzaUtils.EMPTY_BOSH_RESPONSE;
    boshBackedSessionContext.insertRequest(new BoshRequest(httpServletRequest, body, 1L));
    boshBackedSessionContext.writeBoshResponse(body);
    mocksControl.verify();

    BoshResponse boshResponse = captured.getValue();
    assertEquals(BoshServlet.XML_CONTENT_TYPE, boshResponse.getContentType());
    assertEquals(new Renderer(body).getComplete(), new String(boshResponse.getContent()));
}

From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java

@Test
public void testRequestExpired() throws IOException {
    Stanza emtpyStanza = BoshStanzaUtils.EMPTY_BOSH_RESPONSE;

    // addRequest
    HttpServletRequest httpServletRequest = mocksControl.createMock(HttpServletRequest.class);
    AsyncContext asyncContext = mocksControl.createMock(AsyncContext.class);
    expect(httpServletRequest.startAsync()).andReturn(asyncContext).atLeastOnce();
    expect(httpServletRequest.getAsyncContext()).andReturn(asyncContext).atLeastOnce();
    asyncContext.setTimeout(anyLong());//  w ww.j av  a2  s  . c  o m
    httpServletRequest.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>notNull());

    expect(asyncContext.getRequest()).andReturn(httpServletRequest).atLeastOnce();
    asyncContext.dispatch();
    expectLastCall().atLeastOnce();

    Capture<AsyncListener> listenerCaptured = new Capture<AsyncListener>();
    asyncContext.addListener(EasyMock.<AsyncListener>capture(listenerCaptured));

    AsyncEvent asyncEvent = mocksControl.createMock(AsyncEvent.class);

    BoshRequest br = new BoshRequest(httpServletRequest, emtpyStanza, 1L);

    // requestExpired
    expect(httpServletRequest.getAttribute(BOSH_REQUEST_ATTRIBUTE)).andReturn(br);
    Capture<BoshResponse> responseCaptured = new Capture<BoshResponse>();
    httpServletRequest.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE),
            EasyMock.<BoshResponse>capture(responseCaptured));

    // write0
    mocksControl.replay();
    BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null,
            inactivityChecker);

    boshBackedSessionContext.insertRequest(br);
    listenerCaptured.getValue().onTimeout(asyncEvent);
    mocksControl.verify();

    assertEquals(new Renderer(emtpyStanza).getComplete(), new String(responseCaptured.getValue().getContent()));
    assertEquals(BoshServlet.XML_CONTENT_TYPE, responseCaptured.getValue().getContentType());
}

From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java

@Test
public void testAddRequest() {
    // addRequest
    HttpServletRequest httpServletRequest1 = mocksControl.createMock(HttpServletRequest.class);
    HttpServletRequest httpServletRequest2 = mocksControl.createMock(HttpServletRequest.class);
    AsyncContext asyncContext1 = mocksControl.createMock(AsyncContext.class);
    AsyncContext asyncContext2 = mocksControl.createMock(AsyncContext.class);
    BoshStanzaUtils boshStanzaUtils = mocksControl.createMock(BoshStanzaUtils.class);

    expect(httpServletRequest1.startAsync()).andReturn(asyncContext1).atLeastOnce();
    expect(httpServletRequest1.getAsyncContext()).andReturn(asyncContext1).atLeastOnce();

    expect(httpServletRequest2.startAsync()).andReturn(asyncContext2).atLeastOnce();
    expect(httpServletRequest2.getAsyncContext()).andReturn(asyncContext2).anyTimes();

    asyncContext1.setTimeout(anyLong());
    Capture<BoshRequest> br1 = new Capture<BoshRequest>();
    httpServletRequest1.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>capture(br1));

    asyncContext2.setTimeout(anyLong());
    Capture<BoshRequest> br2 = new Capture<BoshRequest>();
    httpServletRequest2.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>capture(br2));

    asyncContext1.addListener(EasyMock.<AsyncListener>anyObject());
    asyncContext2.addListener(EasyMock.<AsyncListener>anyObject());

    asyncContext1.dispatch();//from   www  .  j  a v a2s  . c o m
    expectLastCall().atLeastOnce();

    Stanza body = BoshStanzaUtils.EMPTY_BOSH_RESPONSE;

    // write0
    Capture<BoshResponse> captured = new Capture<BoshResponse>();
    httpServletRequest1.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE), EasyMock.<BoshResponse>capture(captured));

    mocksControl.replay();
    BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null,
            inactivityChecker);

    boshBackedSessionContext.setHold(2);
    // consecutive writes with RID 1 and 2
    long maxRID = 2L;
    boshBackedSessionContext.insertRequest(new BoshRequest(httpServletRequest1, body, 1L));
    boshBackedSessionContext.insertRequest(new BoshRequest(httpServletRequest2, body, maxRID));
    boshBackedSessionContext.writeBoshResponse(body);
    mocksControl.verify();

    assertEquals(httpServletRequest1, br1.getValue().getHttpServletRequest());
    assertEquals(httpServletRequest2, br2.getValue().getHttpServletRequest());

    // expect ack for newest/largest RID
    final Stanza ackedResponse = BoshStanzaUtils.addAttribute(body, "ack", Long.toString(maxRID));
    assertEquals(new Renderer(ackedResponse).getComplete(), new String(captured.getValue().getContent()));
    assertEquals(BoshServlet.XML_CONTENT_TYPE, captured.getValue().getContentType());
}

From source file:org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContextTest.java

@Test
public void testAddRequestWithDelayedResponses() {
    HttpServletRequest httpServletRequest = mocksControl.createMock(HttpServletRequest.class);
    AsyncContext asyncContext = mocksControl.createMock(AsyncContext.class);
    expect(httpServletRequest.startAsync()).andReturn(asyncContext).atLeastOnce();
    expect(httpServletRequest.getAsyncContext()).andReturn(asyncContext).atLeastOnce();
    asyncContext.setTimeout(anyLong());//from  w w w.j  av  a 2  s.co m
    httpServletRequest.setAttribute(eq(BOSH_REQUEST_ATTRIBUTE), EasyMock.<BoshRequest>notNull());

    asyncContext.addListener(EasyMock.<AsyncListener>anyObject());

    asyncContext.dispatch();
    expectLastCall().atLeastOnce();

    Stanza body = BoshStanzaUtils.createBoshStanzaBuilder().startInnerElement("presence").endInnerElement()
            .build();

    Capture<BoshResponse> captured = new Capture<BoshResponse>();
    httpServletRequest.setAttribute(eq(BOSH_RESPONSE_ATTRIBUTE), EasyMock.<BoshResponse>capture(captured));

    mocksControl.replay();

    BoshBackedSessionContext boshBackedSessionContext = new BoshBackedSessionContext(serverRuntimeContext, null,
            inactivityChecker);
    boshBackedSessionContext.writeBoshResponse(body); // queued for merging
    boshBackedSessionContext.writeBoshResponse(body); // queued for merging
    boshBackedSessionContext.writeBoshResponse(body); // queued for merging
    boshBackedSessionContext.insertRequest(new BoshRequest(httpServletRequest, body, 1L));

    mocksControl.verify();

    final String mergedAllBodiesStanza = new String(captured.getValue().getContent());
    assertEquals(3, StringUtils.countMatches(mergedAllBodiesStanza, "<presence"));
}

From source file:org.ireland.jnetty.webapp.RequestDispatcherImpl.java

private void doDispatch(HttpServletRequest request, HttpServletResponse response, HttpInvocation invocation)
        throws ServletException, IOException {

    // ,responsebuffer,TODO: need resetBuffer()?
    response.resetBuffer();//w w  w .  j  a v a2 s .  c  o  m

    // Set the invocation into HttpServlerRequestImpl
    if (request instanceof HttpServletRequestImpl) {
        ((HttpServletRequestImpl) request).setInvocation(_dispatchInvocation);
        ((HttpServletRequestImpl) request).setDispatcherType(DispatcherType.REQUEST);
    }

    boolean isValid = false;

    try {

        invocation.getFilterChainInvocation().service(request, response);
        isValid = true;
    } finally {
        if (request.getAsyncContext() != null) {
            // An async request was started during the forward, don't close the
            // response as it may be written to during the async handling
            return;
        }

        // server/106r, ioc/0310
        if (isValid) {
            finishResponse(response);
        }
    }
}

From source file:org.ireland.jnetty.webapp.RequestDispatcherImpl.java

private void doForward(HttpServletRequest request, HttpServletResponse response, HttpInvocation invocation)
        throws ServletException, IOException {

    // Reset any output that has been buffered, but keep headers/cookies
    response.resetBuffer(); // Servlet-3_1-PFD 9.4

    //Wrap the request
    ForwardRequest wrequest = new ForwardRequest(request, response, invocation);

    // If we have already been forwarded previously, then keep using the established
    // original value. Otherwise, this is the first forward and we need to establish the values.
    // Note: the established value on the original request for pathInfo and
    // for queryString is allowed to be null, but cannot be null for the other values.
    if (request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI) == null) {
        // ??,/*from  w w  w  .j  a va  2  s. c o m*/
        wrequest.setAttribute(RequestDispatcher.FORWARD_REQUEST_URI, request.getRequestURI());

        wrequest.setAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH, request.getContextPath());
        wrequest.setAttribute(RequestDispatcher.FORWARD_SERVLET_PATH, request.getServletPath());
        wrequest.setAttribute(RequestDispatcher.FORWARD_PATH_INFO, request.getPathInfo());
        wrequest.setAttribute(RequestDispatcher.FORWARD_QUERY_STRING, request.getQueryString());
    }

    boolean isValid = false;

    try {

        invocation.getFilterChainInvocation().service(wrequest, response);

        isValid = true;
    } finally {
        if (request.getAsyncContext() != null) {
            // An async request was started during the forward, don't close the
            // response as it may be written to during the async handling
            return;
        }

        // server/106r, ioc/0310
        if (isValid) {
            finishResponse(response);
        }
    }
}