RequestPredicateFactory args use Tuple

Rather than String[]. Still indexed though.
This commit is contained in:
Spencer Gibb
2017-03-14 23:16:02 -06:00
parent cd7767e30e
commit bcd76d82a5
21 changed files with 109 additions and 56 deletions

16
pom.xml
View File

@@ -52,6 +52,7 @@
<spring-boot.version>2.0.0.BUILD-SNAPSHOT</spring-boot.version>
<spring-cloud-commons.version>1.2.0.BUILD-SNAPSHOT</spring-cloud-commons.version>
<spring-cloud-netflix.version>1.3.0.BUILD-SNAPSHOT</spring-cloud-netflix.version>
<spring-tuple.version>1.0.0.RELEASE</spring-tuple.version>
</properties>
<dependencyManagement>
@@ -135,6 +136,21 @@
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tuple</artifactId>
<version>${spring-tuple.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>

View File

@@ -18,6 +18,10 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -54,6 +58,10 @@
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tuple</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>

View File

@@ -18,6 +18,7 @@
package org.springframework.cloud.gateway.discovery;
import java.net.URI;
import java.util.Collections;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.api.RouteLocator;
@@ -57,7 +58,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator {
// add a predicate that matches the url at /serviceId/**
PredicateDefinition predicate = new PredicateDefinition();
predicate.setName(normalizePredicateName(PathRequestPredicateFactory.class));
predicate.setArgs("/" + serviceId + "/**");
predicate.setArgs(Collections.singletonMap("path", "/" + serviceId + "/**"));
route.getPredicates().add(predicate);
//TODO: support for other default predicates

View File

@@ -17,8 +17,6 @@
package org.springframework.cloud.gateway.handler;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -27,19 +25,21 @@ import java.util.function.Function;
import org.springframework.cloud.gateway.api.RouteLocator;
import org.springframework.cloud.gateway.handler.predicate.RequestPredicateFactory;
import org.springframework.cloud.gateway.model.Route;
import org.springframework.cloud.gateway.model.PredicateDefinition;
import org.springframework.cloud.gateway.model.Route;
import org.springframework.tuple.Tuple;
import org.springframework.tuple.TupleBuilder;
import org.springframework.web.reactive.function.server.PublicDefaultServerRequest;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.handler.AbstractHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import reactor.core.publisher.Mono;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
import reactor.core.publisher.Mono;
/**
* @author Spencer Gibb
*/
@@ -165,16 +165,18 @@ public class RequestPredicateHandlerMapping extends AbstractHandlerMapping {
throw new IllegalArgumentException("Unable to find RequestPredicateFactory with name " + predicate.getName());
}
if (logger.isDebugEnabled()) {
List<String> args;
if (predicate.getArgs() != null) {
args = Arrays.asList(predicate.getArgs());
} else {
args = Collections.emptyList();
}
logger.debug("Route " + route.getId() + " applying "
+ args + " to " + predicate.getName());
+ predicate.getArgs() + " to " + predicate.getName());
}
return found.apply(predicate.getArgs());
TupleBuilder builder = TupleBuilder.tuple();
for (Map.Entry<String, String> entry : predicate.getArgs().entrySet()) {
builder.put(entry.getKey(), entry.getValue());
}
Tuple tuple = builder.build();
return found.apply(tuple);
}
/**

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.time.ZonedDateTime;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.RequestPredicate;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactory.parseZonedDateTime;
@@ -29,9 +30,9 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest
public class AfterRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
final ZonedDateTime dateTime = parseZonedDateTime(args[0]);
final ZonedDateTime dateTime = parseZonedDateTime(args.getString(0));
return request -> {
final ZonedDateTime now = ZonedDateTime.now();

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.time.ZonedDateTime;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.RequestPredicate;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactory.parseZonedDateTime;
@@ -29,9 +30,9 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest
public class BeforeRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
final ZonedDateTime dateTime = parseZonedDateTime(args[0]);
final ZonedDateTime dateTime = parseZonedDateTime(args.getString(0));
return request -> {
final ZonedDateTime now = ZonedDateTime.now();

View File

@@ -21,6 +21,7 @@ import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.springframework.tuple.Tuple;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -30,13 +31,14 @@ import org.springframework.web.reactive.function.server.RequestPredicate;
public class BetweenRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(2, args);
//TODO: is ZonedDateTime the right thing to use?
final ZonedDateTime dateTime1 = parseZonedDateTime(args[0]);
final ZonedDateTime dateTime2 = parseZonedDateTime(args[1]);
Assert.isTrue(dateTime1.isBefore(dateTime2), args[0] + " must be before " + args[1]);
final ZonedDateTime dateTime1 = parseZonedDateTime(args.getString(0));
final ZonedDateTime dateTime2 = parseZonedDateTime(args.getString(1));
Assert.isTrue(dateTime1.isBefore(dateTime2), args.getString(0) +
" must be before " + args.getString(1));
return request -> {
final ZonedDateTime now = ZonedDateTime.now();

View File

@@ -20,6 +20,7 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.util.List;
import org.springframework.http.HttpCookie;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.PublicDefaultServerRequest;
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -29,10 +30,10 @@ import org.springframework.web.reactive.function.server.RequestPredicate;
public class CookieRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(2, args);
String name = args[0];
String regexp = args[1];
String name = args.getString(0);
String regexp = args.getString(1);
return request -> {
//TODO: bad cast?

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.handler.predicate;
import java.util.List;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RequestPredicates;
@@ -28,10 +29,10 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
public class HeaderRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(2, args);
String header = args[0];
String regexp = args[1];
String header = args.getString(0);
String regexp = args.getString(1);
return RequestPredicates.headers(headers -> {
List<String> values = headers.asHttpHeaders().get(header);

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.tuple.Tuple;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -34,9 +35,9 @@ public class HostRequestPredicateFactory implements RequestPredicateFactory {
}
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
String pattern = args[0];
String pattern = args.getString(0);
return RequestPredicates.headers(headers -> {
String host = headers.asHttpHeaders().getFirst("Host");

View File

@@ -18,6 +18,7 @@
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.http.HttpMethod;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RequestPredicates;
@@ -27,9 +28,9 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
public class MethodRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
String method = args[0];
String method = args.getString(0);
return RequestPredicates.method(HttpMethod.resolve(method));
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RequestPredicates;
@@ -26,9 +27,9 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
public class PathRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
String pattern = args[0];
String pattern = args.getString(0);
//TODO: support custom PathPatternParser
return RequestPredicates.path(pattern);

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.PublicDefaultServerRequest;
import org.springframework.web.reactive.function.server.RequestPredicate;
import org.springframework.web.reactive.function.server.RequestPredicates;
@@ -27,11 +28,11 @@ import org.springframework.web.reactive.function.server.RequestPredicates;
public class QueryRequestPredicateFactory implements RequestPredicateFactory {
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
String param = args[0];
String param = args.getString(0);
if (args.length < 2) {
if (args.size() < 2) {
return req -> {
//TODO: ServerRequest support for query params with no value
PublicDefaultServerRequest request = (PublicDefaultServerRequest) req;
@@ -39,7 +40,7 @@ public class QueryRequestPredicateFactory implements RequestPredicateFactory {
};
}
String regexp = args[1];
String regexp = args.getString(1);
return RequestPredicates.queryParam(param, value -> value.matches(regexp));
}

View File

@@ -25,6 +25,7 @@ import java.util.Optional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.gateway.support.SubnetUtils;
import org.springframework.tuple.Tuple;
import org.springframework.web.reactive.function.server.PublicDefaultServerRequest;
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -36,13 +37,13 @@ public class RemoteAddrRequestPredicateFactory implements RequestPredicateFactor
private static final Log log = LogFactory.getLog(RemoteAddrRequestPredicateFactory.class);
@Override
public RequestPredicate apply(String... args) {
public RequestPredicate apply(Tuple args) {
validate(1, args);
List<SubnetUtils> sources = new ArrayList<>();
if (args != null) {
for (String arg : args) {
addSource(sources, arg);
for (Object arg : args.getValues()) {
addSource(sources, (String) arg);
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.gateway.handler.predicate;
import org.springframework.tuple.Tuple;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.server.RequestPredicate;
@@ -25,11 +26,10 @@ import org.springframework.web.reactive.function.server.RequestPredicate;
*/
public interface RequestPredicateFactory {
//TODO: use tuple instead of String array
RequestPredicate apply(String... args);
RequestPredicate apply(Tuple args);
default void validate(int minimumSize, String... args) {
Assert.isTrue(args != null && args.length >= minimumSize,
default void validate(int minimumSize, Tuple args) {
Assert.isTrue(args != null && args.size() >= minimumSize,
"args must have at least "+ minimumSize +" entry(s)");
}
}

View File

@@ -17,12 +17,15 @@
package org.springframework.cloud.gateway.model;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import javax.validation.ValidationException;
import javax.validation.constraints.NotNull;
import org.springframework.cloud.gateway.support.NameUtils;
import static org.springframework.util.StringUtils.tokenizeToStringArray;
/**
@@ -31,7 +34,7 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray;
public class PredicateDefinition {
@NotNull
private String name;
private String[] args;
private Map<String, String> args = new LinkedHashMap<>();
public PredicateDefinition() {
}
@@ -45,7 +48,10 @@ public class PredicateDefinition {
setName(text.substring(0, eqIdx));
String[] args = tokenizeToStringArray(text.substring(eqIdx+1), ",");
setArgs(args);
for (int i=0; i < args.length; i++) {
this.args.put(NameUtils.generateName(i), args[i]);
}
}
public String getName() {
@@ -56,11 +62,11 @@ public class PredicateDefinition {
this.name = name;
}
public String[] getArgs() {
public Map<String, String> getArgs() {
return args;
}
public void setArgs(String... args) {
public void setArgs(Map<String, String> args) {
this.args = args;
}
@@ -70,7 +76,7 @@ public class PredicateDefinition {
if (o == null || getClass() != o.getClass()) return false;
PredicateDefinition that = (PredicateDefinition) o;
return Objects.equals(name, that.name) &&
Arrays.equals(args, that.args);
Objects.equals(args, that.args);
}
@Override
@@ -82,7 +88,7 @@ public class PredicateDefinition {
public String toString() {
final StringBuilder sb = new StringBuilder("PredicateDefinition{");
sb.append("name='").append(name).append('\'');
sb.append(", args=").append(Arrays.toString(args));
sb.append(", args=").append(args);
sb.append('}');
return sb.toString();
}

View File

@@ -24,6 +24,11 @@ import org.springframework.cloud.gateway.handler.predicate.RequestPredicateFacto
* @author Spencer Gibb
*/
public class NameUtils {
public static final String GENERATED_NAME_PREFIX = "__:_._gen__+_";
public static String generateName(int i) {
return GENERATED_NAME_PREFIX + i;
}
public static String normalizePredicateName(Class<? extends RequestPredicateFactory> clazz) {
return clazz.getSimpleName().replace(RequestPredicateFactory.class.getSimpleName(), "");

View File

@@ -25,6 +25,7 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHoursMillis;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.plusHours;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.plusHoursMillis;
import static org.springframework.tuple.TupleBuilder.tuple;
/**
* @author Spencer Gibb
@@ -68,6 +69,6 @@ public class AfterRequestPredicateFactoryTests {
}
private boolean runPredicate(String dateString) {
return new AfterRequestPredicateFactory().apply(dateString).test(getRequest());
return new AfterRequestPredicateFactory().apply(tuple().of("1", dateString)).test(getRequest());
}
}

View File

@@ -25,6 +25,7 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHoursMillis;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.plusHours;
import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.plusHoursMillis;
import static org.springframework.tuple.TupleBuilder.tuple;
/**
* @author Spencer Gibb
@@ -68,6 +69,6 @@ public class BeforeRequestPredicateFactoryTests {
}
private boolean runPredicate(String dateString) {
return new BeforeRequestPredicateFactory().apply(dateString).test(getRequest());
return new BeforeRequestPredicateFactory().apply(tuple().of("1", dateString)).test(getRequest());
}
}

View File

@@ -28,6 +28,7 @@ import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.tuple.TupleBuilder.tuple;
/**
* @author Spencer Gibb
@@ -95,7 +96,7 @@ public class BetweenRequestPredicateFactoryTests {
}
boolean runPredicate(String dateString1, String dateString2) {
return new BetweenRequestPredicateFactory().apply(dateString1, dateString2).test(getRequest());
return new BetweenRequestPredicateFactory().apply(tuple().of("1", dateString1, "2", dateString2)).test(getRequest());
}
static String minusHoursMillis(int hours) {

View File

@@ -179,7 +179,8 @@ spring:
uri: ${test.uri}
predicates:
- name: Path
args: /**
args:
path: /**
#myservice:
# ribbon: