Commit f9fc8ba8 by Dave Syer

Change signature of RouteLocator.getRoutes()

If it returns Routes instead of Strings it's a lot more obvious what it's supposed.
parent 2585798a
...@@ -16,11 +16,13 @@ ...@@ -16,11 +16,13 @@
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationEventPublisherAware;
...@@ -66,7 +68,11 @@ public class RoutesEndpoint implements MvcEndpoint, ApplicationEventPublisherAwa ...@@ -66,7 +68,11 @@ public class RoutesEndpoint implements MvcEndpoint, ApplicationEventPublisherAwa
@ResponseBody @ResponseBody
@ManagedAttribute @ManagedAttribute
public Map<String, String> getRoutes() { public Map<String, String> getRoutes() {
return this.routes.getRoutes(); Map<String, String> map = new LinkedHashMap<>();
for (Route route : this.routes.getRoutes()) {
map.put(route.getPath(), route.getLocation());
}
return map;
} }
@Override @Override
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
package org.springframework.cloud.netflix.zuul.filters; package org.springframework.cloud.netflix.zuul.filters;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.List;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -32,7 +32,7 @@ public interface RouteLocator { ...@@ -32,7 +32,7 @@ public interface RouteLocator {
/** /**
* A map of route path (pattern) to location (e.g. service id or URL). * A map of route path (pattern) to location (e.g. service id or URL).
*/ */
Map<String, String> getRoutes(); List<Route> getRoutes();
/** /**
* Maps a path to an actual route with full metadata. * Maps a path to an actual route with full metadata.
......
...@@ -16,8 +16,10 @@ ...@@ -16,8 +16,10 @@
package org.springframework.cloud.netflix.zuul.filters; package org.springframework.cloud.netflix.zuul.filters;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
...@@ -58,13 +60,15 @@ public class SimpleRouteLocator implements RouteLocator { ...@@ -58,13 +60,15 @@ public class SimpleRouteLocator implements RouteLocator {
} }
@Override @Override
public Map<String, String> getRoutes() { public List<Route> getRoutes() {
if (this.routes.get() == null) { if (this.routes.get() == null) {
this.routes.set(locateRoutes()); this.routes.set(locateRoutes());
} }
Map<String, String> values = new LinkedHashMap<>(); List<Route> values = new ArrayList<>();
for (String url : this.routes.get().keySet()) { for (String url : this.routes.get().keySet()) {
values.put(url, this.routes.get().get(url).getLocation()); ZuulRoute route = this.routes.get().get(url);
String path = route.getPath();
values.add(getRoute(route, path));
} }
return values; return values;
} }
......
...@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator; import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.util.PatternMatchUtils; import org.springframework.util.PatternMatchUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
...@@ -88,13 +89,13 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping { ...@@ -88,13 +89,13 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping {
} }
private void registerHandlers() { private void registerHandlers() {
Collection<String> routes = this.routeLocator.getRoutes().keySet(); Collection<Route> routes = this.routeLocator.getRoutes();
if (routes.isEmpty()) { if (routes.isEmpty()) {
this.logger.warn("No routes found from RouteLocator"); this.logger.warn("No routes found from RouteLocator");
} }
else { else {
for (String url : routes) { for (Route route : routes) {
registerHandler(url, this.zuul); registerHandler(route.getPrefix() + route.getPath(), this.zuul);
} }
} }
} }
......
...@@ -33,6 +33,7 @@ import org.springframework.boot.test.TestRestTemplate; ...@@ -33,6 +33,7 @@ import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator; import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
...@@ -82,7 +83,12 @@ public class SampleZuulProxyAppTestsWithHttpClient { ...@@ -82,7 +83,12 @@ public class SampleZuulProxyAppTestsWithHttpClient {
private RibbonCommandFactory<?> ribbonCommandFactory; private RibbonCommandFactory<?> ribbonCommandFactory;
private String getRoute(String path) { private String getRoute(String path) {
return this.routes.getRoutes().get(path); for (Route route : this.routes.getRoutes()) {
if (path.equals(route.getPrefix() + route.getPath())) {
return route.getLocation();
}
}
return null;
} }
@Test @Test
......
...@@ -36,6 +36,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient; ...@@ -36,6 +36,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.StaticServerList; import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator; import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory;
...@@ -88,7 +89,12 @@ public class SampleZuulProxyApplicationTests { ...@@ -88,7 +89,12 @@ public class SampleZuulProxyApplicationTests {
private RibbonCommandFactory<?> ribbonCommandFactory; private RibbonCommandFactory<?> ribbonCommandFactory;
private String getRoute(String path) { private String getRoute(String path) {
return this.routes.getRoutes().get(path); for (Route route : this.routes.getRoutes()) {
if (path.equals(route.getPrefix() + route.getPath())) {
return route.getLocation();
}
}
return null;
} }
@Test @Test
......
...@@ -57,7 +57,7 @@ public class SimpleZuulServerApplicationTests { ...@@ -57,7 +57,7 @@ public class SimpleZuulServerApplicationTests {
private RouteLocator routes; private RouteLocator routes;
private String getRoute(String path) { private String getRoute(String path) {
return this.routes.getRoutes().get(path); return this.routes.getMatchingRoute(path).getLocation();
} }
@Test @Test
......
...@@ -22,6 +22,7 @@ import org.junit.Before; ...@@ -22,6 +22,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
...@@ -53,8 +54,8 @@ public class ZuulHandlerMappingTests { ...@@ -53,8 +54,8 @@ public class ZuulHandlerMappingTests {
@Test @Test
public void mappedPath() throws Exception { public void mappedPath() throws Exception {
Mockito.when(this.locator.getRoutes()) Mockito.when(this.locator.getRoutes()).thenReturn(
.thenReturn(Collections.singletonMap("/foo/**", "foo")); Collections.singletonList(new Route("foo", "/foo/**", "foo", "", null)));
this.request.setServletPath("/foo/"); this.request.setServletPath("/foo/");
this.mapping.setDirty(true); this.mapping.setDirty(true);
assertNotNull(this.mapping.getHandler(this.request)); assertNotNull(this.mapping.getHandler(this.request));
...@@ -62,8 +63,8 @@ public class ZuulHandlerMappingTests { ...@@ -62,8 +63,8 @@ public class ZuulHandlerMappingTests {
@Test @Test
public void defaultPath() throws Exception { public void defaultPath() throws Exception {
Mockito.when(this.locator.getRoutes()) Mockito.when(this.locator.getRoutes()).thenReturn(
.thenReturn(Collections.singletonMap("/**", "default")); Collections.singletonList(new Route("default", "/**", "foo", "", null)));
; ;
this.request.setServletPath("/"); this.request.setServletPath("/");
this.mapping.setDirty(true); this.mapping.setDirty(true);
...@@ -72,8 +73,8 @@ public class ZuulHandlerMappingTests { ...@@ -72,8 +73,8 @@ public class ZuulHandlerMappingTests {
@Test @Test
public void errorPath() throws Exception { public void errorPath() throws Exception {
Mockito.when(this.locator.getRoutes()) Mockito.when(this.locator.getRoutes()).thenReturn(
.thenReturn(Collections.singletonMap("/**", "default")); Collections.singletonList(new Route("default", "/**", "foo", "", null)));
this.request.setServletPath("/error"); this.request.setServletPath("/error");
this.mapping.setDirty(true); this.mapping.setDirty(true);
assertNull(this.mapping.getHandler(this.request)); assertNull(this.mapping.getHandler(this.request));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment