Search in sources :

Example 1 with FilterDefinition

use of org.springframework.cloud.gateway.filter.FilterDefinition 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 FilterDefinition

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

the class DiscoveryClientRouteDefinitionLocator method getRouteDefinitions.

@Override
public Flux<RouteDefinition> getRouteDefinitions() {
    return Flux.fromIterable(discoveryClient.getServices()).map(serviceId -> {
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.setId(this.routeIdPrefix + serviceId);
        routeDefinition.setUri(URI.create("lb://" + serviceId));
        // add a predicate that matches the url at /serviceId
        /*PredicateDefinition barePredicate = new PredicateDefinition();
					barePredicate.setName(normalizePredicateName(PathRoutePredicate.class));
					barePredicate.addArg(PATTERN_KEY, "/" + serviceId);
					routeDefinition.getPredicates().add(barePredicate);*/
        // add a predicate that matches the url at /serviceId/**
        PredicateDefinition subPredicate = new PredicateDefinition();
        subPredicate.setName(normalizeRoutePredicateName(PathRoutePredicateFactory.class));
        subPredicate.addArg(PATTERN_KEY, "/" + serviceId + "/**");
        routeDefinition.getPredicates().add(subPredicate);
        // TODO: support for other default predicates
        // add a filter that removes /serviceId by default
        FilterDefinition filter = new FilterDefinition();
        filter.setName(normalizeFilterFactoryName(RewritePathGatewayFilterFactory.class));
        String regex = "/" + serviceId + "/(?<remaining>.*)";
        String replacement = "/${remaining}";
        filter.addArg(REGEXP_KEY, regex);
        filter.addArg(REPLACEMENT_KEY, replacement);
        routeDefinition.getFilters().add(filter);
        return routeDefinition;
    });
}
Also used : FilterDefinition(org.springframework.cloud.gateway.filter.FilterDefinition) RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) PredicateDefinition(org.springframework.cloud.gateway.handler.predicate.PredicateDefinition) RewritePathGatewayFilterFactory(org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory) PathRoutePredicateFactory(org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory)

Aggregations

FilterDefinition (org.springframework.cloud.gateway.filter.FilterDefinition)2 PredicateDefinition (org.springframework.cloud.gateway.handler.predicate.PredicateDefinition)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 GatewayFilter (org.springframework.cloud.gateway.filter.GatewayFilter)1 OrderedGatewayFilter (org.springframework.cloud.gateway.filter.OrderedGatewayFilter)1