Merge pull request #1 from sdmcraft/SLING-6501
SLING-6501:Incorrect normalization of path of an asset with name begiā¦
diff --git a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
index 134e133..02d4739 100644
--- a/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
+++ b/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
@@ -82,7 +82,7 @@
lastSlash = bufIdx;
numDots = 0;
- } else if (c == '.' && numDots < 2) {
+ } else if (c == '.' && useDot(buf, bufIdx) && numDots < 2) {
numDots++;
} else {
// find the next slash
@@ -118,6 +118,21 @@
return resolved;
}
+ // use this dot only if followed by /
+ // don't use if followed by neither . nor /
+ // keep checking till a non-dot is found
+ private static boolean useDot(char[] buf, int bufIdx) {
+ while(bufIdx < buf.length -1) {
+ if(buf[bufIdx] == '/') {
+ return true;
+ }
+ else if(buf[bufIdx] != '.') {
+ return false;
+ }
+ bufIdx++;
+ }
+ return true;
+ }
/**
* Utility method returns the parent path of the given <code>path</code>,
* which is normalized by {@link #normalize(String)} before resolving the
diff --git a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
index 3688ac0..3e8ca08 100644
--- a/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
+++ b/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
@@ -114,6 +114,12 @@
assertEquals("/az/...", ResourceUtil.normalize("/az/..."));
assertEquals("/az/bz/...", ResourceUtil.normalize("/az/bz/..."));
+ assertEquals("/content/dam/jpg_folder/.jpg/jcr:content/metadata/dc:title",
+ ResourceUtil.normalize("/content/dam/./jpg_folder/.jpg/jcr:content/metadata/dc:title"));
+
+ assertEquals("/content/dam/jpg_folder/....jpg/jcr:content/metadata/dc:title",
+ ResourceUtil.normalize("/content/dam/./jpg_folder/....jpg/jcr:content/metadata/dc:title"));
+
try {
ResourceUtil.normalize(null);
fail("Resolving null expects NullPointerException");