Better parsing of connection factory urls. Fixes #2254 (#2263)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
This commit is contained in:
Ryan Baxter
2023-05-04 16:04:49 -04:00
committed by GitHub
parent 6ec9c432cb
commit 436fdf9910
2 changed files with 73 additions and 5 deletions

View File

@@ -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<String> placeholders = getPlaceholders(key);
List<Placeholder> placeholders = getPlaceholders(key);
List<String> 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<String> getPlaceholders(String key) {
private List<Placeholder> getPlaceholders(String key) {
Pattern pattern = Pattern.compile(PLACEHOLDER_PATTERN_STRING);
Matcher matcher = pattern.matcher(key);
List<String> placeholders = new LinkedList<>();
List<Placeholder> 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;
}
}
}

View File

@@ -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);