Java Code Examples for javax.servlet.AsyncEvent#getSuppliedRequest()

The following examples show how to use javax.servlet.AsyncEvent#getSuppliedRequest() . 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
public void onTimeout(AsyncEvent event) throws IOException {
    boolean expectedAsyncStarted = true;

    TestAsyncContextImpl.track("onTimeout-");
    if (completeOnTimeout){
        event.getAsyncContext().complete();
        expectedAsyncStarted = false;
    }
    if (dispatchUrl != null) {
        event.getAsyncContext().dispatch(dispatchUrl);
        expectedAsyncStarted = false;
    }

    ServletRequest req = event.getSuppliedRequest();
    asyncStartedCorrect = (expectedAsyncStarted == req.isAsyncStarted());
}
 
Example 2
Source File: JettyAsyncListener.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
private Throwable getThrowable(AsyncEvent asyncEvent) {
    try {
        if (asyncEvent.getThrowable() != null) {
            return asyncEvent.getThrowable();
        }
        // Jetty 8.x
        final ServletRequest request = asyncEvent.getSuppliedRequest();
        if (request != null) {
            final Object errorException = request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
            if (errorException instanceof Throwable) {
                return (Throwable) errorException;
            }
        }
    } catch (Exception ignored) {
    }
    return null;
}
 
Example 3
Source File: ServletRuntime.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public void onComplete(AsyncEvent e) {
  HttpServletRequest req = (HttpServletRequest) e.getSuppliedRequest();
  // Use package-private attribute to check if this hook was called redundantly
  Object sendHandled = req.getAttribute("brave.servlet.TracingFilter$SendHandled");
  if (sendHandled instanceof AtomicBoolean
      && ((AtomicBoolean) sendHandled).compareAndSet(false, true)) {
    HttpServletResponse res = (HttpServletResponse) e.getSuppliedResponse();

    HttpServerResponse response =
        brave.servlet.HttpServletResponseWrapper.create(req, res, e.getThrowable());
    handler.handleSend(response, span);
  } else {
    // TODO: None of our tests reach this condition. Make a concrete case that re-enters the
    // onComplete hook or remove the special case
  }
}
 
Example 4
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void onError(AsyncEvent event) throws IOException {
    boolean expectedAsyncStarted = true;

    TestAsyncContextImpl.track("onError-");
    if (completeOnError) {
        event.getAsyncContext().complete();
        expectedAsyncStarted = false;
    }

    ServletRequest req = event.getSuppliedRequest();
    asyncStartedCorrect = (expectedAsyncStarted == req.isAsyncStarted());
}
 
Example 5
Source File: ApmAsyncListener.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private void endTransaction(AsyncEvent event) {
    // To ensure transaction is ended only by a single event
    if (completed.getAndSet(true) || transaction == null) {
        return;
    }

    try {
        HttpServletRequest request = (HttpServletRequest) event.getSuppliedRequest();
        request.removeAttribute(TRANSACTION_ATTRIBUTE);

        HttpServletResponse response = (HttpServletResponse) event.getSuppliedResponse();
        final Response resp = transaction.getContext().getResponse();
        if (transaction.isSampled() && servletTransactionHelper.isCaptureHeaders()) {
            for (String headerName : response.getHeaderNames()) {
                resp.addHeader(headerName, response.getHeaders(headerName));
            }
        }
        // request.getParameterMap() may allocate a new map, depending on the servlet container implementation
        // so only call this method if necessary
        final String contentTypeHeader = request.getHeader("Content-Type");
        final Map<String, String[]> parameterMap;
        if (transaction.isSampled() && servletTransactionHelper.captureParameters(request.getMethod(), contentTypeHeader)) {
            parameterMap = request.getParameterMap();
        } else {
            parameterMap = null;
        }
        Throwable throwableToSend = event.getThrowable();
        if (throwableToSend == null) {
            throwableToSend = throwable;
        }
        servletTransactionHelper.onAfter(transaction, throwableToSend,
            response.isCommitted(), response.getStatus(), true, request.getMethod(), parameterMap,
            request.getServletPath(), request.getPathInfo(), contentTypeHeader, false
        );
    } finally {
        asyncContextAdviceHelperImpl.recycle(this);
    }
}
 
Example 6
Source File: TracingAsyncListener.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete(AsyncEvent event) throws IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) event.getSuppliedRequest();
    HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
    for (ServletFilterSpanDecorator spanDecorator: spanDecorators) {
            spanDecorator.onResponse(httpRequest,
            httpResponse,
            span);
    }
    span.finish();
}
 
Example 7
Source File: TracingAsyncListener.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public void onTimeout(AsyncEvent event) throws IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) event.getSuppliedRequest();
    HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
    for (ServletFilterSpanDecorator spanDecorator : spanDecorators) {
          spanDecorator.onTimeout(httpRequest,
              httpResponse,
              event.getAsyncContext().getTimeout(),
              span);
      }
}
 
