Search in sources :

Example 1 with Endpoint

use of org.glassfish.jersey.server.internal.process.Endpoint in project jersey by jersey.

the class ContainerFilteringStage method apply.

@Override
@SuppressWarnings("unchecked")
public Continuation<RequestProcessingContext> apply(RequestProcessingContext context) {
    Iterable<ContainerRequestFilter> sortedRequestFilters;
    final boolean postMatching = responseFilters == null;
    final ContainerRequest request = context.request();
    final TracingLogger tracingLogger = TracingLogger.getInstance(request);
    if (postMatching) {
        // post-matching
        final ArrayList<Iterable<RankedProvider<ContainerRequestFilter>>> rankedProviders = new ArrayList<>(2);
        rankedProviders.add(requestFilters);
        rankedProviders.add(request.getRequestFilters());
        sortedRequestFilters = Providers.mergeAndSortRankedProviders(new RankedComparator<ContainerRequestFilter>(), rankedProviders);
        context.monitoringEventBuilder().setContainerRequestFilters(sortedRequestFilters);
        context.triggerEvent(RequestEvent.Type.REQUEST_MATCHED);
    } else {
        // pre-matching (response filter stage is pushed in pre-matching phase, so that if pre-matching filter
        // throws exception, response filters get still invoked)
        context.push(new ResponseFilterStage(context, responseFilters, tracingLogger));
        sortedRequestFilters = Providers.sortRankedProviders(new RankedComparator<ContainerRequestFilter>(), requestFilters);
    }
    final TracingLogger.Event summaryEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER_SUMMARY : ServerTraceEvent.PRE_MATCH_SUMMARY);
    final long timestamp = tracingLogger.timestamp(summaryEvent);
    int processedCount = 0;
    try {
        final TracingLogger.Event filterEvent = (postMatching ? ServerTraceEvent.REQUEST_FILTER : ServerTraceEvent.PRE_MATCH);
        for (ContainerRequestFilter filter : sortedRequestFilters) {
            final long filterTimestamp = tracingLogger.timestamp(filterEvent);
            try {
                filter.filter(request);
            } catch (Exception exception) {
                throw new MappableException(exception);
            } finally {
                processedCount++;
                tracingLogger.logDuration(filterEvent, filterTimestamp, filter);
            }
            final Response abortResponse = request.getAbortResponse();
            if (abortResponse != null) {
                // abort accepting & return response
                return Continuation.of(context, Stages.asStage(new Endpoint() {

                    @Override
                    public ContainerResponse apply(final RequestProcessingContext requestContext) {
                        return new ContainerResponse(requestContext.request(), abortResponse);
                    }
                }));
            }
        }
    } finally {
        if (postMatching) {
            context.triggerEvent(RequestEvent.Type.REQUEST_FILTERED);
        }
        tracingLogger.logDuration(summaryEvent, timestamp, processedCount);
    }
    return Continuation.of(context, getDefaultNext());
}
Also used : MappableException(org.glassfish.jersey.server.internal.process.MappableException) ArrayList(java.util.ArrayList) TracingLogger(org.glassfish.jersey.message.internal.TracingLogger) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) MappableException(org.glassfish.jersey.server.internal.process.MappableException) RankedComparator(org.glassfish.jersey.model.internal.RankedComparator) Response(javax.ws.rs.core.Response) RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 2 with Endpoint

use of org.glassfish.jersey.server.internal.process.Endpoint in project jersey by jersey.

the class ServerRuntime method process.

/**
     * Process a container request.
     *
     * @param request container request to be processed.
     */
public void process(final ContainerRequest request) {
    TracingUtils.initTracingSupport(tracingConfig, tracingThreshold, request);
    TracingUtils.logStart(request);
    final UriRoutingContext routingContext = request.getUriRoutingContext();
    RequestEventBuilder monitoringEventBuilder = EmptyRequestEventBuilder.INSTANCE;
    RequestEventListener monitoringEventListener = null;
    if (applicationEventListener != null) {
        monitoringEventBuilder = new RequestEventImpl.Builder().setContainerRequest(request).setExtendedUriInfo(routingContext);
        monitoringEventListener = applicationEventListener.onRequest(monitoringEventBuilder.build(RequestEvent.Type.START));
    }
    request.setProcessingProviders(processingProviders);
    final RequestProcessingContext context = new RequestProcessingContext(injectionManager, request, routingContext, monitoringEventBuilder, monitoringEventListener);
    request.checkState();
    final Responder responder = new Responder(context, ServerRuntime.this);
    final RequestScope.Instance requestScopeInstance = requestScope.createInstance();
    // TODO: Will we need EXTERNAL_REQUEST_SCOPE after the injection task?
    final AsyncResponderHolder asyncResponderHolder = new AsyncResponderHolder(responder, externalRequestScope, requestScopeInstance, externalRequestScope.open(injectionManager));
    context.initAsyncContext(asyncResponderHolder);
    requestScope.runInScope(requestScopeInstance, new Runnable() {

        @Override
        public void run() {
            try {
                // for later resolving of relative location URIs
                if (!disableLocationHeaderRelativeUriResolution) {
                    final URI uriToUse = rfc7231LocationHeaderRelativeUriResolution ? request.getRequestUri() : request.getBaseUri();
                    OutboundJaxrsResponse.Builder.setBaseUri(uriToUse);
                }
                final Ref<Endpoint> endpointRef = Refs.emptyRef();
                final RequestProcessingContext data = Stages.process(context, requestProcessingRoot, endpointRef);
                final Endpoint endpoint = endpointRef.get();
                if (endpoint == null) {
                    // not found
                    throw new NotFoundException();
                }
                final ContainerResponse response = endpoint.apply(data);
                if (!asyncResponderHolder.isAsync()) {
                    responder.process(response);
                } else {
                    externalRequestScope.suspend(asyncResponderHolder.externalContext, injectionManager);
                }
            } catch (final Throwable throwable) {
                responder.process(throwable);
            } finally {
                asyncResponderHolder.release();
                // clear base URI from the thread
                OutboundJaxrsResponse.Builder.clearBaseUri();
            }
        }
    });
}
Also used : UriRoutingContext(org.glassfish.jersey.server.internal.routing.UriRoutingContext) NotFoundException(javax.ws.rs.NotFoundException) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) ExternalRequestScope(org.glassfish.jersey.server.spi.ExternalRequestScope) RequestScope(org.glassfish.jersey.process.internal.RequestScope) URI(java.net.URI) RequestProcessingContext(org.glassfish.jersey.server.internal.process.RequestProcessingContext) Ref(org.glassfish.jersey.internal.util.collection.Ref) RequestEventImpl(org.glassfish.jersey.server.internal.monitoring.RequestEventImpl) Endpoint(org.glassfish.jersey.server.internal.process.Endpoint) RequestEventListener(org.glassfish.jersey.server.monitoring.RequestEventListener) RequestEventBuilder(org.glassfish.jersey.server.internal.monitoring.RequestEventBuilder) EmptyRequestEventBuilder(org.glassfish.jersey.server.internal.monitoring.EmptyRequestEventBuilder)

Example 3 with Endpoint

use of org.glassfish.jersey.server.internal.process.Endpoint in project jersey by jersey.

the class RoutingStage method _apply.

@SuppressWarnings("unchecked")
private RoutingResult _apply(final RequestProcessingContext request, final Router router) {
    final Router.Continuation continuation = router.apply(request);
    for (Router child : continuation.next()) {
        RoutingResult result = _apply(continuation.requestContext(), child);
        if (result.endpoint != null) {
            // we're done
            return result;
        }
    // else continue
    }
    Endpoint endpoint = Routers.extractEndpoint(router);
    if (endpoint != null) {
        // inflector at terminal stage found
        return RoutingResult.from(continuation.requestContext(), endpoint);
    }
    // inflector at terminal stage not found
    return RoutingResult.from(continuation.requestContext());
}
Also used : Endpoint(org.glassfish.jersey.server.internal.process.Endpoint)

Aggregations

Endpoint (org.glassfish.jersey.server.internal.process.Endpoint)3 RequestProcessingContext (org.glassfish.jersey.server.internal.process.RequestProcessingContext)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 NotFoundException (javax.ws.rs.NotFoundException)1 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)1 Response (javax.ws.rs.core.Response)1 Ref (org.glassfish.jersey.internal.util.collection.Ref)1 MessageBodyProviderNotFoundException (org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException)1 TracingLogger (org.glassfish.jersey.message.internal.TracingLogger)1 RankedComparator (org.glassfish.jersey.model.internal.RankedComparator)1 RequestScope (org.glassfish.jersey.process.internal.RequestScope)1 EmptyRequestEventBuilder (org.glassfish.jersey.server.internal.monitoring.EmptyRequestEventBuilder)1 RequestEventBuilder (org.glassfish.jersey.server.internal.monitoring.RequestEventBuilder)1 RequestEventImpl (org.glassfish.jersey.server.internal.monitoring.RequestEventImpl)1 MappableException (org.glassfish.jersey.server.internal.process.MappableException)1 UriRoutingContext (org.glassfish.jersey.server.internal.routing.UriRoutingContext)1 RequestEventListener (org.glassfish.jersey.server.monitoring.RequestEventListener)1 ExternalRequestScope (org.glassfish.jersey.server.spi.ExternalRequestScope)1