diff --git a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/ResourceLocator.java b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/ResourceLocator.java index b6ffd2c6ca4..b9c7b257f72 100644 --- a/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/ResourceLocator.java +++ b/backend/manager/modules/restapi/jaxrs/src/main/java/org/ovirt/engine/api/restapi/resource/ResourceLocator.java @@ -2,6 +2,8 @@ import java.lang.reflect.Method; import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.ovirt.engine.api.rsdl.ServiceTreeCrawler; import org.ovirt.engine.api.rsdl.ServiceTreeNode; @@ -13,6 +15,8 @@ */ public class ResourceLocator { + private static final Pattern ULR_VERSION_PART_PATTERN = Pattern.compile("/api/(v\\d+/)?"); + private static ResourceLocator instance; private ResourceLocator() { @@ -58,12 +62,15 @@ public BaseBackendResource locateResource(String href) throws Exception { * http://localhost:8080/ovirt-engine/api/ * Remain with: * datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b + * + * Api definition with version also can be truncated (e.g. /api/v3/ or /api/v4/) */ - private String removePrefix(String href) { - int index = href.indexOf("/api/"); - if (index>0) { - href = href.substring(index+5); + static String removePrefix(String href) { + Matcher matcher = ULR_VERSION_PART_PATTERN.matcher(href); + if (matcher.find()) { + href = href.substring(matcher.end()); } + return href; } } diff --git a/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/ResourceLocatorTest.java b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/ResourceLocatorTest.java new file mode 100644 index 00000000000..1c41c9079a9 --- /dev/null +++ b/backend/manager/modules/restapi/jaxrs/src/test/java/org/ovirt/engine/api/restapi/resource/ResourceLocatorTest.java @@ -0,0 +1,42 @@ +package org.ovirt.engine.api.restapi.resource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class ResourceLocatorTest { + + @ParameterizedTest + @MethodSource("providerUrlsToCheckSuffixExtraction") + void testResourceLocatorGetPrefix(String source, String expectedResult) { + assertEquals(expectedResult, ResourceLocator.removePrefix(source)); + } + + private static Stream providerUrlsToCheckSuffixExtraction() { + var basePart = "http://localhost:8080/ovirt-engine"; + var suffix = "datacenters/1034e9ba-c1a4-442c-8bc9-f7c1c997652b"; + + return Stream.of( + Arguments.of( + basePart + "/api/v12/" + suffix, + suffix + ), + Arguments.of( + basePart + "/api/" + suffix, + suffix + ), + Arguments.of( + basePart + "/api/v4/" + suffix, + suffix + ), + Arguments.of( // Without pattern, method should return the same string. + basePart + "/" + suffix, + basePart + "/" + suffix + ) + ); + } +}