Java Code Examples for javax.servlet.http.HttpServletRequest#getAsyncContext()

The following examples show how to use javax.servlet.http.HttpServletRequest#getAsyncContext() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("AsyncErrorPageGet-");

    final AsyncContext ctxt = req.getAsyncContext();

    switch(mode) {
        case COMPLETE:
            TestAsyncContextImpl.track("Complete-");
            ctxt.complete();
            break;
        case DISPATCH:
            TestAsyncContextImpl.track("Dispatch-");
            ctxt.dispatch("/error/nonasync");
            break;
        case NO_COMPLETE:
            TestAsyncContextImpl.track("NoOp-");
            break;
        default:
            // Impossible
            break;
    }
}
 
Example 2
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("AsyncErrorPageGet-");

    final AsyncContext ctxt = req.getAsyncContext();

    switch(mode) {
        case COMPLETE:
            TestAsyncContextImpl.track("Complete-");
            ctxt.complete();
            break;
        case DISPATCH:
            TestAsyncContextImpl.track("Dispatch-");
            ctxt.dispatch("/error/nonasync");
            break;
        case NO_COMPLETE:
            TestAsyncContextImpl.track("NoOp-");
            break;
        default:
            // Impossible
            break;
    }
}
 
Example 3
Source Project: tomcatsrc   File: TestAsyncContextImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    TestAsyncContextImpl.track("AsyncErrorPageGet-");

    final AsyncContext ctxt = req.getAsyncContext();

    switch(mode) {
        case COMPLETE:
            TestAsyncContextImpl.track("Complete-");
            ctxt.complete();
            break;
        case DISPATCH:
            TestAsyncContextImpl.track("Dispatch-");
            ctxt.dispatch("/error/nonasync");
            break;
        case NO_COMPLETE:
            TestAsyncContextImpl.track("NoOp-");
            break;
        default:
            // Impossible
            break;
    }
}
 
Example 4
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException {
  // only interested in http requests
  if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
    HttpServletRequest httpReq = (HttpServletRequest) request;
    HttpServletResponse httpResp = (HttpServletResponse) response;

    HttpRequestContext context = handler.handleStart(httpReq, httpReq);
    OcHttpServletListener listener = new OcHttpServletListener(handler, context);
    httpReq.setAttribute(OcHttpServletUtil.OPENCENSUS_SERVLET_LISTENER, listener);

    int length = httpReq.getContentLength();
    if (length > 0) {
      handler.handleMessageReceived(context, length);
    }

    Scope scope = Tracing.getTracer().withSpan(handler.getSpanFromContext(context));
    try {
      chain.doFilter(httpReq, httpResp);
    } finally {
      scope.close();
    }

    if (httpReq.isAsyncStarted()) {
      AsyncContext async = httpReq.getAsyncContext();
      async.addListener(listener, httpReq, httpResp);
    } else {
      OcHttpServletUtil.recordMessageSentEvent(handler, context, httpResp);
      handler.handleEnd(context, httpReq, httpResp, null);
    }
  } else {
    // pass request through unchanged
    chain.doFilter(request, response);
  }
}
 
Example 5
static AsyncContext switchRequestToAsyncIfNeeded(final HttpServletRequest request){
    if (request.isAsyncStarted()){
        if (log.isDebugEnabled()) log.debug("Async context already started. Return it");
        return request.getAsyncContext();
    }else{
        if (log.isDebugEnabled()) log.info("Start async context and return it.");
        return request.startAsync();
    }
}
 
Example 6
private AsyncContext getAsyncContext(HttpServletRequest req) {
    if(req.isAsyncStarted()) {
        return req.getAsyncContext();
    } else {
        return req.startAsync();
    }
}
 
Example 7
Source Project: cxf   File: SseEventSinkContextProvider.java    License: Apache License 2.0 5 votes vote down vote up
protected SseEventSink createSseEventSink(final HttpServletRequest request,
        final MessageBodyWriter<OutboundSseEvent> writer,
        final AsyncResponse async, final Integer bufferSize) {
    if (bufferSize != null) {
        return new SseEventSinkImpl(writer, async, request.getAsyncContext(), bufferSize);
    } else {        
        return new SseEventSinkImpl(writer, async, request.getAsyncContext());
    }
}
 
Example 8
/**
 * {@inheritDoc}
 * <p>
 * Overridden to provide more information about the error. The
 * implementation is derived from the jetty 9.1.4 implementation of the
 * method in the base {@link ProxyServlet} class, but logs @ ERROR so we can
 * see more about the underlying problem.
 * 
 * @see <a href="http://trac.blazegraph.com/ticket/941" > HA LBS Gateway errors
 *      under heavy load </a>
 * 
 *      TODO jetty 9.2 provides a fully asynchronous proxy servlet. We will
 *      wind up replacing our base class with that implementation soon,
 *      probably for the 1.3.2 release. Until then, this will provide
 *      additional diagnoistic information about the root causes when there
 *      is a gateway error (proxying fails). If we can find some patterns to
 *      these failures, then it would be useful to recharacterize more of
 *      them to encourage the client to retry the request. Those semantics
 *      are not really available for 502 (Bad Gateway). They are more a
 *      appropriate for both 503 (Service Unavailable - temporary overload),
 *      and 504 (Gateway Timeout). 503 might be the best choice if there is
 *      not an explicit timeout and the root cause does not clearly indicate
 *      a durable problem with the target host.
 */
@Override
protected void onProxyResponseFailure(//
        final HttpServletRequest request,//
        final HttpServletResponse response,//
        final Response proxyResponse,//
        final Throwable failure) {
    
    log.error(getRequestId(request) + " proxying failed: " + request, failure);
    if (!response.isCommitted())
    {
        if (failure instanceof TimeoutException)
            response.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
        else
            response.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
    }
    
    AsyncContext asyncContext = request.getAsyncContext();
    asyncContext.complete();
}
 
Example 9
@Override
protected void onResponseFailure(HttpServletRequest request, HttpServletResponse response, 
                                  Response proxyResponse, Throwable failure){
  //System.err.println("Response Failure!");
  this.setForwardingUrl();
  
  HttpClient c = null;
  try {
    c = this.createHttpClient();
  } catch (ServletException e1) {
    e1.printStackTrace();
  }
  
  final Request proxyRequest =  c.newRequest(this.forwardingUrl)
      .method(request.getMethod())
      .version(HttpVersion.fromString(request.getProtocol()));
  
  boolean hasContent = request.getContentLength() > 0 || request.getContentType() != null;
  for (Enumeration<String> headerNames = request.getHeaderNames(); headerNames.hasMoreElements();){
      String headerName = headerNames.nextElement();
      if (HttpHeader.TRANSFER_ENCODING.is(headerName))
          hasContent = true;
      for (Enumeration<String> headerValues = request.getHeaders(headerName); headerValues.hasMoreElements();){
          String headerValue = headerValues.nextElement();
          if (headerValue != null)
              proxyRequest.header(headerName, headerValue);
      }
  }

  // Add proxy headers
  addViaHeader(proxyRequest);
  addXForwardedHeaders(proxyRequest, request);

  final AsyncContext asyncContext = request.getAsyncContext();
  // We do not timeout the continuation, but the proxy request
  asyncContext.setTimeout(0);
  proxyRequest.timeout(getTimeout(), TimeUnit.MILLISECONDS);

  if (hasContent)
    try {
      proxyRequest.content(proxyRequestContent(proxyRequest, request));
    } catch (IOException e) {
      e.printStackTrace();
    }

  customizeProxyRequest(proxyRequest, request);

  proxyRequest.send(new ProxyResponseListener(request, response));
}