Change Predicate from (value, args[]) to (args...)
This commit is contained in:
@@ -14,9 +14,6 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray;
|
||||
public class PredicateDefinition {
|
||||
@NotNull
|
||||
private String name;
|
||||
@NotNull
|
||||
private String value;
|
||||
|
||||
private String[] args;
|
||||
|
||||
public PredicateDefinition() {
|
||||
@@ -31,12 +28,7 @@ public class PredicateDefinition {
|
||||
setName(text.substring(0, eqIdx));
|
||||
|
||||
String[] args = tokenizeToStringArray(text.substring(eqIdx+1), ",");
|
||||
|
||||
setValue(args[0]);
|
||||
|
||||
if (args.length > 1) {
|
||||
setArgs(Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
setArgs(args);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -47,19 +39,11 @@ public class PredicateDefinition {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
public void setArgs(String[] args) {
|
||||
public void setArgs(String... args) {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@@ -69,20 +53,18 @@ public class PredicateDefinition {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PredicateDefinition that = (PredicateDefinition) o;
|
||||
return Objects.equals(name, that.name) &&
|
||||
Objects.equals(value, that.value) &&
|
||||
Arrays.equals(args, that.args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, value, args);
|
||||
return Objects.hash(name, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("PredicateDefinition{");
|
||||
sb.append("name='").append(name).append('\'');
|
||||
sb.append(", value='").append(value).append('\'');
|
||||
sb.append(", args=").append(Arrays.toString(args));
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
|
||||
@@ -35,7 +35,7 @@ public class DiscoveryClientRouteReader implements RouteReader {
|
||||
// add a predicate that matches the url at /serviceId/**
|
||||
PredicateDefinition predicate = new PredicateDefinition();
|
||||
predicate.setName("Url");
|
||||
predicate.setValue("/" + serviceId + "/**");
|
||||
predicate.setArgs("/" + serviceId + "/**");
|
||||
route.getPredicates().add(predicate);
|
||||
|
||||
//TODO: support for other default predicates
|
||||
|
||||
@@ -142,10 +142,10 @@ public class RoutePredicateHandlerMapping extends AbstractHandlerMapping {
|
||||
} else {
|
||||
args = Collections.emptyList();
|
||||
}
|
||||
logger.debug("Route " + route.getId() + " applying "+ predicate.getValue()
|
||||
+ ", " + args + " to " + predicate.getName());
|
||||
logger.debug("Route " + route.getId() + " applying "
|
||||
+ args + " to " + predicate.getName());
|
||||
}
|
||||
return found.apply(predicate.getValue(), predicate.getArgs());
|
||||
return found.apply(predicate.getArgs());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,8 +13,9 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePr
|
||||
public class AfterRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String dateString, String[] args) {
|
||||
final ZonedDateTime dateTime = parseZonedDateTime(dateString);
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(1, args);
|
||||
final ZonedDateTime dateTime = parseZonedDateTime(args[0]);
|
||||
|
||||
return exchange -> {
|
||||
final ZonedDateTime now = ZonedDateTime.now();
|
||||
|
||||
@@ -13,8 +13,9 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePr
|
||||
public class BeforeRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String dateString, String[] args) {
|
||||
final ZonedDateTime dateTime = parseZonedDateTime(dateString);
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(1, args);
|
||||
final ZonedDateTime dateTime = parseZonedDateTime(args[0]);
|
||||
|
||||
return exchange -> {
|
||||
final ZonedDateTime now = ZonedDateTime.now();
|
||||
|
||||
@@ -14,12 +14,12 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
public class BetweenRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String dateString, String[] args) {
|
||||
validate(args, 1);
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(2, args);
|
||||
|
||||
//TODO: is ZonedDateTime the right thing to use?
|
||||
final ZonedDateTime dateTime1 = parseZonedDateTime(dateString);
|
||||
final ZonedDateTime dateTime2 = parseZonedDateTime(args[0]);
|
||||
final ZonedDateTime dateTime1 = parseZonedDateTime(args[0]);
|
||||
final ZonedDateTime dateTime2 = parseZonedDateTime(args[1]);
|
||||
Assert.isTrue(dateTime1.isBefore(dateTime2));
|
||||
|
||||
return exchange -> {
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
/**
|
||||
@@ -13,13 +12,12 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
public class CookieRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String name, String[] args) {
|
||||
//TODO: caching can happen here
|
||||
return exchange -> {
|
||||
Assert.isTrue(args != null && args.length == 1,
|
||||
"args must have one entry");
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(2, args);
|
||||
String name = args[0];
|
||||
String regexp = args[1];
|
||||
|
||||
String regexp = args[0];
|
||||
return exchange -> {
|
||||
List<HttpCookie> cookies = exchange.getRequest().getCookies().get(name);
|
||||
for (HttpCookie cookie : cookies) {
|
||||
if (cookie.getValue().matches(regexp)) {
|
||||
|
||||
@@ -11,11 +11,11 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
public class HeaderRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String header, String[] args) {
|
||||
validate(args, 1);
|
||||
String regexp = args[0];
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(2, args);
|
||||
String header = args[0];
|
||||
String regexp = args[1];
|
||||
|
||||
//TODO: caching can happen here
|
||||
return exchange -> {
|
||||
|
||||
List<String> values = exchange.getRequest().getHeaders().get(header);
|
||||
|
||||
@@ -18,8 +18,10 @@ public class HostRoutePredicate implements RoutePredicate {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String pattern, String[] args) {
|
||||
//TODO: caching can happen here
|
||||
public Predicate<ServerWebExchange> apply(String[] args) {
|
||||
validate(1, args);
|
||||
String pattern = args[0];
|
||||
|
||||
return exchange -> {
|
||||
String host = exchange.getRequest().getHeaders().getFirst("Host");
|
||||
return this.pathMatcher.match(pattern, host);
|
||||
|
||||
@@ -11,8 +11,9 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
public class MethodRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String method, String[] args) {
|
||||
//TODO: caching can happen here
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(1, args);
|
||||
String method = args[0];
|
||||
return exchange -> {
|
||||
HttpMethod requestMethod = exchange.getRequest().getMethod();
|
||||
return requestMethod.matches(method);
|
||||
|
||||
@@ -11,20 +11,18 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
public class QueryRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String param, String[] args) {
|
||||
//TODO: caching can happen here
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(1, args);
|
||||
String param = args[0];
|
||||
|
||||
return exchange -> {
|
||||
String regexp = null;
|
||||
|
||||
if (args != null && args.length == 1) {
|
||||
regexp = args[0];
|
||||
}
|
||||
|
||||
if (regexp == null) {
|
||||
if (args.length < 2) {
|
||||
// check existence of header
|
||||
return exchange.getRequest().getQueryParams().containsKey(param);
|
||||
}
|
||||
|
||||
String regexp = args[1];
|
||||
|
||||
List<String> values = exchange.getRequest().getQueryParams().get(param);
|
||||
for (String value : values) {
|
||||
if (value.matches(regexp)) {
|
||||
|
||||
@@ -19,10 +19,10 @@ public class RemoteAddrRoutePredicate implements RoutePredicate {
|
||||
private static final Log log = LogFactory.getLog(RemoteAddrRoutePredicate.class);
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String source, String[] args) {
|
||||
List<SubnetUtils> sources = new ArrayList<>();
|
||||
addSource(sources, source);
|
||||
public Predicate<ServerWebExchange> apply(String... args) {
|
||||
validate(1, args);
|
||||
|
||||
List<SubnetUtils> sources = new ArrayList<>();
|
||||
if (args != null) {
|
||||
for (String arg : args) {
|
||||
addSource(sources, arg);
|
||||
@@ -39,8 +39,8 @@ public class RemoteAddrRoutePredicate implements RoutePredicate {
|
||||
log.warn("Remote addresses didn't match " + hostAddress + " != " + host);
|
||||
}
|
||||
|
||||
for (SubnetUtils subnet : sources) {
|
||||
if (subnet.getInfo().isInRange(hostAddress)) {
|
||||
for (SubnetUtils source : sources) {
|
||||
if (source.getInfo().isInRange(hostAddress)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
*/
|
||||
public interface RoutePredicate {
|
||||
|
||||
Predicate<ServerWebExchange> apply(String value, String[] args);
|
||||
Predicate<ServerWebExchange> apply(String... args);
|
||||
|
||||
default void validate(String[] args, int requiredSize) {
|
||||
Assert.isTrue(args != null && args.length == requiredSize,
|
||||
"args must have "+ requiredSize +" entry(s)");
|
||||
default void validate(int minimumSize, String... args) {
|
||||
Assert.isTrue(args != null && args.length >= minimumSize,
|
||||
"args must have at least "+ minimumSize +" entry(s)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,10 @@ public class UrlRoutePredicate implements RoutePredicate {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String pattern, String[] args) {
|
||||
public Predicate<ServerWebExchange> apply(String[] args) {
|
||||
validate(1, args);
|
||||
String pattern = args[0];
|
||||
|
||||
return exchange -> {
|
||||
String lookupPath = getPathHelper().getLookupPathForRequest(exchange);
|
||||
boolean match = getPathMatcher().match(pattern, lookupPath);
|
||||
|
||||
@@ -51,6 +51,6 @@ public class AfterRoutePredicateTests {
|
||||
}
|
||||
|
||||
private boolean runPredicate(String dateString) {
|
||||
return new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
return new AfterRoutePredicate().apply(dateString).test(getExchange());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,6 @@ public class BeforeRoutePredicateTests {
|
||||
}
|
||||
|
||||
private boolean runPredicate(String dateString) {
|
||||
return new BeforeRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
return new BeforeRoutePredicate().apply(dateString).test(getExchange());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class BetweenRoutePredicateTests {
|
||||
}
|
||||
|
||||
boolean runPredicate(String dateString1, String dateString2) {
|
||||
return new BetweenRoutePredicate().apply(dateString1, new String[]{dateString2}).test(getExchange());
|
||||
return new BetweenRoutePredicate().apply(dateString1, dateString2).test(getExchange());
|
||||
}
|
||||
|
||||
static String minusHoursMillis(int hours) {
|
||||
|
||||
@@ -153,7 +153,7 @@ spring:
|
||||
uri: http://httpbin.org:80
|
||||
predicates:
|
||||
- name: Url
|
||||
value: /**
|
||||
args: /**
|
||||
|
||||
myservice:
|
||||
ribbon:
|
||||
|
||||
Reference in New Issue
Block a user