You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by am...@apache.org on 2014/01/31 17:29:06 UTC
svn commit: r1563166 - in /sling/trunk/bundles/jcr/resource/src:
main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java
Author: amitgupt
Date: Fri Jan 31 16:29:06 2014
New Revision: 1563166
URL: http://svn.apache.org/r1563166
Log:
SLING-3361 - JcrResourceUtil.createPath does not work in certain ACL setups
Fixed in createPath method. Now it looks at the youngest existing child in tree..
Added:
sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java
Modified:
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
Modified: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java?rev=1563166&r1=1563165&r2=1563166&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java (original)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java Fri Jan 31 16:29:06 2014
@@ -280,17 +280,30 @@ public class JcrResourceUtil {
String nodeType,
Session session,
boolean autoSave)
- throws RepositoryException {
+ throws RepositoryException {
if (path == null || path.length() == 0 || "/".equals(path)) {
return session.getRootNode();
} else if (!session.itemExists(path)) {
- return createPath(session.getRootNode(),
- path.substring(1),
+ String existingPath = findExistingPath(path, session);
+
+
+ String relativePath = null;
+ Node parentNode = null;
+ if (existingPath != null) {
+ parentNode = session.getNode(existingPath);
+ relativePath = path.substring(existingPath.length() + 1);
+ } else {
+ relativePath = path.substring(1);
+ parentNode = session.getRootNode();
+ }
+
+ return createPath(parentNode,
+ relativePath,
intermediateNodeType,
nodeType,
autoSave);
} else {
- return (Node) session.getItem(path);
+ return session.getNode(path);
}
}
@@ -317,6 +330,15 @@ public class JcrResourceUtil {
if (relativePath == null || relativePath.length() == 0 || "/".equals(relativePath)) {
return parentNode;
} else if (!parentNode.hasNode(relativePath)) {
+ Session session = parentNode.getSession();
+ String path = parentNode.getPath() + "/" + relativePath;
+ String existingPath = findExistingPath(path, session);
+
+ if (existingPath != null) {
+ parentNode = session.getNode(existingPath);
+ relativePath = path.substring(existingPath.length() + 1);
+ }
+
Node node = parentNode;
int pos = relativePath.lastIndexOf('/');
if ( pos != -1 ) {
@@ -353,4 +375,24 @@ public class JcrResourceUtil {
return parentNode.getNode(relativePath);
}
}
+
+ private static String findExistingPath(String path, Session session)
+ throws RepositoryException {
+ //find the parent that exists
+ // we can start from the youngest child in tree
+ int currentIndex = path.lastIndexOf('/');
+ String temp = path;
+ String existingPath = null;
+ while (currentIndex > 0) {
+ temp = temp.substring(0, currentIndex);
+ //break when first existing parent is found
+ if (session.itemExists(temp)) {
+ existingPath = temp;
+ break;
+ }
+ currentIndex = temp.lastIndexOf("/");
+ }
+
+ return existingPath;
+ }
}
Added: sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java?rev=1563166&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java (added)
+++ sling/trunk/bundles/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceUtilTest.java Fri Jan 31 16:29:06 2014
@@ -0,0 +1,95 @@
+package org.apache.sling.jcr.resource.internal;
+
+import javax.jcr.Node;
+import org.apache.sling.commons.testing.jcr.RepositoryTestBase;
+import org.apache.sling.jcr.resource.JcrResourceUtil;
+
+/**
+ * Test of JcrResourceUtil.
+ */
+public class JcrResourceUtilTest extends RepositoryTestBase {
+ private String rootPath;
+
+ private Node rootNode;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ rootPath = "/test" + System.currentTimeMillis();
+ rootNode = getSession().getRootNode().addNode(rootPath.substring(1),
+ "nt:unstructured");
+ session.save();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ if (rootNode != null) {
+ rootNode.remove();
+ session.save();
+ }
+
+ super.tearDown();
+ }
+
+ public void testCreatePathRootExists() throws Exception {
+ String pathToCreate = rootPath + "/a/b/c";
+ Node created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+
+ //test appending the path
+ pathToCreate = pathToCreate + "/d";
+ created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+ assertTrue(session.itemExists(pathToCreate));
+ }
+
+ public void testCreateRoot() throws Exception {
+ String pathToCreate = "/";
+ Node created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+
+ assertNotNull(created);
+
+ assertEquals(created.getPath(), pathToCreate);
+ }
+
+
+ public void testCreatePathThatExists() throws Exception {
+ String pathToCreate = rootPath + "/a/b/c";
+ Node created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+
+ created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+ assertTrue(session.itemExists(pathToCreate));
+ }
+
+ public void testCreatePathBoundaryCase() throws Exception {
+ String pathToCreate = "/a";
+ Node created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+ assertTrue(session.itemExists(pathToCreate));
+ }
+
+ public void testCreatePathNodeVariant() throws Exception {
+ String pathToCreate = rootPath + "/a/b/c";
+ Node created = JcrResourceUtil.createPath(pathToCreate, "nt:unstructured", "nt:unstructured", session, true);
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+
+ //test appending the path
+ created = JcrResourceUtil.createPath(created, "d/e", "nt:unstructured", "nt:unstructured", true);
+
+ pathToCreate = pathToCreate + "/d/e";
+ assertNotNull(created);
+ assertEquals(created.getPath(), pathToCreate);
+ assertTrue(session.itemExists(pathToCreate));
+ }
+}