You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/12/14 14:25:39 UTC
svn commit: r1719906 - in /sling/trunk/bundles/commons/resourcebuilder/src:
main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
Author: bdelacretaz
Date: Mon Dec 14 13:25:39 2015
New Revision: 1719906
URL: http://svn.apache.org/viewvc?rev=1719906&view=rev
Log:
SLING-5356 - allows paths in file(...) calls
Modified:
sling/trunk/bundles/commons/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
sling/trunk/bundles/commons/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
Modified: sling/trunk/bundles/commons/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java?rev=1719906&r1=1719905&r2=1719906&view=diff
==============================================================================
--- sling/trunk/bundles/commons/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java (original)
+++ sling/trunk/bundles/commons/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java Mon Dec 14 13:25:39 2015
@@ -84,20 +84,32 @@ public class ResourceBuilderImpl impleme
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 impleme
}
@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 impleme
}
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 impleme
}
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;
Modified: sling/trunk/bundles/commons/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java?rev=1719906&r1=1719905&r2=1719906&view=diff
==============================================================================
--- sling/trunk/bundles/commons/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java (original)
+++ sling/trunk/bundles/commons/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java Mon Dec 14 13:25:39 2015
@@ -34,6 +34,7 @@ import org.apache.sling.resourcebuilder.
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);
}
@@ -292,13 +306,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)