Browse Source

Adds PrefixPathRouteFilter

pull/41/head
Spencer Gibb 8 years ago
parent
commit
67162a9775
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 3
      docs/src/main/asciidoc/spring-cloud-gateway.adoc
  2. 6
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  3. 45
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/route/PrefixPathRouteFilter.java
  4. 21
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java

3
docs/src/main/asciidoc/spring-cloud-gateway.adoc

@ -143,6 +143,9 @@ TODO: document AddResponseHeader Route Filter @@ -143,6 +143,9 @@ TODO: document AddResponseHeader Route Filter
=== Hystrix Route Filter
TODO: document Hystrix Route Filter
=== PrefixPath Route Filter
TODO: document PrefixPath Route Filter
=== RedirectTo Route Filter
TODO: document RedirectTo Route Filter

6
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean @@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.gateway.actuate.GatewayEndpoint;
import org.springframework.cloud.gateway.filter.route.PrefixPathRouteFilter;
import org.springframework.cloud.gateway.model.RouteLocator;
import org.springframework.cloud.gateway.model.RouteWriter;
import org.springframework.cloud.gateway.filter.GlobalFilter;
@ -236,6 +237,11 @@ public class GatewayAutoConfiguration { @@ -236,6 +237,11 @@ public class GatewayAutoConfiguration {
}
}
@Bean(name = "PrefixPathRouteFilter")
public PrefixPathRouteFilter prefixPathRouteFilter() {
return new PrefixPathRouteFilter();
}
@Bean(name = "RedirectToRouteFilter")
public RedirectToRouteFilter redirectToRouteFilter() {
return new RedirectToRouteFilter();

45
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/route/PrefixPathRouteFilter.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.filter.route;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.WebFilter;
/**
* @author Spencer Gibb
*/
public class PrefixPathRouteFilter implements RouteFilter {
@Override
@SuppressWarnings("unchecked")
public WebFilter apply(String... args) {
validate(1, args);
final String prefix = args[0];
return (exchange, chain) -> {
ServerHttpRequest req = exchange.getRequest();
String newPath = prefix + req.getURI().getPath();
ServerHttpRequest request = req.mutate()
.path(newPath)
.build();
return chain.filter(exchange.mutate().request(request).build());
};
}
}

21
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java

@ -159,27 +159,6 @@ public class BaseWebClientTests { @@ -159,27 +159,6 @@ public class BaseWebClientTests {
});
}
@Bean(name="PrefixPathRouteFilter")
public RouteFilter prefixPathRouteFilter() {
return new RouteFilter() {
@Override
public WebFilter apply(String... args) {
validate(1, args);
final String prefix = args[0];
return (exchange, chain) -> {
ServerHttpRequest req = exchange.getRequest();
String newPath = prefix + req.getURI().getPath();
ServerHttpRequest request = req.mutate()
.path(newPath)
.build();
return chain.filter(exchange.mutate().request(request).build());
};
}
};
}
@Bean
@Order(500)
public GlobalFilter modifyResponseFilter() {

Loading…
Cancel
Save