diff --git a/ors-api/src/main/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListener.java b/ors-api/src/main/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListener.java index ad9edad991..2c37a76ff4 100644 --- a/ors-api/src/main/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListener.java +++ b/ors-api/src/main/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListener.java @@ -37,6 +37,8 @@ import org.heigit.ors.util.StringUtility; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import static org.heigit.ors.api.ORSEnvironmentPostProcessor.ORS_CONFIG_LOCATION_ENV; @@ -102,22 +104,27 @@ record SourceFileElements(String repoBaseUrlString, String repoName, String loca } SourceFileElements extractSourceFileElements(String sourceFilePropertyValue) { - String repoBaseUrlString = null; - String repoName = null; - String localOsmFilePath = ""; + String accessScheme = null, repoBaseUrlString = null, repoName = null; + sourceFilePropertyValue = sourceFilePropertyValue.trim(); try { - new URL(sourceFilePropertyValue); - LOGGER.debug("Configuration property 'source_file' contains a URL, using value as URL for a graphs repository"); - sourceFilePropertyValue = sourceFilePropertyValue.trim().replaceAll("/$", ""); - String[] urlElements = sourceFilePropertyValue.split("/"); + URI uri = new URI(sourceFilePropertyValue); + accessScheme = uri.getScheme(); + repoBaseUrlString = uri.getHost(); + if (accessScheme == null || repoBaseUrlString == null) { + throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a valid schema or host"); + } + repoBaseUrlString = accessScheme + "://" + repoBaseUrlString; - repoName = urlElements[urlElements.length - 1]; - repoBaseUrlString = sourceFilePropertyValue.replaceAll("/%s$".formatted(repoName), ""); - } catch (MalformedURLException e) { + String[] pathElements = uri.getPath().split("/"); + if (pathElements.length == 0) { + throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a path"); + } + repoName = pathElements[pathElements.length - 1]; + } catch (URISyntaxException e) { LOGGER.debug("Configuration property 'source_file' does not contain a URL, using value as local osm file path"); - localOsmFilePath = sourceFilePropertyValue; + return new SourceFileElements(repoBaseUrlString, repoName, sourceFilePropertyValue); } - return new SourceFileElements(repoBaseUrlString, repoName, localOsmFilePath); + return new SourceFileElements(repoBaseUrlString, repoName, ""); } @Override diff --git a/ors-api/src/test/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListenerTest.java b/ors-api/src/test/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListenerTest.java index 280647dd7a..4a13517f73 100644 --- a/ors-api/src/test/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListenerTest.java +++ b/ors-api/src/test/java/org/heigit/ors/api/servlet/listeners/ORSInitContextListenerTest.java @@ -16,17 +16,16 @@ void setUp(){ } @ParameterizedTest @CsvSource(value = { - "https://example.com/test-repo/planet, https://example.com, test-repo, planet", - "https://example.com/test-repo/planet/, https://example.com, test-repo, planet", - "' https://example.com/test-repo/planet ', https://example.com, test-repo, planet", - "' https://example.com/test-repo/planet/ ', https://example.com, test-repo, planet", - "' http://example.com/test-repo/planet/ ', http://example.com, test-repo, planet" + "https://example.com/foo1/foo2/foo3/test-repo, https://example.com, test-repo", + "https://example.com/test-repo/, https://example.com, test-repo", + "https://example.com/test-repo, https://example.com, test-repo", + "' https://example.com/test-repo ', https://example.com, test-repo", + "https://example.com/test-repos/, https://example.com, test-repos", + "http://example.com/test-repo/, http://example.com, test-repo" }) - void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName, String expectedCoverage){ + void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName){ ORSInitContextListener.SourceFileElements elements = orsInitContextListener.extractSourceFileElements(url); assertEquals(expectedBaseUrl, elements.repoBaseUrlString()); assertEquals(expectedRepoName, elements.repoName()); - // TODO fix this function - // assertEquals(expectedCoverage, elements.repoCoverage()); } } \ No newline at end of file