Search in sources :

Example 16 with RESTEasyTracingLogger

use of org.jboss.resteasy.tracing.RESTEasyTracingLogger in project resteasy by resteasy.

the class ResourceMethodInvoker method invokeOnTarget.

protected BuiltResponse invokeOnTarget(HttpRequest request, HttpResponse response, Object target) {
    final RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
    final long timestamp = tracingLogger.timestamp("METHOD_INVOKE");
    final long msTimeStamp = methodStatisticsLogger.timestamp();
    try {
        // we don't pop so writer interceptors can get at this
        ResteasyContext.pushContext(ResourceInfo.class, resourceInfo);
        ResteasyContext.pushContext(Configuration.class, resourceMethodProviderFactory);
        if (requestFilters != null && requestFilters.length > 0) {
            PostMatchContainerRequestContext requestContext = new PostMatchContainerRequestContext(request, this, requestFilters, () -> invokeOnTargetAfterFilter(request, response, target));
            // let it handle the continuation
            return requestContext.filter();
        } else {
            return invokeOnTargetAfterFilter(request, response, target);
        }
    } finally {
        methodStatisticsLogger.duration(msTimeStamp);
        if (resource instanceof SingletonResource) {
            tracingLogger.logDuration("METHOD_INVOKE", timestamp, ((SingletonResource) resource).traceInfo(), method.getMethod());
        } else {
            tracingLogger.logDuration("METHOD_INVOKE", timestamp, resource, method.getMethod());
        }
    }
}
Also used : PostMatchContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.PostMatchContainerRequestContext) SingletonResource(org.jboss.resteasy.plugins.server.resourcefactory.SingletonResource) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger)

Example 17 with RESTEasyTracingLogger

use of org.jboss.resteasy.tracing.RESTEasyTracingLogger in project resteasy by resteasy.

the class PreMatchContainerRequestContext method filter.

public synchronized BuiltResponse filter() {
    RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(httpRequest);
    final long totalTimestamp = tracingLogger.timestamp("REQUEST_FILTER_SUMMARY");
    while (requestFilters != null && currentFilter < requestFilters.length) {
        ContainerRequestFilter filter = requestFilters[currentFilter++];
        try {
            suspended = false;
            response = null;
            throwable = null;
            inFilter = true;
            final long timestamp = tracingLogger.timestamp("REQUEST_FILTER");
            filter.filter(this);
            tracingLogger.logDuration("REQUEST_FILTER", timestamp, filter);
        } catch (IOException e) {
            throw new ApplicationException(e);
        } finally {
            inFilter = false;
        }
        if (suspended) {
            if (!httpRequest.getAsyncContext().isSuspended())
                httpRequest.getAsyncContext().suspend();
            // ignore any abort request until we are resumed
            filterReturnIsMeaningful = false;
            response = null;
            return null;
        }
        BuiltResponse serverResponse = (BuiltResponse) getResponseAbortedWith();
        if (serverResponse != null) {
            // handle the case where we've been suspended by a previous filter
            if (filterReturnIsMeaningful)
                return serverResponse;
            else {
                httpRequest.getAsyncContext().getAsyncResponse().resume(serverResponse);
                return null;
            }
        }
        if (throwable != null) {
            // handle the case where we've been suspended by a previous filter
            if (filterReturnIsMeaningful)
                SynchronousDispatcher.rethrow(throwable);
            else {
                writeException(throwable);
                return null;
            }
        }
    }
    tracingLogger.logDuration("REQUEST_FILTER_SUMMARY", totalTimestamp, requestFilters == null ? 0 : requestFilters.length);
    // here it means we reached the last filter
    // some frameworks don't support async request filters, in which case suspend() is forbidden
    // so if we get here we're still synchronous and don't have a continuation, which must be in
    // the caller
    startedContinuation = true;
    if (continuation == null)
        return null;
    // in any case, return the continuation: sync will use it, and async will ignore it
    return continuation.get();
}
Also used : ApplicationException(org.jboss.resteasy.spi.ApplicationException) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) IOException(java.io.IOException)

Example 18 with RESTEasyTracingLogger

use of org.jboss.resteasy.tracing.RESTEasyTracingLogger in project resteasy by resteasy.

the class ContainerResponseContextImpl method filter.

public synchronized void filter() throws IOException {
    RESTEasyTracingLogger logger = RESTEasyTracingLogger.getInstance(request);
    while (currentFilter < responseFilters.length) {
        ContainerResponseFilter filter = responseFilters[currentFilter++];
        try {
            suspended = false;
            throwable = null;
            inFilter = true;
            final long timestamp = logger.timestamp("RESPONSE_FILTER");
            filter.filter(requestContext, this);
            logger.logDuration("RESPONSE_FILTER", timestamp, filter);
        } catch (IOException e) {
            throw new ApplicationException(e);
        } finally {
            inFilter = false;
        }
        if (suspended) {
            if (!request.getAsyncContext().isSuspended()) {
                request.getAsyncContext().suspend();
                weSuspended = true;
            }
            // ignore any abort request until we are resumed
            filterReturnIsMeaningful = false;
            return;
        }
        if (throwable != null) {
            // handle the case where we've been suspended by a previous filter
            if (filterReturnIsMeaningful)
                SynchronousDispatcher.rethrow(throwable);
            else {
                writeException(throwable);
                return;
            }
        }
    }
    // the caller
    if (continuation == null)
        return;
    // if we've never been suspended, the caller is valid so let it handle any exception
    if (filterReturnIsMeaningful) {
        continuation.run(onComplete);
        return;
    }
    // try to write it out
    try {
        continuation.run((t) -> {
            onComplete.accept(t);
            if (weSuspended) {
                // if we're the ones who turned the request async, nobody will call complete() for us, so we have to
                request.getAsyncContext().complete();
            }
        });
    } catch (IOException e) {
        LogMessages.LOGGER.unknownException(request.getHttpMethod(), request.getUri().getPath(), e);
    }
}
Also used : ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) ApplicationException(org.jboss.resteasy.spi.ApplicationException) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) RunnableWithIOException(org.jboss.resteasy.core.ServerResponseWriter.RunnableWithIOException) IOException(java.io.IOException)

