Browse Source

Change Filter from (value, args[]) to (args...)

pull/41/head
Spencer Gibb 8 years ago
parent
commit
06b5211a39
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 23
      src/main/java/org/springframework/cloud/gateway/config/FilterDefinition.java
  2. 5
      src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java
  3. 7
      src/main/java/org/springframework/cloud/gateway/filter/route/AddRequestHeaderRouteFilter.java
  4. 9
      src/main/java/org/springframework/cloud/gateway/filter/route/AddRequestParameterRouteFilter.java
  5. 9
      src/main/java/org/springframework/cloud/gateway/filter/route/AddResponseHeaderRouteFilter.java
  6. 4
      src/main/java/org/springframework/cloud/gateway/filter/route/HystrixRouteFilter.java
  7. 11
      src/main/java/org/springframework/cloud/gateway/filter/route/RedirectToRouteFilter.java
  8. 5
      src/main/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilter.java
  9. 5
      src/main/java/org/springframework/cloud/gateway/filter/route/RemoveResponseHeaderRouteFilter.java
  10. 8
      src/main/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilter.java
  11. 4
      src/main/java/org/springframework/cloud/gateway/filter/route/RouteFilter.java
  12. 6
      src/main/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilter.java
  13. 9
      src/main/java/org/springframework/cloud/gateway/filter/route/SetResponseHeaderRouteFilter.java
  14. 6
      src/main/java/org/springframework/cloud/gateway/filter/route/SetStatusRouteFilter.java
  15. 5
      src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java
  16. 2
      src/test/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilterTests.java
  17. 2
      src/test/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilterTests.java
  18. 3
      src/test/resources/application.yml

23
src/main/java/org/springframework/cloud/gateway/config/FilterDefinition.java

