Add fullPath as convenience property on Route

More often than not what Route.path was used it had to be concatenated
with the prefix. This change just encapsulates that concern in the
Route itself.
This commit is contained in:
Dave Syer
2016-01-15 13:55:23 +00:00
parent f9fc8ba8aa
commit 98e1ac7375
7 changed files with 23 additions and 13 deletions

View File

@@ -70,7 +70,7 @@ public class RoutesEndpoint implements MvcEndpoint, ApplicationEventPublisherAwa
public Map<String, String> getRoutes() {
Map<String, String> map = new LinkedHashMap<>();
for (Route route : this.routes.getRoutes()) {
map.put(route.getPath(), route.getLocation());
map.put(route.getFullPath(), route.getLocation());
}
return map;
}

View File

@@ -16,15 +16,28 @@
package org.springframework.cloud.netflix.zuul.filters;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import lombok.Data;
@Data
@AllArgsConstructor
public class Route {
public Route(String id, String path, String location, String prefix,
Boolean retryable) {
this.id = id;
this.prefix = StringUtils.hasText(prefix) ? prefix : "";
this.path = path;
this.fullPath = prefix + path;
this.location = location;
this.retryable = retryable;
}
private String id;
private String fullPath;
private String path;
private String location;

View File

@@ -81,8 +81,9 @@ public class PreDecorationFilter extends ZuulFilter {
ctx.addOriginResponseHeader("X-Zuul-Service", location);
}
else if (location.startsWith("forward:")) {
ctx.set("forward.to", StringUtils.cleanPath(
location.substring("forward:".length()) + route.getPath()));
ctx.set("forward.to",
StringUtils.cleanPath(location.substring("forward:".length())
+ route.getPath()));
ctx.setRouteHost(null);
return null;
}

View File

@@ -95,7 +95,7 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
}
else {
for (Route route : routes) {
registerHandler(route.getPrefix() + route.getPath(), this.zuul);
registerHandler(route.getFullPath(), this.zuul);
}
}
}

View File

@@ -84,7 +84,7 @@ public class SampleZuulProxyAppTestsWithHttpClient {
private String getRoute(String path) {
for (Route route : this.routes.getRoutes()) {
if (path.equals(route.getPrefix() + route.getPath())) {
if (path.equals(route.getFullPath())) {
return route.getLocation();
}
}

View File

@@ -90,7 +90,7 @@ public class SampleZuulProxyApplicationTests {
private String getRoute(String path) {
for (Route route : this.routes.getRoutes()) {
if (path.equals(route.getPrefix() + route.getPath())) {
if (path.equals(route.getFullPath())) {
return route.getLocation();
}
}

View File

@@ -29,7 +29,6 @@ import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -590,10 +589,7 @@ public class DiscoveryClientRouteLocatorTests {
private Route getRoute(List<Route> routes, String path) {
for (Route route : routes) {
String pattern = route.getPath();
if (StringUtils.hasText(route.getPrefix())) {
pattern = route.getPrefix() + route.getPath();
}
String pattern = route.getFullPath();
if (path.equals(pattern)) {
return route;
}