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

The following examples show how to use javax.servlet.http.HttpServletRequest#getAsyncContext() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 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 2
Source File: TestAsyncContextImpl.java    From Tomcat7.0.67 with 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 3
Source File: TestAsyncContextImpl.java    From tomcatsrc with 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
Source File: OcHttpServletFilter.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
@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
Source File: MultipartController.java    From nio-multipart with Apache License 2.0 5 votes vote down vote up
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
Source File: OpenshiftServlet.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
private AsyncContext getAsyncContext(HttpServletRequest req) {
    if(req.isAsyncStarted()) {
        return req.getAsyncContext();
    } else {
        return req.startAsync();
    }
}
 
Example 7
Source File: SseEventSinkContextProvider.java    From cxf with 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
Source File: HALoadBalancerServlet.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * {@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
Source File: CommandProxyServlet.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@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));
}