@ -14,9 +14,6 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray; @@ -14,9 +14,6 @@ import static org.springframework.util.StringUtils.tokenizeToStringArray;
public class FilterDefinition {
@NotNull
private String name;
@NotNull
private String value;
private String[] args;
public FilterDefinition() {
@ -32,11 +29,7 @@ public class FilterDefinition { @@ -32,11 +29,7 @@ public class FilterDefinition {
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 +40,11 @@ public class FilterDefinition { @@ -47,19 +40,11 @@ public class FilterDefinition {
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 +54,18 @@ public class FilterDefinition { @@ -69,20 +54,18 @@ public class FilterDefinition {
if (o == null || getClass() != o.getClass()) return false;
FilterDefinition that = (FilterDefinition) 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("FilterDefinition{");
sb.append("name='").append(name).append('\'');
sb.append(", value='").append(value).append('\'');
sb.append(", args=").append(Arrays.toString(args));
sb.append('}');
return sb.toString();

5
src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java

@ -43,8 +43,9 @@ public class DiscoveryClientRouteReader implements RouteReader { @@ -43,8 +43,9 @@ public class DiscoveryClientRouteReader implements RouteReader {
// add a filter that removes /serviceId by default
FilterDefinition filter = new FilterDefinition();
filter.setName("RewritePath");
filter.setValue("/" + serviceId + "/(?<remaining>.*)");
filter.setArgs(new String[]{ "/${remaining}" });
String regex = "/" + serviceId + "/(?<remaining>.*)";
String replacement = "/${remaining}";
filter.setArgs(regex, replacement);
route.getFilters().add(filter);
//TODO: support for default filters

7
src/main/java/org/springframework/cloud/gateway/filter/route/AddRequestHeaderRouteFilter.java

@ -9,13 +9,12 @@ import org.springframework.http.server.reactive.ServerHttpRequest; @@ -9,13 +9,12 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
public class AddRequestHeaderRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String header, String[] args) {
validate(args, 1);
public WebFilter apply(String... args) {
validate(2, args);
//TODO: caching can happen here
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest().mutate()
.header(header, args[0])
.header(args[0], args[1])
.build();
return chain.filter(exchange.mutate().request(request).build());

9
src/main/java/org/springframework/cloud/gateway/filter/route/AddRequestParameterRouteFilter.java

@ -17,10 +17,11 @@ import org.springframework.web.server.WebFilter; @@ -17,10 +17,11 @@ import org.springframework.web.server.WebFilter;
public class AddRequestParameterRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String parameter, String[] args) {
validate(args, 1);
public WebFilter apply(String... args) {
validate(2, args);
String parameter = args[0];
String value = args[1];
//TODO: caching can happen here
return (exchange, chain) -> {
URI uri = exchange.getRequest().getURI();
@ -37,7 +38,7 @@ public class AddRequestParameterRouteFilter implements RouteFilter { @@ -37,7 +38,7 @@ public class AddRequestParameterRouteFilter implements RouteFilter {
//TODO urlencode?
query.append(parameter);
query.append('=');
query.append(args[0]);
query.append(value);
ServerHttpRequest request = new QueryParamServerHttpRequestBuilder(exchange.getRequest())
.query(query.toString())

9
src/main/java/org/springframework/cloud/gateway/filter/route/AddResponseHeaderRouteFilter.java

@ -8,12 +8,13 @@ import org.springframework.web.server.WebFilter; @@ -8,12 +8,13 @@ import org.springframework.web.server.WebFilter;
public class AddResponseHeaderRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String header, String[] args) {
validate(args, 1);
public WebFilter apply(String... args) {
validate(2, args);
final String header = args[0];
final String value = args[1];
//TODO: caching can happen here
return (exchange, chain) -> {
exchange.getResponse().getHeaders().add(header, args[0]);
exchange.getResponse().getHeaders().add(header, value);
return chain.filter(exchange);
};

4
src/main/java/org/springframework/cloud/gateway/filter/route/HystrixRouteFilter.java

@ -19,7 +19,9 @@ import rx.Subscription; @@ -19,7 +19,9 @@ import rx.Subscription;
public class HystrixRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String commandName, String[] args) {
public WebFilter apply(String... args) {
validate(1, args);
final String commandName = args[0];
final HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey(getClass().getSimpleName());
final HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey(commandName);

11
src/main/java/org/springframework/cloud/gateway/filter/route/RedirectToRouteFilter.java

@ -20,18 +20,19 @@ import reactor.core.publisher.Mono; @@ -20,18 +20,19 @@ import reactor.core.publisher.Mono;
*/
public class RedirectToRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String statusString, String[] args) {
validate(args, 1);
public WebFilter apply(String... args) {
validate(2, args);
final String statusString = args[0];
final String uri = args[1];
final HttpStatus httpStatus = parse(statusString);
final URL url;
try {
url = URI.create(args[0]).toURL();
url = URI.create(uri).toURL();
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid url " + args[0], e);
throw new IllegalArgumentException("Invalid url " + uri, e);
}
//TODO: caching can happen here
return (exchange, chain) ->
chain.filter(exchange).then(() -> {
if (!isResponseCommitted(exchange)) {

5
src/main/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilter.java

@ -11,9 +11,10 @@ public class RemoveRequestHeaderRouteFilter implements RouteFilter { @@ -11,9 +11,10 @@ public class RemoveRequestHeaderRouteFilter implements RouteFilter {
public static final String FAKE_HEADER = "_______force_______";
@Override
public WebFilter apply(String header, String[] args) {
public WebFilter apply(String... args) {
validate(1, args);
final String header = args[0];
//TODO: caching can happen here
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest().mutate()
.header(FAKE_HEADER, "mutable") //TODO: is there a better way?

5
src/main/java/org/springframework/cloud/gateway/filter/route/RemoveResponseHeaderRouteFilter.java

@ -8,9 +8,10 @@ import org.springframework.web.server.WebFilter; @@ -8,9 +8,10 @@ import org.springframework.web.server.WebFilter;
public class RemoveResponseHeaderRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String header, String[] args) {
public WebFilter apply(String... args) {
validate(1, args);
final String header = args[0];
//TODO: caching can happen here
return (exchange, chain) -> {
exchange.getResponse().getHeaders().remove(header);

8
src/main/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilter.java

@ -9,11 +9,11 @@ import org.springframework.http.server.reactive.ServerHttpRequest; @@ -9,11 +9,11 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
public class RewritePathRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String regex, String[] args) {
validate(args, 1);
String replacement = args[0].replace("$\\", "$");
public WebFilter apply(String... args) {
validate(2, args);
final String regex = args[0];
String replacement = args[1].replace("$\\", "$");
//TODO: caching can happen here
return (exchange, chain) -> {
ServerHttpRequest req = exchange.getRequest();
String path = req.getURI().getPath();

4
src/main/java/org/springframework/cloud/gateway/filter/route/RouteFilter.java

@ -8,9 +8,9 @@ import org.springframework.web.server.WebFilter; @@ -8,9 +8,9 @@ import org.springframework.web.server.WebFilter;
*/
public interface RouteFilter {
WebFilter apply(String value, String[] args);
WebFilter apply(String... args);
default void validate(String[] args, int requiredSize) {
default void validate(int requiredSize, String... args) {
Assert.isTrue(args != null && args.length == requiredSize,
"args must have "+ requiredSize +" entry(s)");
}

6
src/main/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilter.java

@ -17,10 +17,10 @@ public class SetPathRouteFilter implements RouteFilter { @@ -17,10 +17,10 @@ public class SetPathRouteFilter implements RouteFilter {
@Override
@SuppressWarnings("unchecked")
public WebFilter apply(String template, String[] args) {
UriTemplate uriTemplate = new UriTemplate(template);
public WebFilter apply(String... args) {
validate(1, args);
UriTemplate uriTemplate = new UriTemplate(args[0]);
//TODO: caching can happen here
return (exchange, chain) -> {
Map<String, String> variables = getAttribute(exchange, URL_PREDICATE_VARS_ATTR, Map.class);
ServerHttpRequest req = exchange.getRequest();

9
src/main/java/org/springframework/cloud/gateway/filter/route/SetResponseHeaderRouteFilter.java

@ -8,12 +8,13 @@ import org.springframework.web.server.WebFilter; @@ -8,12 +8,13 @@ import org.springframework.web.server.WebFilter;
public class SetResponseHeaderRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String header, String[] args) {
validate(args, 1);
public WebFilter apply(String... args) {
validate(2, args);
final String header = args[0];
final String value = args[1];
//TODO: caching can happen here
return (exchange, chain) -> {
exchange.getResponse().getHeaders().set(header, args[0]);
exchange.getResponse().getHeaders().set(header, value);
return chain.filter(exchange);
};

6
src/main/java/org/springframework/cloud/gateway/filter/route/SetStatusRouteFilter.java

@ -15,10 +15,10 @@ import reactor.core.publisher.Mono; @@ -15,10 +15,10 @@ import reactor.core.publisher.Mono;
public class SetStatusRouteFilter implements RouteFilter {
@Override
public WebFilter apply(String statusString, String[] args) {
final HttpStatus httpStatus = ServerWebExchangeUtils.parse(statusString);
public WebFilter apply(String... args) {
validate(1, args);
final HttpStatus httpStatus = ServerWebExchangeUtils.parse(args[0]);
//TODO: caching can happen here
return (exchange, chain) -> {
// option 1 (runs in filter order)

5
src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java

@ -143,10 +143,9 @@ public class FilteringWebHandler extends WebHandlerDecorator { @@ -143,10 +143,9 @@ public class FilteringWebHandler extends WebHandlerDecorator {
} else {
args = Collections.emptyList();
}
logger.debug("Route " + route.getId() + " applying filter " + definition.getValue()
+ ", " + args + " to " + definition.getName());
logger.debug("Route " + route.getId() + " applying filter " + args + " to " + definition.getName());
}
return filter.apply(definition.getValue(), definition.getArgs());
return filter.apply(definition.getArgs());
})
.collect(Collectors.toList());

2
src/test/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilterTests.java

@ -30,7 +30,7 @@ public class RewritePathRouteFilterTests { @@ -30,7 +30,7 @@ public class RewritePathRouteFilterTests {
}
private void testRewriteFilter(String regex, String replacement, String actualPath, String expectedPath) {
WebFilter filter = new RewritePathRouteFilter().apply(regex, new String[]{replacement});
WebFilter filter = new RewritePathRouteFilter().apply(regex, replacement);
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost"+ actualPath)

2
src/test/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilterTests.java

@ -37,7 +37,7 @@ public class SetPathRouteFilterTests { @@ -37,7 +37,7 @@ public class SetPathRouteFilterTests {
}
private void testRewriteFilter(String template, String actualPath, String expectedPath, HashMap<String, String> variables) {
WebFilter filter = new SetPathRouteFilter().apply(template, new String[]{});
WebFilter filter = new SetPathRouteFilter().apply(new String[]{template});
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost"+ actualPath)

3
src/test/resources/application.yml

@ -124,7 +124,8 @@ spring: @@ -124,7 +124,8 @@ spring:
- Host=**.setstatusint.org
- Url=/headers
filters:
- SetStatus=401
- name: SetStatus
args: 401
# =====================================
- id: set_status_string_test

Loading…
Cancel
Save