use of org.glassfish.jersey.model.internal.RankedComparator 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());
}
Aggregations