Search in sources :

Example 1 with PredicateDefinition

use of org.springframework.cloud.gateway.handler.predicate.PredicateDefinition in project spring-cloud-gateway by spring-cloud.

the class RouteDefinitionRouteLocator method combinePredicates.

private Predicate<ServerWebExchange> combinePredicates(RouteDefinition routeDefinition) {
    List<PredicateDefinition> predicates = routeDefinition.getPredicates();
    Predicate<ServerWebExchange> predicate = lookup(routeDefinition, predicates.get(0));
    for (PredicateDefinition andPredicate : predicates.subList(1, predicates.size())) {
        Predicate<ServerWebExchange> found = lookup(routeDefinition, andPredicate);
        predicate = predicate.and(found);
    }
    return predicate;
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) PredicateDefinition(org.springframework.cloud.gateway.handler.predicate.PredicateDefinition)

Example 2 with PredicateDefinition

use of org.springframework.cloud.gateway.handler.predicate.PredicateDefinition 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

PredicateDefinition (org.springframework.cloud.gateway.handler.predicate.PredicateDefinition)2 FilterDefinition (org.springframework.cloud.gateway.filter.FilterDefinition)1 RewritePathGatewayFilterFactory (org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory)1 PathRoutePredicateFactory (org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory)1 RouteDefinition (org.springframework.cloud.gateway.route.RouteDefinition)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1