From 984cc5b8859aed2871d116861b6fb17cf51527e6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 3 May 2016 09:11:28 +0100 Subject: [PATCH] Fix bug in request handling For some reason path info is always null(?), so the way to get the resource path is to use request.getRequestURI(). --- .../server/resource/ResourceController.java | 18 ++++++++++++++---- .../resource/ResourceControllerTests.java | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java index ba25bf72..80f77733 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/resource/ResourceController.java @@ -37,6 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UrlPathHelper; /** * An HTTP endpoint for serving up templated plain text resources from an underlying @@ -57,20 +58,30 @@ public class ResourceController { private EnvironmentRepository environmentRepository; + private UrlPathHelper helper = new UrlPathHelper(); + public ResourceController(ResourceRepository resourceRepository, EnvironmentRepository environmentRepository) { this.resourceRepository = resourceRepository; this.environmentRepository = environmentRepository; + this.helper.setAlwaysUseFullPath(true); } @RequestMapping("/{name}/{profile}/{label}/**") public String resolve(@PathVariable String name, @PathVariable String profile, @PathVariable String label, HttpServletRequest request) throws IOException { - String path = request.getPathInfo() - .substring(String.format("/%s/%s/%s/", name, profile, label).length()); + String path = getFilePath(request, name, profile, label); return resolve(name, profile, label, path); } + private String getFilePath(HttpServletRequest request, String name, String profile, + String label) { + String stem = String.format("/%s/%s/%s/", name, profile, label); + String path = this.helper.getPathWithinApplication(request); + path = path.substring(path.indexOf(stem) + stem.length()); + return path; + } + synchronized String resolve(String name, String profile, String label, String path) throws IOException { if (label != null && label.contains("(_)")) { @@ -93,8 +104,7 @@ public class ResourceController { public synchronized byte[] binary(@PathVariable String name, @PathVariable String profile, @PathVariable String label, HttpServletRequest request) throws IOException { - String path = request.getPathInfo() - .substring(String.format("/%s/%s/%s/", name, profile, label).length()); + String path = getFilePath(request, name, profile, label); return binary(name, profile, label, path); } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java index d0927220..5d693b23 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/resource/ResourceControllerTests.java @@ -98,7 +98,17 @@ public class ResourceControllerTests { public void resourceWithSlashRequest() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); MockHttpServletRequest request = new MockHttpServletRequest(); - request.setPathInfo("/foo/bar/dev/" + "spam/foo.txt"); + request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); + String resource = this.controller.resolve("foo", "bar", "dev", request); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void resourceWithSlashRequestAndServletPath() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setServletPath("/spring"); + request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); String resource = this.controller.resolve("foo", "bar", "dev", request); assertEquals("foo: dev_bar/spam", resource); } @@ -121,7 +131,7 @@ public class ResourceControllerTests { public void resourceWithSlashForBinaryRequest() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); MockHttpServletRequest request = new MockHttpServletRequest(); - request.setPathInfo("/foo/bar/dev/" + "spam/foo.txt"); + request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); byte[] resource = this.controller.binary("foo", "bar", "dev", request ); assertEquals("foo: dev_bar/spam", new String(resource)); }