Search in sources :

Example 1 with OrderedGatewayFilter

use of org.springframework.cloud.gateway.filter.OrderedGatewayFilter in project spring-cloud-gateway by spring-cloud.

the class RouteDefinitionRouteLocator method loadGatewayFilters.

@SuppressWarnings("unchecked")
private List<GatewayFilter> loadGatewayFilters(String id, List<FilterDefinition> filterDefinitions) {
    List<GatewayFilter> filters = filterDefinitions.stream().map(definition -> {
        GatewayFilterFactory factory = this.gatewayFilterFactories.get(definition.getName());
        if (factory == null) {
            throw new IllegalArgumentException("Unable to find GatewayFilterFactory with name " + definition.getName());
        }
        Map<String, String> args = definition.getArgs();
        if (logger.isDebugEnabled()) {
            logger.debug("RouteDefinition " + id + " applying filter " + args + " to " + definition.getName());
        }
        if (factory.isConfigurable()) {
            Map<String, Object> properties = factory.shortcutType().normalize(args, factory, this.parser, this.beanFactory);
            Object configuration = factory.newConfig();
            ConfigurationUtils.bind(configuration, properties, factory.shortcutFieldPrefix(), definition.getName(), validator);
            GatewayFilter gatewayFilter = factory.apply(configuration);
            if (this.publisher != null) {
                this.publisher.publishEvent(new FilterArgsEvent(this, id, properties));
            }
            return gatewayFilter;
        } else {
            Tuple tuple = getTuple(factory, args, this.parser, this.beanFactory);
            return factory.apply(tuple);
        }
    }).collect(Collectors.toList());
    ArrayList<GatewayFilter> ordered = new ArrayList<>(filters.size());
    for (int i = 0; i < filters.size(); i++) {
        ordered.add(new OrderedGatewayFilter(filters.get(i), i + 1));
    }
    return ordered;
}
Also used : ShortcutConfigurable(org.springframework.cloud.gateway.support.ShortcutConfigurable) Validator(org.springframework.validation.Validator) TupleBuilder(org.springframework.tuple.TupleBuilder) RoutePredicateFactory(org.springframework.cloud.gateway.handler.predicate.RoutePredicateFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter) FilterArgsEvent(org.springframework.cloud.gateway.event.FilterArgsEvent) PredicateArgsEvent(org.springframework.cloud.gateway.event.PredicateArgsEvent) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) LinkedHashMap(java.util.LinkedHashMap) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) GatewayProperties(org.springframework.cloud.gateway.config.GatewayProperties) GatewayFilterFactory(org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory) ShortcutConfigurable.getValue(org.springframework.cloud.gateway.support.ShortcutConfigurable.getValue) Predicate(java.util.function.Predicate) FilterDefinition(org.springframework.cloud.gateway.filter.FilterDefinition) BeansException(org.springframework.beans.BeansException) Collectors(java.util.stream.Collectors) PredicateDefinition(org.springframework.cloud.gateway.handler.predicate.PredicateDefinition) Flux(reactor.core.publisher.Flux) List(java.util.List) ConfigurationUtils(org.springframework.cloud.gateway.support.ConfigurationUtils) Tuple(org.springframework.tuple.Tuple) BeanFactory(org.springframework.beans.factory.BeanFactory) ShortcutConfigurable.normalizeKey(org.springframework.cloud.gateway.support.ShortcutConfigurable.normalizeKey) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) Log(org.apache.commons.logging.Log) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LogFactory(org.apache.commons.logging.LogFactory) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) ArrayList(java.util.ArrayList) FilterArgsEvent(org.springframework.cloud.gateway.event.FilterArgsEvent) OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter) GatewayFilterFactory(org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) Tuple(org.springframework.tuple.Tuple)

Example 2 with OrderedGatewayFilter

use of org.springframework.cloud.gateway.filter.OrderedGatewayFilter in project spring-cloud-gateway by spring-cloud.

the class GatewayFilterSpec method filter.

public GatewayFilterSpec filter(GatewayFilter gatewayFilter, int order) {
    if (gatewayFilter instanceof Ordered) {
        this.routeBuilder.filter(gatewayFilter);
        log.warn("GatewayFilter already implements ordered " + gatewayFilter.getClass() + "ignoring order parameter: " + order);
        return this;
    }
    this.routeBuilder.filter(new OrderedGatewayFilter(gatewayFilter, order));
    return this;
}
Also used : OrderedGatewayFilter(org.springframework.cloud.gateway.filter.OrderedGatewayFilter) Ordered(org.springframework.core.Ordered)

Aggregations

OrderedGatewayFilter (org.springframework.cloud.gateway.filter.OrderedGatewayFilter)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 BeansException (org.springframework.beans.BeansException)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 GatewayProperties (org.springframework.cloud.gateway.config.GatewayProperties)1 FilterArgsEvent (org.springframework.cloud.gateway.event.FilterArgsEvent)1 PredicateArgsEvent (org.springframework.cloud.gateway.event.PredicateArgsEvent)1 FilterDefinition (org.springframework.cloud.gateway.filter.FilterDefinition)1 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)1 GatewayFilterFactory (org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory)1