001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.reactive.function.server; 018 019import java.util.function.Function; 020 021import reactor.core.publisher.Mono; 022 023import org.springframework.util.Assert; 024import org.springframework.web.reactive.function.server.support.ServerRequestWrapper; 025 026/** 027 * Represents a function that filters a {@linkplain HandlerFunction handler function}. 028 * 029 * @author Arjen Poutsma 030 * @since 5.0 031 * @param <T> the type of the {@linkplain HandlerFunction handler function} to filter 032 * @param <R> the type of the response of the function 033 * @see RouterFunction#filter(HandlerFilterFunction) 034 */ 035@FunctionalInterface 036public interface HandlerFilterFunction<T extends ServerResponse, R extends ServerResponse> { 037 038 /** 039 * Apply this filter to the given handler function. The given 040 * {@linkplain HandlerFunction handler function} represents the next entity in the chain, 041 * and can be {@linkplain HandlerFunction#handle(ServerRequest) invoked} in order to 042 * proceed to this entity, or not invoked to block the chain. 043 * @param request the request 044 * @param next the next handler or filter function in the chain 045 * @return the filtered response 046 * @see ServerRequestWrapper 047 */ 048 Mono<R> filter(ServerRequest request, HandlerFunction<T> next); 049 050 /** 051 * Return a composed filter function that first applies this filter, and then applies the 052 * {@code after} filter. 053 * @param after the filter to apply after this filter is applied 054 * @return a composed filter that first applies this function and then applies the 055 * {@code after} function 056 */ 057 default HandlerFilterFunction<T, R> andThen(HandlerFilterFunction<T, T> after) { 058 Assert.notNull(after, "HandlerFilterFunction must not be null"); 059 return (request, next) -> { 060 HandlerFunction<T> nextHandler = handlerRequest -> after.filter(handlerRequest, next); 061 return filter(request, nextHandler); 062 }; 063 } 064 065 /** 066 * Apply this filter to the given handler function, resulting in a filtered handler function. 067 * @param handler the handler function to filter 068 * @return the filtered handler function 069 */ 070 default HandlerFunction<R> apply(HandlerFunction<T> handler) { 071 Assert.notNull(handler, "HandlerFunction must not be null"); 072 return request -> this.filter(request, handler); 073 } 074 075 /** 076 * Adapt the given request processor function to a filter function that only operates 077 * on the {@code ServerRequest}. 078 * @param requestProcessor the request processor 079 * @return the filter adaptation of the request processor 080 */ 081 static HandlerFilterFunction<?, ?> ofRequestProcessor( 082 Function<ServerRequest, Mono<ServerRequest>> requestProcessor) { 083 084 Assert.notNull(requestProcessor, "Function must not be null"); 085 return (request, next) -> requestProcessor.apply(request).flatMap(next::handle); 086 } 087 088 /** 089 * Adapt the given response processor function to a filter function that only operates 090 * on the {@code ServerResponse}. 091 * @param responseProcessor the response processor 092 * @return the filter adaptation of the request processor 093 */ 094 static <T extends ServerResponse, R extends ServerResponse> HandlerFilterFunction<T, R> ofResponseProcessor( 095 Function<T, Mono<R>> responseProcessor) { 096 097 Assert.notNull(responseProcessor, "Function must not be null"); 098 return (request, next) -> next.handle(request).flatMap(responseProcessor); 099 } 100 101}