You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2016/07/04 13:26:26 UTC

svn commit: r1751279 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Author: reschke
Date: Mon Jul  4 13:26:25 2016
New Revision: 1751279

URL: http://svn.apache.org/viewvc?rev=1751279&view=rev
Log:
JCR-3987: JcrUtils.getOrCreateByPath fails if session is not allowed to read root

(patch by Carsten Ziegeler)

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java?rev=1751279&r1=1751278&r2=1751279&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java Mon Jul  4 13:26:25 2016
@@ -1543,6 +1543,27 @@ public class JcrUtils {
             return baseNode.getNode(path);
         }
 
+        // find the parent that exists
+        // we can start from the deepest child in tree
+        String fullPath = baseNode.getPath().equals("/") ? "/" + path : baseNode.getPath() + "/" + path;
+        int currentIndex = fullPath.lastIndexOf('/');
+        String temp = fullPath;
+        String existingPath = null;
+        while (currentIndex > 0) {
+            temp = temp.substring(0, currentIndex);
+            // break when first existing parent is found
+            if (baseNode.getSession().itemExists(temp)) {
+                existingPath = temp;
+                break;
+            }
+            currentIndex = temp.lastIndexOf("/");
+        }
+
+        if (existingPath != null) {
+            baseNode = baseNode.getSession().getNode(existingPath);
+            path = path.substring(existingPath.length() + 1);
+        }
+
         Node node = baseNode;
         int pos = path.lastIndexOf('/');