You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:27 UTC

[sling-org-apache-sling-resourcebuilder] 18/36: SLING-5356 - allows paths in file(...) calls

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.resourcebuilder-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourcebuilder.git

commit 55ea9241e55191894ad4846625e30e2a6c1fc2c1
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Dec 14 13:25:39 2015 +0000

    SLING-5356 - allows paths in file(...) calls
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/resourcebuilder@1719906 13f79535-47bb-0310-9956-ffa450edef68
---
 .../resourcebuilder/impl/ResourceBuilderImpl.java  | 50 ++++++++++++++--------
 .../impl/ResourceBuilderImplTest.java              | 29 ++++++++-----
 2 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
index 3c89010..48b9be9 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
@@ -84,20 +84,32 @@ public class ResourceBuilderImpl implements ResourceBuilder {
         hierarchyMode();
         return this;
     }
-
-    @Override
-    public ResourceBuilder resource(String relativePath, Object... properties) {
-        Resource r = null;
+    
+    private void checkRelativePath(String relativePath) {
         if(relativePath.startsWith("/")) {
             throw new IllegalArgumentException("Path is not relative:" + relativePath);
         }
         if(relativePath.contains("..")) {
             throw new IllegalArgumentException("Path contains invalid pattern '..': " + relativePath);
         }
-        
-        final String fullPath = currentParent.getPath() + "/" + relativePath;
-        final String parentPath = ResourceUtil.getParent(fullPath);
+    }
+
+    private String parentPath(String relativePath) {
+        final String parentPath = currentParent.getPath();
+        final String fullPath = 
+            parentPath.endsWith("/")  ? 
+            parentPath + relativePath : 
+            parentPath + "/" + relativePath;
+        return ResourceUtil.getParent(fullPath);
+    }
+    
+    @Override
+    public ResourceBuilder resource(String relativePath, Object... properties) {
+        Resource r = null;
+        checkRelativePath(relativePath);
+        final String parentPath = parentPath(relativePath);
         final Resource myParent = ensureResourceExists(parentPath);
+        final String fullPath = currentParent.getPath() + "/" + relativePath;
         
         try {
             r = currentParent.getResourceResolver().getResource(fullPath);
@@ -173,18 +185,18 @@ public class ResourceBuilderImpl implements ResourceBuilder {
     }
     
     @Override
-    public ResourceBuilder file(String filename, InputStream data, String mimeType, long lastModified) {
-        Resource file = null;
-        final ResourceResolver resolver = currentParent.getResourceResolver();
-        final String name = ResourceUtil.getName(filename);
-        
-        if(!filename.equals(name)) {
-            throw new IllegalArgumentException("Filename must not be a path:" + filename + " -> " + name);
-        }
+    public ResourceBuilder file(String relativePath, InputStream data, String mimeType, long lastModified) {
+        checkRelativePath(relativePath);
+        final String name = ResourceUtil.getName(relativePath);
         if(data == null) {
-            throw new IllegalArgumentException("Data is null for file " + filename);
+            throw new IllegalArgumentException("Data is null for file " + name);
         }
         
+        Resource file = null;
+        final ResourceResolver resolver = currentParent.getResourceResolver();
+        final String parentPath = parentPath(relativePath);
+        
+        final Resource parent = ensureResourceExists(parentPath);
         try {
             final String fullPath = currentParent.getPath() + "/" + name;
             if(resolver.getResource(fullPath) != null) {
@@ -192,11 +204,11 @@ public class ResourceBuilderImpl implements ResourceBuilder {
             }
             final Map<String, Object> fileProps = new HashMap<String, Object>();
             fileProps.put(JCR_PRIMARYTYPE, NT_FILE);
-            file = resolver.create(currentParent, name, fileProps);
+            file = resolver.create(parent, name, fileProps);
             
             final Map<String, Object> contentProps = new HashMap<String, Object>();
             contentProps.put(JCR_PRIMARYTYPE, NT_RESOURCE);
-            contentProps.put(JCR_MIMETYPE, getMimeType(filename, mimeType));
+            contentProps.put(JCR_MIMETYPE, getMimeType(name, mimeType));
             contentProps.put(JCR_LASTMODIFIED, getLastModified(lastModified));
             contentProps.put(JCR_DATA, data);
             resolver.create(file, JCR_CONTENT, contentProps); 
@@ -205,7 +217,7 @@ public class ResourceBuilderImpl implements ResourceBuilder {
         }
         
         if(file == null) {
-            throw new RuntimeException("Unable to get or created file resource " + filename + " under " + currentParent.getPath());
+            throw new RuntimeException("Unable to get or created file resource " + relativePath + " under " + currentParent.getPath());
         }
         if(hierarchyMode) {
             currentParent = file;
diff --git a/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java b/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
index 1beedbe..608a813 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
@@ -34,6 +34,7 @@ import org.apache.sling.resourcebuilder.test.ResourceAssertions;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.apache.sling.testing.mock.sling.services.MockMimeTypeService;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -86,6 +87,15 @@ public class ResourceBuilderImplTest {
         A = new ResourceAssertions(testRootPath, resourceResolver);
     }
     
+    @After
+    public void cleanup() throws PersistenceException {
+        final Resource r = resourceResolver.getResource(testRootPath);
+        if(r != null) {
+            resourceResolver.delete(r);
+            resourceResolver.commit();
+        }
+    }
+    
     @Test
     public void basicResource() throws Exception {
         getBuilder(testRootPath)
@@ -199,6 +209,11 @@ public class ResourceBuilderImplTest {
         getBuilder(testRootPath).resource("../foo");
     }
     
+    @Test(expected=IllegalArgumentException.class)
+    public void aboveParentFailsFile() throws Exception {
+        getBuilder(testRootPath).file("../foo.js", null);
+    }
+    
     @Test
     public void simpleTree() throws Exception {
         getBuilder(testRootPath)
@@ -229,8 +244,7 @@ public class ResourceBuilderImplTest {
             .file("models.js", getClass().getResourceAsStream("/files/models.js"), "MT1", 42)
             .file("text.html", getClass().getResourceAsStream("/files/text.html"), "MT2", 43)
             .atParent()
-            .resource("apps")
-            .file("myapp.json", getClass().getResourceAsStream("/files/myapp.json"), "MT3", 44)
+            .file("apps/myapp.json", getClass().getResourceAsStream("/files/myapp.json"), "MT3", 44)
             .atParent()
             .resource("apps/content/myapp/resource")
             .atParent()
@@ -276,10 +290,10 @@ public class ResourceBuilderImplTest {
     @Test
     public void autoEverything() throws Exception {
         getBuilder(testRootPath)
-            .file("models.js", getClass().getResourceAsStream("/files/models.js"))
+            .file("a/b/c/models.js", getClass().getResourceAsStream("/files/models.js"))
             .commit()
             ;
-        A.assertFile("models.js", 
+        A.assertFile("a/b/c/models.js", 
                 "application/javascript", "function someJavascriptFunction()", lastModified);
     }
     
@@ -293,13 +307,6 @@ public class ResourceBuilderImplTest {
     }
     
     @Test(expected=IllegalArgumentException.class)
-    public void fileWithPathFails() throws Exception {
-        getBuilder(testRootPath)
-            .file("somewhere/files/models.js", getClass().getResourceAsStream("/files/models.js"), null, 42)
-            ;
-    }
-    
-    @Test(expected=IllegalArgumentException.class)
     public void nullDataFails() throws Exception {
         getBuilder(testRootPath)
             .file("models.js", null, null, 42)

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.