Package io.micronaut.http.filter
Class FilterRunner
java.lang.Object
io.micronaut.http.filter.FilterRunner
The filter runner will start processing the filters in the forward order.
All the request filters are executed till one of them returns a response (bypasses the route execution for controllers or the client invocation),
or the terminal filter will produce the response from the route/client call.
After that, the filters are processed in the opposite order so response filters can be processed,
which can sometimes override the existing response.
There is a special case of response filters that needs to process the response; for those cases,
the filter needs to be suspended, and the next filter in the order needs to be executed.
When the response is committed, the filter will be resumed when it's processed again.
There is a special case for the client filters; those will process the exception,
which needs to be tracked during the response filtering phase.
- Since:
- 4.0.0
- Author:
- Jonas Konrad, Denis Stepanov
-
Constructor Summary
ConstructorDescriptionFilterRunner
(@Nullable List<GenericHttpFilter> preMatchingFilters, @Nullable List<GenericHttpFilter> filters, BiFunction<HttpRequest<?>, PropagatedContext, ExecutionFlow<HttpResponse<?>>> responseProvider) Create a new filter runner, to be used only once.FilterRunner
(List<GenericHttpFilter> filters) Create a new filter runner, to be used only once.FilterRunner
(List<GenericHttpFilter> filters, BiFunction<HttpRequest<?>, PropagatedContext, ExecutionFlow<HttpResponse<?>>> responseProvider) Create a new filter runner, to be used only once. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doRouteMatch
(@NonNull HttpRequest<?> request) Do the route match and set it into the request.protected @NonNull List<GenericHttpFilter>
findFiltersAfterRouteMatch
(@NonNull HttpRequest<?> request) Find filters after route is found.protected @Nullable ExecutionFlow<HttpResponse<?>>
processFailure
(HttpRequest<?> request, Throwable failure, PropagatedContext propagatedContext) Transform a failure, e.g.protected ExecutionFlow<HttpResponse<?>>
processResponse
(HttpRequest<?> request, HttpResponse<?> response, PropagatedContext propagatedContext) Transform a response, e.g.protected @NonNull ExecutionFlow<HttpResponse<?>>
provideResponse
(@NonNull HttpRequest<?> request, @NonNull PropagatedContext propagatedContext) Provide response.final @NonNull ExecutionFlow<HttpResponse<?>>
run
(@NonNull HttpRequest<?> request) Execute the filters for the given request.final ExecutionFlow<HttpResponse<?>>
run
(HttpRequest<?> request, PropagatedContext propagatedContext) Execute the filters for the given request.static void
sort
(@NonNull List<GenericHttpFilter> filters) Sort filters according to their declared order (e.g.static void
sortReverse
(@NonNull List<GenericHttpFilter> filters) Sort filters according to their declared order (e.g.
-
Constructor Details
-
FilterRunner
public FilterRunner(List<GenericHttpFilter> filters, BiFunction<HttpRequest<?>, PropagatedContext, ExecutionFlow<HttpResponse<?>>> responseProvider) Create a new filter runner, to be used only once.- Parameters:
filters
- The filters to runresponseProvider
- The response provider
-
FilterRunner
Create a new filter runner, to be used only once.- Parameters:
filters
- The filters to run- Since:
- 4.6
-
FilterRunner
public FilterRunner(@Nullable @Nullable List<GenericHttpFilter> preMatchingFilters, @Nullable @Nullable List<GenericHttpFilter> filters, BiFunction<HttpRequest<?>, PropagatedContext, ExecutionFlow<HttpResponse<?>>> responseProvider) Create a new filter runner, to be used only once.- Parameters:
preMatchingFilters
- The pre matching filtersfilters
- The filters to runresponseProvider
- The response provider- Since:
- 4.6
-
-
Method Details
-
sort
Sort filters according to their declared order (e.g. annotation,Ordered.getOrder()
...). List must not contain terminal filters.- Parameters:
filters
- The list of filters to sort in place
-
sortReverse
Sort filters according to their declared order (e.g. annotation,Ordered.getOrder()
...). List must not contain terminal filters. Reverse order.- Parameters:
filters
- The list of filters to sort in place
-
doRouteMatch
Do the route match and set it into the request.- Parameters:
request
- The request- Since:
- 4.6
-
processResponse
protected ExecutionFlow<HttpResponse<?>> processResponse(HttpRequest<?> request, HttpResponse<?> response, PropagatedContext propagatedContext) Transform a response, e.g. by replacing an error response with an exception. Called before every filter.- Parameters:
request
- The current requestresponse
- The current responsepropagatedContext
- The propagated context- Returns:
- A flow that will be passed on to the next filter
-
processFailure
@Nullable protected @Nullable ExecutionFlow<HttpResponse<?>> processFailure(HttpRequest<?> request, Throwable failure, PropagatedContext propagatedContext) Transform a failure, e.g. by replacing an exception with an error response. Called before every filter.- Parameters:
request
- The current requestfailure
- The failurepropagatedContext
- The propagated context- Returns:
- A flow that will be passed on to the next filter, or null if exception is not remapped
-
provideResponse
@NonNull protected @NonNull ExecutionFlow<HttpResponse<?>> provideResponse(@NonNull @NonNull HttpRequest<?> request, @NonNull @NonNull PropagatedContext propagatedContext) Provide response.- Parameters:
request
- The requestpropagatedContext
- The propagatedContext- Returns:
- The flow
-
findFiltersAfterRouteMatch
@NonNull protected @NonNull List<GenericHttpFilter> findFiltersAfterRouteMatch(@NonNull @NonNull HttpRequest<?> request) Find filters after route is found.- Parameters:
request
- The request- Returns:
- The filters
- Since:
- 4.6
-
run
@NonNull public final @NonNull ExecutionFlow<HttpResponse<?>> run(@NonNull @NonNull HttpRequest<?> request) Execute the filters for the given request. May only be called once- Parameters:
request
- The request- Returns:
- The flow that completes after all filters and the terminal operation, with the final response
-
run
public final ExecutionFlow<HttpResponse<?>> run(HttpRequest<?> request, PropagatedContext propagatedContext) Execute the filters for the given request. May only be called once- Parameters:
request
- The requestpropagatedContext
- The propagated context- Returns:
- The flow that completes after all filters and the terminal operation, with the final response
-