Example 8
Source File: TracingAsyncListener.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public void onError(AsyncEvent event) throws IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) event.getSuppliedRequest();
    HttpServletResponse httpResponse = (HttpServletResponse) event.getSuppliedResponse();
    for (ServletFilterSpanDecorator spanDecorator: spanDecorators) {
        spanDecorator.onError(httpRequest,
            httpResponse,
            event.getThrowable(),
            span);
    }
}
 
Example 9
Source File: RestAsyncListener.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void onTimeout(AsyncEvent event) throws IOException {
  // in this time, maybe:
  // 1.invocation in executor's queue
  // 2.already executing in executor
  // 3.already send response
  // to avoid concurrent, must lock request
  ServletRequest request = event.getSuppliedRequest();
  HttpServletRequestEx requestEx = (HttpServletRequestEx) request.getAttribute(RestConst.REST_REQUEST);
  LOGGER.error("Rest request timeout, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());

  // Waiting till executing in executor done. This operation may block container pool and make timeout requests in executor's
  // queue getting executed, and will cause bad performance. So default timeout is setting to -1 to disable timeout.
  synchronized (requestEx) {
    ServletResponse response = event.getAsyncContext().getResponse();
    if (!response.isCommitted()) {
      // invocation in executor's queue
      response.setContentType(MediaType.APPLICATION_JSON);

      // we don't know if developers declared one statusCode in contract
      // so we use cse inner statusCode here
      ((HttpServletResponse) response).setStatus(Status.INTERNAL_SERVER_ERROR.getStatusCode());
      PrintWriter out = response.getWriter();
      out.write(TIMEOUT_MESSAGE);
      response.flushBuffer();
    }

    request.removeAttribute(RestConst.REST_REQUEST);
  }

  LOGGER.error("Rest request timeout committed, method {}, path {}.", requestEx.getMethod(), requestEx.getRequestURI());
}
 
Example 10
Source File: ServerAsyncListener.java    From aceql-http with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
   public void onTimeout(AsyncEvent event) throws IOException {
HttpServletRequest request = (HttpServletRequest)event.getSuppliedRequest();
LoggerUtil.log(request, event.getThrowable());

HttpServletResponse response = (HttpServletResponse)event.getSuppliedResponse();
AsyncDebug.debug("Set reponse status to SC_GATEWAY_TIMEOUT");
response.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT);
   }
 
Example 11
Source File: ServletRuntime.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void onTimeout(AsyncEvent e) {
  // Propagate the timeout so that the onComplete hook can see it.
  ServletRequest request = e.getSuppliedRequest();
  if (request.getAttribute("error") == null) {
    request.setAttribute("error", new AsyncTimeoutException(e));
  }
}
 
Example 12
Source File: WingtipsRequestSpanCompletionAsyncListener.java    From wingtips with Apache License 2.0 4 votes vote down vote up
/**
 * Does the work of doing the final span tagging and naming, and completes the span for {@link
 * #originalRequestTracingState}. The request, response, and any error needed for the tagging/naming are pulled
 * from the given {@link AsyncEvent}. This method is configured to only ever execute once (via the
 * {@link #alreadyCompleted} atomic boolean) - subsequent calls will return immediately without doing anything.
 *
 * @param event The {@link AsyncEvent} that triggered finalizing the request span.
 */
@SuppressWarnings("deprecation")
protected void completeRequestSpan(AsyncEvent event) {
    // Async servlet stuff can trigger multiple completion methods depending on how the request is processed,
    //      but we only care about the first.
    if (alreadyCompleted.getAndSet(true)) {
        return;
    }

    ServletRequest request = event.getSuppliedRequest();
    ServletResponse response = event.getSuppliedResponse();

    final HttpServletRequest httpRequest = (request instanceof HttpServletRequest)
                                     ? (HttpServletRequest) request
                                     : null;
    final HttpServletResponse httpResponse = (response instanceof HttpServletResponse)
                                       ? (HttpServletResponse) response
                                       : null;
    final Throwable error = event.getThrowable();

    // Reattach the original tracing state and handle span finalization/completion.
    //noinspection deprecation
    runnableWithTracing(
        new Runnable() {
            @Override
            public void run() {
                Span span = Tracer.getInstance().getCurrentSpan();

                try {
                    // Handle response/error tagging and final span name.
                    tagAndNamingStrategy.handleResponseTaggingAndFinalSpanName(
                        span, httpRequest, httpResponse, error, tagAndNamingAdapter
                    );
                }
                finally {
                    // Complete the overall request span.
                    Tracer.getInstance().completeRequestSpan();
                }
            }
        },
        originalRequestTracingState
    ).run();
}
 
Example 13
Source File: ServletRuntime.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override public void onError(AsyncEvent e) {
  ServletRequest request = e.getSuppliedRequest();
  if (request.getAttribute("error") == null) {
    request.setAttribute("error", e.getThrowable());
  }
}