diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactory.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactory.java index c9ad4205..f86a33ae 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactory.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactory.java @@ -137,11 +137,18 @@ public class HttpClientConfigurableHttpConnectionFactory implements Configurable // if token[0] equals url then there was no placeholder in the the url, so // matching needed if (tokens.length >= 1 && !tokens[0].equals(url.toString())) { - List placeholders = getPlaceholders(key); + List placeholders = getPlaceholders(key); List values = getValues(spec, tokens); if (placeholders.size() == values.size()) { for (int i = 0; i < values.size(); i++) { - spec = spec.replace(values.get(i), String.format("{%s}", placeholders.get(i))); + // if the key does not start with first part of the spec before the + // place holder then its + // not a match + String specBeforePlaceholder = spec.substring(0, placeholders.get(i).start); + if (key.startsWith(specBeforePlaceholder)) { + spec = specBeforePlaceholder + "{" + placeholders.get(i).group + "}" + + spec.substring(placeholders.get(i).start + values.get(i).length()); + } } } } @@ -165,14 +172,30 @@ public class HttpClientConfigurableHttpConnectionFactory implements Configurable return values; } - private List getPlaceholders(String key) { + private List getPlaceholders(String key) { Pattern pattern = Pattern.compile(PLACEHOLDER_PATTERN_STRING); Matcher matcher = pattern.matcher(key); - List placeholders = new LinkedList<>(); + List placeholders = new LinkedList<>(); while (matcher.find()) { - placeholders.add(matcher.group(1)); + placeholders.add(new Placeholder(matcher.group(1), matcher.start(), matcher.end())); } return placeholders; } + private static class Placeholder { + + String group; + + int start; + + int end; + + Placeholder(String group, int start, int end) { + this.start = start; + this.end = end; + this.group = group; + } + + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactoryTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactoryTest.java index 43948763..39954e62 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactoryTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/HttpClientConfigurableHttpConnectionFactoryTest.java @@ -221,6 +221,51 @@ public class HttpClientConfigurableHttpConnectionFactoryTest { assertThat(actualHttpClientBuilder).isSameAs(expectedHttpClientBuilder); } + @Test + public void applicationNameAlsoOccursInBaseURL() throws Exception { + MultipleJGitEnvironmentProperties properties = new MultipleJGitEnvironmentProperties(); + properties.setUri("http://server.com/{placeholder}-test.git"); + this.connectionFactory.addConfiguration(properties); + + HttpConnection actualConnection = this.connectionFactory + .create(new URL("http://server.com/server-test.git" + "/some/path.properties")); + + HttpClientBuilder expectedHttpClientBuilder = this.connectionFactory.httpClientBuildersByUri.values().stream() + .findFirst().get(); + HttpClientBuilder actualHttpClientBuilder = getActualHttpClientBuilder(actualConnection); + assertThat(actualHttpClientBuilder).isSameAs(expectedHttpClientBuilder); + } + + @Test + public void applicationNameAlsoOccursInBaseURLMultiplePlaceholders() throws Exception { + MultipleJGitEnvironmentProperties properties = new MultipleJGitEnvironmentProperties(); + properties.setUri("http://server.com/{placeholder}-foo/{placeholder}-test.git"); + this.connectionFactory.addConfiguration(properties); + + HttpConnection actualConnection = this.connectionFactory + .create(new URL("http://server.com/hello-foo/server-test.git" + "/some/path.properties")); + + HttpClientBuilder expectedHttpClientBuilder = this.connectionFactory.httpClientBuildersByUri.values().stream() + .findFirst().get(); + HttpClientBuilder actualHttpClientBuilder = getActualHttpClientBuilder(actualConnection); + assertThat(actualHttpClientBuilder).isSameAs(expectedHttpClientBuilder); + } + + @Test + public void applicationNameAlsoOccursLaterInPath() throws Exception { + MultipleJGitEnvironmentProperties properties = new MultipleJGitEnvironmentProperties(); + properties.setUri("http://localhost/{placeholder}-testval.git"); + this.connectionFactory.addConfiguration(properties); + + HttpConnection actualConnection = this.connectionFactory + .create(new URL("http://localhost/val-testval.git" + "/some/path.properties")); + + HttpClientBuilder expectedHttpClientBuilder = this.connectionFactory.httpClientBuildersByUri.values().stream() + .findFirst().get(); + HttpClientBuilder actualHttpClientBuilder = getActualHttpClientBuilder(actualConnection); + assertThat(actualHttpClientBuilder).isSameAs(expectedHttpClientBuilder); + } + private HttpClient getActualHttpClient(HttpConnection actualConnection) { Field clientField = ReflectionUtils.findField(actualConnection.getClass(), "client"); ReflectionUtils.makeAccessible(clientField);