add validate

This commit is contained in:
Spencer Gibb
2017-01-20 18:51:11 -07:00
parent d4dfdca566
commit 6189f5f112
2 changed files with 9 additions and 4 deletions

View File

@@ -3,7 +3,6 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.util.List;
import java.util.function.Predicate;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -13,12 +12,12 @@ public class HeaderRoutePredicate implements RoutePredicate {
@Override
public Predicate<ServerWebExchange> apply(String header, String[] args) {
validate(args, 1);
String regexp = args[0];
//TODO: caching can happen here
return exchange -> {
Assert.isTrue(args != null && args.length == 1,
"args must have one entry");
String regexp = args[0];
List<String> values = exchange.getRequest().getHeaders().get(header);
for (String value : values) {
if (value.matches(regexp)) {

View File

@@ -2,6 +2,7 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.util.function.Predicate;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -10,4 +11,9 @@ import org.springframework.web.server.ServerWebExchange;
public interface RoutePredicate {
Predicate<ServerWebExchange> apply(String value, String[] args);
default void validate(String[] args, int requiredSize) {
Assert.isTrue(args != null && args.length == requiredSize,
"args must have "+ requiredSize +" entry(s)");
}
}