This is an automated email from the ASF dual-hosted git repository.

ilgrosso pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/syncope.git

commit 6c851cfffd810067f65f00b6a41638de86a84362
Author: Francesco Chicchiriccò <[email protected]>
AuthorDate: Tue Dec 31 14:59:33 2024 +0100

    Reworking URIUtils for improve robustness
---
 .../syncope/core/persistence/api/utils/URIUtils.java    | 17 ++++++++++-------
 .../core/persistence/api/utils/URIUtilsTest.java        |  4 ++++
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git 
a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/utils/URIUtils.java
 
b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/utils/URIUtils.java
index 1618beed40..0fd8e4dbca 100644
--- 
a/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/utils/URIUtils.java
+++ 
b/core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/utils/URIUtils.java
@@ -18,11 +18,11 @@
  */
 package org.apache.syncope.core.persistence.api.utils;
 
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.nio.file.Path;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.SystemUtils;
 
 public final class URIUtils {
 
@@ -33,15 +33,13 @@ public final class URIUtils {
     /**
      * Build a valid URI out of the given location.
      * Only "file", "connid" and "connids" schemes are allowed.
-     * For "file", invalid characters are handled via intermediate 
transformation into URL.
      *
      * @param location the candidate location for URI
      * @return valid URI for the given location
-     * @throws MalformedURLException if the intermediate URL is not valid
      * @throws URISyntaxException if the given location does not correspond to 
a valid URI
      */
-    public static URI buildForConnId(final String location) throws 
MalformedURLException, URISyntaxException {
-        final String candidate = location.trim();
+    public static URI buildForConnId(final String location) throws 
URISyntaxException {
+        String candidate = location.trim();
 
         if (!candidate.startsWith("file:")
                 && !candidate.startsWith("connid:") && 
!candidate.startsWith("connids:")) {
@@ -51,7 +49,12 @@ public final class URIUtils {
 
         URI uri;
         if (candidate.startsWith("file:")) {
-            uri = Path.of(new 
URL(candidate).getFile()).toFile().getAbsoluteFile().toURI();
+            candidate = StringUtils.substringAfter(candidate, "file:");
+            if (SystemUtils.IS_OS_WINDOWS) {
+                candidate = StringUtils.stripStart(candidate, "/");
+            }
+
+            uri = Path.of(candidate).toFile().getAbsoluteFile().toURI();
         } else {
             uri = new URI(candidate);
         }
diff --git 
a/core/persistence-api/src/test/java/org/apache/syncope/core/persistence/api/utils/URIUtilsTest.java
 
b/core/persistence-api/src/test/java/org/apache/syncope/core/persistence/api/utils/URIUtilsTest.java
index 8b2e665ee2..15b1e89d9d 100644
--- 
a/core/persistence-api/src/test/java/org/apache/syncope/core/persistence/api/utils/URIUtilsTest.java
+++ 
b/core/persistence-api/src/test/java/org/apache/syncope/core/persistence/api/utils/URIUtilsTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.syncope.core.persistence.api.utils;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -40,5 +41,8 @@ public class URIUtilsTest extends AbstractTest {
         location.set("connid:test/location");
         URI expectedURI = new URI(location.get().trim());
         assertEquals(expectedURI, URIUtils.buildForConnId(location.get()));
+
+        assertDoesNotThrow(() -> 
URIUtils.buildForConnId("file:Z:\\syncope\\fit\\core-reference\\target/bundles/"));
+        assertDoesNotThrow(() -> 
URIUtils.buildForConnId("file:/Z:\\syncope\\fit\\core-reference\\target/bundles/"));
     }
 }

Reply via email to