You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/03/23 17:03:49 UTC

[camel] branch master updated: CAMEL-13047: FileUtil.compachPath should preserve scheme. For example validator component with its resolver may not able to resolve nested xsd schemas.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 7da8bb4  CAMEL-13047: FileUtil.compachPath should preserve scheme. For example validator component with its resolver may not able to resolve nested xsd schemas.
7da8bb4 is described below

commit 7da8bb4e66d00eccccc5424f2685d06841c6c742
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Mar 23 18:01:39 2021 +0100

    CAMEL-13047: FileUtil.compachPath should preserve scheme. For example validator component with its resolver may not able to resolve nested xsd schemas.
---
 .../java/org/apache/camel/util/FileUtilTest.java   | 36 ++++++++++++++++++++++
 .../main/java/org/apache/camel/util/FileUtil.java  | 26 ++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java b/core/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
index ce878f1..4b77330 100644
--- a/core/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
@@ -291,6 +291,42 @@ public class FileUtilTest extends TestSupport {
         assertEquals(in, out);
     }
 
+    @Test
+    public void testCompactFilePath() {
+        // should preserve the file: scheme prefix
+        if (FileUtil.isWindows()) {
+            assertEquals("file:..\\foo", FileUtil.compactPath("file:..\\foo"));
+            assertEquals("file:..\\..\\foo", FileUtil.compactPath("file:..\\..\\foo"));
+            assertEquals("file:..\\..\\foo\\bar", FileUtil.compactPath("file:..\\..\\foo\\bar"));
+            assertEquals("file:..\\..\\foo", FileUtil.compactPath("file:..\\..\\foo\\bar\\.."));
+            assertEquals("file:foo", FileUtil.compactPath("file:foo"));
+            assertEquals("file:bar", FileUtil.compactPath("file:foo\\..\\bar"));
+            assertEquals("file:bar\\baz", FileUtil.compactPath("file:foo\\..\\bar\\baz"));
+            assertEquals("file:foo\\baz", FileUtil.compactPath("file:foo\\bar\\..\\baz"));
+            assertEquals("file:baz", FileUtil.compactPath("file:foo\\bar\\..\\..\\baz"));
+            assertEquals("file:..\\baz", FileUtil.compactPath("file:foo\\bar\\..\\..\\..\\baz"));
+            assertEquals("file:..\\foo\\bar", FileUtil.compactPath("file:..\\foo\\bar"));
+            assertEquals("file:foo\\bar\\baz", FileUtil.compactPath("file:foo\\bar\\.\\baz"));
+            assertEquals("file:foo\\bar\\baz", FileUtil.compactPath("file:foo\\bar\\\\baz"));
+            assertEquals("file:\\foo\\bar\\baz", FileUtil.compactPath("file:\\foo\\bar\\baz"));
+        } else {
+            assertEquals("file:../foo", FileUtil.compactPath("file:../foo"));
+            assertEquals("file:../../foo", FileUtil.compactPath("file:../../foo"));
+            assertEquals("file:../../foo/bar", FileUtil.compactPath("file:../../foo/bar"));
+            assertEquals("file:../../foo", FileUtil.compactPath("file:../../foo/bar/.."));
+            assertEquals("file:foo", FileUtil.compactPath("file:foo"));
+            assertEquals("file:bar", FileUtil.compactPath("file:foo/../bar"));
+            assertEquals("file:bar/baz", FileUtil.compactPath("file:foo/../bar/baz"));
+            assertEquals("file:foo/baz", FileUtil.compactPath("file:foo/bar/../baz"));
+            assertEquals("file:baz", FileUtil.compactPath("file:foo/bar/../../baz"));
+            assertEquals("file:../baz", FileUtil.compactPath("file:foo/bar/../../../baz"));
+            assertEquals("file:../foo/bar", FileUtil.compactPath("file:../foo/bar"));
+            assertEquals("file:foo/bar/baz", FileUtil.compactPath("file:foo/bar/./baz"));
+            assertEquals("file:foo/bar/baz", FileUtil.compactPath("file:foo/bar//baz"));
+            assertEquals("file:/foo/bar/baz", FileUtil.compactPath("file:/foo/bar/baz"));
+        }
+    }
+
     @BeforeEach
     void createTestDir() throws IOException {
         Files.createDirectories(testDirectory());
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/FileUtil.java b/core/camel-util/src/main/java/org/apache/camel/util/FileUtil.java
index e4c23cb..b87d48d 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/FileUtil.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/FileUtil.java
@@ -301,6 +301,14 @@ public final class FileUtil {
         // need to normalize path before compacting
         path = normalizePath(path);
 
+        // preserve scheme
+        String scheme = null;
+        if (hasScheme(path)) {
+            int pos = path.indexOf(':');
+            scheme = path.substring(0, pos);
+            path = path.substring(pos + 1);
+        }
+
         // preserve ending slash if given in input path
         boolean endsWithSlash = path.endsWith("/") || path.endsWith("\\");
 
@@ -332,6 +340,10 @@ public final class FileUtil {
 
         // build path based on stack
         StringBuilder sb = new StringBuilder();
+        if (scheme != null) {
+            sb.append(scheme);
+            sb.append(":");
+        }
 
         for (int i = 0; i < cntSlashsAtStart; i++) {
             sb.append(separator);
@@ -554,4 +566,18 @@ public final class FileUtil {
         }
     }
 
+    /**
+     * Determines whether the URI has a scheme (e.g. file:, classpath: or http:)
+     *
+     * @param  uri the URI
+     * @return     <tt>true</tt> if the URI starts with a scheme
+     */
+    private static boolean hasScheme(String uri) {
+        if (uri == null) {
+            return false;
+        }
+
+        return uri.startsWith("file:") || uri.startsWith("classpath:") || uri.startsWith("http:");
+    }
+
 }