1818
1919import java .util .ArrayList ;
2020import java .util .List ;
21+ import java .util .concurrent .ConcurrentHashMap ;
2122import java .util .stream .Collectors ;
2223
2324import org .apache .commons .logging .Log ;
2425import org .apache .commons .logging .LogFactory ;
26+ import org .springframework .cloud .gateway .config .GatewayProperties ;
2527import reactor .core .publisher .Mono ;
2628
2729import org .springframework .cloud .gateway .filter .GatewayFilter ;
@@ -55,8 +57,13 @@ public class FilteringWebHandler implements WebHandler {
5557
5658 private final List <GatewayFilter > globalFilters ;
5759
58- public FilteringWebHandler (List <GlobalFilter > globalFilters ) {
60+ private ConcurrentHashMap <Route ,List <GatewayFilter >> RouteFilterMap = new ConcurrentHashMap ();
61+
62+ private GatewayProperties properties ;
63+
64+ public FilteringWebHandler (List <GlobalFilter > globalFilters , GatewayProperties properties ) {
5965 this .globalFilters = loadFilters (globalFilters );
66+ this .properties = properties ;
6067 }
6168
6269 private static List <GatewayFilter > loadFilters (List <GlobalFilter > filters ) {
@@ -84,12 +91,7 @@ private static List<GatewayFilter> loadFilters(List<GlobalFilter> filters) {
8491 @ Override
8592 public Mono <Void > handle (ServerWebExchange exchange ) {
8693 Route route = exchange .getRequiredAttribute (GATEWAY_ROUTE_ATTR );
87- List <GatewayFilter > gatewayFilters = route .getFilters ();
88-
89- List <GatewayFilter > combined = new ArrayList <>(this .globalFilters );
90- combined .addAll (gatewayFilters );
91- // TODO: needed or cached?
92- AnnotationAwareOrderComparator .sort (combined );
94+ List <GatewayFilter > combined = getCombinedFilters (route );
9395
9496 if (logger .isDebugEnabled ()) {
9597 logger .debug ("Sorted gatewayFilterFactories: " + combined );
@@ -98,6 +100,25 @@ public Mono<Void> handle(ServerWebExchange exchange) {
98100 return new DefaultGatewayFilterChain (combined ).filter (exchange );
99101 }
100102
103+ public List <GatewayFilter > getCombinedFilters (Route route ){
104+ if (this .properties .isFilterCache ()) {
105+ if (!this .RouteFilterMap .contains (route )) {
106+ RouteFilterMap .put (route ,getAllFilters (route ));
107+ }
108+ return RouteFilterMap .get (route );
109+ }else {
110+ return getAllFilters (route );
111+ }
112+
113+ }
114+ public List <GatewayFilter > getAllFilters (Route route ){
115+ List <GatewayFilter > gatewayFilters = route .getFilters ();
116+ List <GatewayFilter > combined = new ArrayList <>(this .globalFilters );
117+ combined .addAll (gatewayFilters );
118+ AnnotationAwareOrderComparator .sort (combined );
119+ return combined ;
120+ }
121+
101122 private static class DefaultGatewayFilterChain implements GatewayFilterChain {
102123
103124 private final int index ;
0 commit comments