Example 19 with RESTEasyTracingLogger

use of org.jboss.resteasy.tracing.RESTEasyTracingLogger in project resteasy by resteasy.

the class ExceptionHandler method handleException.

public Response handleException(HttpRequest request, Throwable e) {
    Response jaxrsResponse = null;
    RESTEasyTracingLogger logger = RESTEasyTracingLogger.getInstance(request);
    // lookup mapper on class name of exception
    jaxrsResponse = executeExactExceptionMapper(e, logger);
    if (jaxrsResponse == null) {
        if (e instanceof ClientErrorException) {
            // These are BadRequestException and NotFoundException exceptions
            jaxrsResponse = executeExceptionMapper(e, logger);
            if (jaxrsResponse == null) {
                jaxrsResponse = handleClientErrorException(request, (ClientErrorException) e);
            }
        } else if (e instanceof WebApplicationException) {
            /*
             * If the response property of the exception does not
             * contain an entity and an exception mapping provider
             * (see section 4.4) is available for
             * WebApplicationException an implementation MUST use the
             * provider to create a new Response instance, otherwise
             * the response property is used directly.
             */
            WebApplicationException wae = (WebApplicationException) e;
            if (wae.getResponse() != null && wae.getResponse().getEntity() != null) {
                jaxrsResponse = wae.getResponse();
            } else {
                // look at exception's subClass tree for possible mappers
                jaxrsResponse = executeExceptionMapper(e, logger);
                if (jaxrsResponse == null) {
                    jaxrsResponse = handleWebApplicationException((WebApplicationException) e);
                }
            }
        } else if (e instanceof Failure) {
            // known exceptions that extend from Failure
            if (e instanceof WriterException) {
                jaxrsResponse = handleWriterException(request, (WriterException) e, logger);
            } else if (e instanceof ReaderException) {
                jaxrsResponse = handleReaderException(request, (ReaderException) e, logger);
            } else {
                jaxrsResponse = executeExceptionMapper(e, logger);
                if (jaxrsResponse == null) {
                    jaxrsResponse = handleFailure(request, (Failure) e);
                }
            }
        } else {
            if (e instanceof ApplicationException) {
                jaxrsResponse = handleApplicationException(request, (ApplicationException) e, logger);
            } else {
                jaxrsResponse = executeExceptionMapper(e, logger);
            }
        }
    }
    if (jaxrsResponse == null) {
        throw new UnhandledException(e);
    }
    return jaxrsResponse;
}
Also used : Response(jakarta.ws.rs.core.Response) UnhandledException(org.jboss.resteasy.spi.UnhandledException) NoLogWebApplicationException(org.jboss.resteasy.spi.NoLogWebApplicationException) WebApplicationException(jakarta.ws.rs.WebApplicationException) ApplicationException(org.jboss.resteasy.spi.ApplicationException) NoLogWebApplicationException(org.jboss.resteasy.spi.NoLogWebApplicationException) WebApplicationException(jakarta.ws.rs.WebApplicationException) ClientErrorException(jakarta.ws.rs.ClientErrorException) RESTEasyTracingLogger(org.jboss.resteasy.tracing.RESTEasyTracingLogger) Failure(org.jboss.resteasy.spi.Failure) WriterException(org.jboss.resteasy.spi.WriterException) ReaderException(org.jboss.resteasy.spi.ReaderException)

Aggregations

RESTEasyTracingLogger (org.jboss.resteasy.tracing.RESTEasyTracingLogger)19 NotFoundException (jakarta.ws.rs.NotFoundException)9 IOException (java.io.IOException)8 BuiltResponse (org.jboss.resteasy.specimpl.BuiltResponse)8 Response (jakarta.ws.rs.core.Response)7 CompletionException (java.util.concurrent.CompletionException)7 HttpResponse (org.jboss.resteasy.spi.HttpResponse)7 InternalServerErrorException (org.jboss.resteasy.spi.InternalServerErrorException)7 UnhandledException (org.jboss.resteasy.spi.UnhandledException)7 ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)5 ArrayList (java.util.ArrayList)5 ResourceInvoker (org.jboss.resteasy.spi.ResourceInvoker)5 List (java.util.List)4 PreMatchContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext)4 Dispatcher (org.jboss.resteasy.spi.Dispatcher)4 Failure (org.jboss.resteasy.spi.Failure)4 ResourceContext (jakarta.ws.rs.container.ResourceContext)3 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)3 Request (jakarta.ws.rs.core.Request)3 UriInfo (jakarta.ws.rs.core.UriInfo)3