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 80f77733..feea79ae 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 @@ -25,8 +25,8 @@ import java.nio.charset.Charset; import javax.servlet.http.HttpServletRequest; +import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.server.environment.EnvironmentRepository; -import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.Resource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -35,6 +35,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UrlPathHelper; @@ -68,10 +69,12 @@ public class ResourceController { } @RequestMapping("/{name}/{profile}/{label}/**") - public String resolve(@PathVariable String name, @PathVariable String profile, - @PathVariable String label, HttpServletRequest request) throws IOException { + public String retrieve(@PathVariable String name, @PathVariable String profile, + @PathVariable String label, HttpServletRequest request, + @RequestParam(defaultValue = "true") boolean resolvePlaceholders) + throws IOException { String path = getFilePath(request, name, profile, label); - return resolve(name, profile, label, path); + return retrieve(name, profile, label, path, resolvePlaceholders); } private String getFilePath(HttpServletRequest request, String name, String profile, @@ -82,21 +85,24 @@ public class ResourceController { return path; } - synchronized String resolve(String name, String profile, String label, String path) - throws IOException { + synchronized String retrieve(String name, String profile, String label, String path, + boolean resolvePlaceholders) throws IOException { if (label != null && label.contains("(_)")) { // "(_)" is uncommon in a git branch name, but "/" cannot be matched // by Spring MVC label = label.replace("(_)", "/"); } - StandardEnvironment environment = prepareEnvironment( - this.environmentRepository.findOne(name, profile, label)); // ensure InputStream will be closed to prevent file locks on Windows try (InputStream is = this.resourceRepository.findOne(name, profile, label, path) .getInputStream()) { String text = StreamUtils.copyToString(is, Charset.forName("UTF-8")); - return resolvePlaceholders(environment, text); + if (resolvePlaceholders) { + Environment environment = this.environmentRepository.findOne(name, + profile, label); + text = resolvePlaceholders(prepareEnvironment(environment), text); + } + return text; } } 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 5d693b23..d09fa5d9 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 @@ -62,9 +62,16 @@ public class ResourceControllerTests { @Test public void templateReplacement() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); - String resource = this.controller.resolve("foo", "bar", "dev", "template.json"); + String resource = this.controller.retrieve("foo", "bar", "dev", "template.json", true); assertTrue("Wrong content: " + resource, resource.matches("\\{\\s*\"foo\": \"dev_bar\"\\s*\\}")); } + + @Test + public void templateReplacementNotForResolvePlaceholdersFalse() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + String resource = this.controller.retrieve("foo", "bar", "dev", "template.json", false); + assertTrue("Wrong content: " + resource, resource.matches("\\{\\s*\"foo\": \"\\$\\{foo\\}\"\\s*\\}")); + } @Test public void templateReplacementNotForBinary() throws Exception { @@ -76,21 +83,21 @@ public class ResourceControllerTests { @Test public void escapedPlaceholder() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); - String resource = this.controller.resolve("foo", "bar", "dev", "placeholder.txt"); + String resource = this.controller.retrieve("foo", "bar", "dev", "placeholder.txt", true); assertEquals("foo: ${foo}", resource); } @Test public void labelWithSlash() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); - String resource = this.controller.resolve("foo", "bar", "dev(_)spam", "foo.txt"); + String resource = this.controller.retrieve("foo", "bar", "dev(_)spam", "foo.txt", true); assertEquals("foo: dev_bar/spam", resource); } @Test public void resourceWithSlash() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); - String resource = this.controller.resolve("foo", "bar", "dev", "spam/foo.txt"); + String resource = this.controller.retrieve("foo", "bar", "dev", "spam/foo.txt", true); assertEquals("foo: dev_bar/spam", resource); } @@ -99,7 +106,7 @@ public class ResourceControllerTests { this.environmentRepository.setSearchLocations("classpath:/test"); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); - String resource = this.controller.resolve("foo", "bar", "dev", request); + String resource = this.controller.retrieve("foo", "bar", "dev", request, true); assertEquals("foo: dev_bar/spam", resource); } @@ -109,10 +116,33 @@ public class ResourceControllerTests { MockHttpServletRequest request = new MockHttpServletRequest(); request.setServletPath("/spring"); request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); - String resource = this.controller.resolve("foo", "bar", "dev", request); + String resource = this.controller.retrieve("foo", "bar", "dev", request, true); assertEquals("foo: dev_bar/spam", resource); } + @Test + public void labelWithSlashForResolvePlaceholdersFalse() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + String resource = this.controller.retrieve("foo", "bar", "dev(_)spam", "foo.txt", false); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void resourceWithSlashForResolvePlaceholdersFalse() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + String resource = this.controller.retrieve("foo", "bar", "dev", "spam/foo.txt", false); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void resourceWithSlashForResolvePlaceholdersFalseRequest() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.setRequestURI("/foo/bar/dev/" + "spam/foo.txt"); + String resource = this.controller.retrieve("foo", "bar", "dev", request, false); + assertEquals("foo: dev_bar/spam", resource); + } + @Test public void labelWithSlashForBinary() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test");