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/09/07 12:59:07 UTC

svn commit: r1759607 - in /jackrabbit/trunk: jackrabbit-core/pom.xml jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Author: reschke
Date: Wed Sep  7 12:59:07 2016
New Revision: 1759607

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

Modified:
    jackrabbit/trunk/jackrabbit-core/pom.xml
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Modified: jackrabbit/trunk/jackrabbit-core/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/pom.xml?rev=1759607&r1=1759606&r2=1759607&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/pom.xml (original)
+++ jackrabbit/trunk/jackrabbit-core/pom.xml Wed Sep  7 12:59:07 2016
@@ -343,6 +343,12 @@ org.apache.jackrabbit.test.api.query.qom
       <version>1.3.149</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <version>1.10.19</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <profiles>

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java?rev=1759607&r1=1759606&r2=1759607&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/integration/UtilsGetPathTest.java Wed Sep  7 12:59:07 2016
@@ -16,15 +16,20 @@
  */
 package org.apache.jackrabbit.core.integration;
 
+import javax.jcr.AccessDeniedException;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 import org.apache.jackrabbit.commons.JcrUtils;
 import org.apache.jackrabbit.test.AbstractJCRTest;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * @see <a href="https://issues.apache.org/jira/browse/JCR-3992">JCR-3992</a>
+ *      and
+ *      <a href="https://issues.apache.org/jira/browse/JCR-4015">JCR-4015</a>
  */
 public class UtilsGetPathTest extends AbstractJCRTest {
 
@@ -43,4 +48,21 @@ public class UtilsGetPathTest extends Ab
         assertEquals(path2, node2.getPath());
         assertTrue(superuser.nodeExists(path2));
     }
+
+    @Test
+    public void testGetOrCreateByPathNoRoot() throws RepositoryException {
+        Node inter = JcrUtils.getOrCreateByPath("/foo", "nt:unstructured", superuser);
+        assertEquals("/foo", inter.getPath());
+        superuser.save();
+
+        // test what happens if getRootNode() throws
+        final Session mockedSession = Mockito.spy(superuser);
+        Mockito.when(mockedSession.getRootNode()).thenThrow(new AccessDeniedException("access denied"));
+        Mockito.when(mockedSession.getNode("/")).thenThrow(new AccessDeniedException("access denied"));
+        Mockito.when(mockedSession.getItem("/")).thenThrow(new AccessDeniedException("access denied"));
+
+        Node result = JcrUtils.getOrCreateByPath("/foo/bar", false, null, null, mockedSession, false);
+        superuser.save();
+        assertEquals("/foo/bar", result.getPath());
+    }
 }

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=1759607&r1=1759606&r2=1759607&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 Wed Sep  7 12:59:07 2016
@@ -1448,10 +1448,25 @@ public class JcrUtils {
         if (absolutePath == null || absolutePath.length() == 0 || "/".equals(absolutePath)) {
             // path denotes root node
             return session.getRootNode();
+        } else if (!absolutePath.startsWith("/")) {
+            throw new IllegalArgumentException("not an absolute path: " + absolutePath);
+        } else {
+            // find deepest existing parent node
+            String path = absolutePath;
+            int currentIndex = path.lastIndexOf('/');
+            String existingPath = null;
+            while (currentIndex > 0 && existingPath == null) {
+                path = path.substring(0, currentIndex);
+                if (session.nodeExists(path)) {
+                    existingPath = path;
+                } else {
+                    currentIndex = path.lastIndexOf('/');
+                }
+            }
+            // create path relative to the root node
+            return getOrCreateByPath(existingPath == null ? session.getRootNode() : session.getNode(existingPath),
+                    absolutePath.substring(currentIndex + 1), createUniqueLeaf, intermediateNodeType, nodeType, autoSave);
         }
-        // create path relative to the root node
-        return getOrCreateByPath(session.getRootNode(), absolutePath.substring(1),
-                createUniqueLeaf, intermediateNodeType, nodeType, autoSave);
     }
 
     /**