camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1388762 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/FileUtil.java main/java/org/apache/camel/util/ObjectHelper.java test/java/org/apache/camel/util/FileUtilTest.java
Date Sat, 22 Sep 2012 09:37:07 GMT
Author: davsclaus
Date: Sat Sep 22 09:37:07 2012
New Revision: 1388762

URL: http://svn.apache.org/viewvc?rev=1388762&view=rev
Log:
CAMEL-5632: Fixed issue with loading resource containg relative paths. Fixed issue with 2+
parent paths using .. notation.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=1388762&r1=1388761&r2=1388762&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java Sat Sep 22 09:37:07
2012
@@ -202,7 +202,8 @@ public final class FileUtil {
     }
 
     /**
-     * Compacts a path by stacking it and reducing <tt>..</tt>
+     * Compacts a path by stacking it and reducing <tt>..</tt>,
+     * and uses OS specific file separators (eg {@link java.io.File#separator}).
      */
     public static String compactPath(String path) {
         if (path == null) {
@@ -219,6 +220,9 @@ public final class FileUtil {
             return path;
         }
 
+        // preserve ending slash if given in input path
+        boolean endsWithSlash = path.endsWith("/") || path.endsWith("\\");
+
         Stack<String> stack = new Stack<String>();
         
         String separatorRegex = File.separator;
@@ -227,8 +231,8 @@ public final class FileUtil {
         }
         String[] parts = path.split(separatorRegex);
         for (String part : parts) {
-            if (part.equals("..") && !stack.isEmpty()) {
-                // only pop if there is a previous path
+            if (part.equals("..") && !stack.isEmpty() && !"..".equals(stack.peek()))
{
+                // only pop if there is a previous path, which is not a ".." path either
                 stack.pop();
             } else {
                 stack.push(part);
@@ -244,6 +248,10 @@ public final class FileUtil {
             }
         }
 
+        if (endsWithSlash) {
+            sb.append(File.separator);
+        }
+
         return sb.toString();
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1388762&r1=1388761&r2=1388762&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Sat Sep 22
09:37:07 2012
@@ -905,17 +905,7 @@ public final class ObjectHelper {
      * @return the modified or unmodified string if there were no changes
      */
     private static String resolveUriPath(String name) {
-        String answer = name;
-        if (answer.indexOf("//") > -1) {
-            answer = answer.replaceAll("//", "/");
-        }
-        if (answer.indexOf("../") > -1) {
-            answer = answer.replaceAll("[A-Za-z0-9]*/\\.\\./", "");
-        }
-        if (answer.indexOf("./") > -1) {
-            answer = answer.replaceAll("\\./", "");
-        }
-        return answer;
+        return FileUtil.compactPath(name);
     }
 
     /**

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java?rev=1388762&r1=1388761&r2=1388762&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java Sat Sep 22
09:37:07 2012
@@ -133,6 +133,10 @@ public class FileUtilTest extends TestCa
     public void testCompactPath() {
         assertEquals(null, FileUtil.compactPath(null));
         if (FileUtil.isWindows()) {
+            assertEquals("..\\foo", FileUtil.compactPath("..\\foo"));
+            assertEquals("..\\..\\foo", FileUtil.compactPath("..\\..\\foo"));
+            assertEquals("..\\..\\foo\\bar", FileUtil.compactPath("..\\..\\foo\\bar"));
+            assertEquals("..\\..\\foo", FileUtil.compactPath("..\\..\\foo\\bar\\.."));
             assertEquals("foo", FileUtil.compactPath("foo"));
             assertEquals("bar", FileUtil.compactPath("foo\\..\\bar"));
             assertEquals("bar\\baz", FileUtil.compactPath("foo\\..\\bar\\baz"));
@@ -141,6 +145,10 @@ public class FileUtilTest extends TestCa
             assertEquals("..\\baz", FileUtil.compactPath("foo\\bar\\..\\..\\..\\baz"));
             assertEquals("..\\foo\\bar", FileUtil.compactPath("..\\foo\\bar"));
         } else {
+            assertEquals("../foo", FileUtil.compactPath("../foo"));
+            assertEquals("../../foo", FileUtil.compactPath("../../foo"));
+            assertEquals("../../foo/bar", FileUtil.compactPath("../../foo/bar"));
+            assertEquals("../../foo", FileUtil.compactPath("../../foo/bar/.."));
             assertEquals("foo", FileUtil.compactPath("foo"));
             assertEquals("bar", FileUtil.compactPath("foo/../bar"));
             assertEquals("bar/baz", FileUtil.compactPath("foo/../bar/baz"));



Mime
View raw message