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 feea79ae..38065a5b 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 @@ -87,6 +87,11 @@ public class ResourceController { synchronized String retrieve(String name, String profile, String label, String path, boolean resolvePlaceholders) throws IOException { + if (name != null && name.contains("(_)")) { + // "(_)" is uncommon in a git repo name, but "/" cannot be matched + // by Spring MVC + name = name.replace("(_)", "/"); + } if (label != null && label.contains("(_)")) { // "(_)" is uncommon in a git branch name, but "/" cannot be matched // by Spring MVC @@ -116,6 +121,11 @@ public class ResourceController { synchronized byte[] binary(String name, String profile, String label, String path) throws IOException { + if (name != null && name.contains("(_)")) { + // "(_)" is uncommon in a git repo name, but "/" cannot be matched + // by Spring MVC + name = name.replace("(_)", "/"); + } if (label != null && label.contains("(_)")) { // "(_)" is uncommon in a git branch name, but "/" cannot be matched // by Spring MVC 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 d09fa5d9..9548b014 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 @@ -17,6 +17,7 @@ package org.springframework.cloud.config.server.resource; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.After; @@ -87,6 +88,52 @@ public class ResourceControllerTests { assertEquals("foo: ${foo}", resource); } + @Test + public void applicationAndLabelPlaceholdersWithoutSlash() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}/{label}"); + String resource = this.controller.retrieve("dev", "bar", "spam", "foo.txt", true); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void applicationPlaceholderWithSlash() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}"); + String resource = this.controller.retrieve("dev(_)spam", "bar", "", "foo.txt", true); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void applicationPlaceholderWithSlashNullLabel() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}"); + String resource = this.controller.retrieve("dev(_)spam", "bar", null, "foo.txt", true); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void labelPlaceholderWithSlash() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{label}"); + String resource = this.controller.retrieve("dev", "bar", "dev(_)spam", "foo.txt", true); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void profilePlaceholderNullLabel() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{profile}"); + String resource = this.controller.retrieve("bar", "dev", null, "spam/foo.txt", true); + assertEquals("foo: dev_bar/spam", resource); + } + + @Test + public void nullNameAndLabel() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + try { + this.controller.retrieve(null, "foo", "bar", "spam/foo.txt", true); + } + catch (Exception e) { + assertNotNull(e); + } + } + @Test public void labelWithSlash() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test"); @@ -143,6 +190,52 @@ public class ResourceControllerTests { assertEquals("foo: dev_bar/spam", resource); } + @Test + public void applicationAndLabelPlaceholdersWithoutSlashForBinary() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}/{label}"); + byte[] resource = this.controller.binary("dev", "bar", "spam", "foo.txt"); + assertEquals("foo: dev_bar/spam", new String(resource)); + } + + @Test + public void applicationPlaceholderWithSlashForBinary() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}"); + byte[] resource = this.controller.binary("dev(_)spam", "bar", "", "foo.txt"); + assertEquals("foo: dev_bar/spam", new String(resource)); + } + + @Test + public void applicationPlaceholderWithSlashForBinaryNullLabel() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{application}"); + byte[] resource = this.controller.binary("dev(_)spam", "bar", null, "foo.txt"); + assertEquals("foo: dev_bar/spam", new String(resource)); + } + + @Test + public void labelPlaceholderWithSlashForBinary() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{label}"); + byte[] resource = this.controller.binary("dev", "bar", "dev(_)spam", "foo.txt"); + assertEquals("foo: dev_bar/spam", new String(resource)); + } + + @Test + public void profilePlaceholderForBinaryNullLabel() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test/{profile}"); + byte[] resource = this.controller.binary("bar", "dev", null, "spam/foo.txt"); + assertEquals("foo: dev_bar/spam", new String(resource)); + } + + @Test + public void forBinaryNullName() throws Exception { + this.environmentRepository.setSearchLocations("classpath:/test"); + try { + this.controller.binary(null, "foo", "bar", "spam/foo.txt"); + } + catch (Exception e) { + assertNotNull(e); + } + } + @Test public void labelWithSlashForBinary() throws Exception { this.environmentRepository.setSearchLocations("classpath:/test");