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:31 UTC
[sling-org-apache-sling-resourcebuilder] 22/36: SLING-6035
ResourceBuilder: Support absolute paths This closes #165
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 6430286a518230dbde2ebe75570600017f44f130
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Sep 5 19:36:07 2016 +0000
SLING-6035 ResourceBuilder: Support absolute paths
This closes #165
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/resourcebuilder@1759348 13f79535-47bb-0310-9956-ffa450edef68
---
.gitignore | 1 +
.../sling/resourcebuilder/api/ResourceBuilder.java | 7 ++--
.../resourcebuilder/impl/ResourceBuilderImpl.java | 39 ++++++++++++++--------
.../impl/ResourceBuilderImplTest.java | 24 ++++++++++---
4 files changed, 49 insertions(+), 22 deletions(-)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0829d12
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+sling
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java b/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
index 1a1f965..07ae787 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/api/ResourceBuilder.java
@@ -45,12 +45,13 @@ public interface ResourceBuilder {
/** Create a Resource, which optionally becomes the current
* parent Resource.
- * @param relativePath The path of the Resource to create, relative to
- * this builder's current parent Resource.
+ * @param path The path of the Resource to create.
+ * If it's a relative path this builder's current resource is used as parent.
+ * Otherwise the resource is created ad the given absoulte path.
* @param properties optional name-value pairs
* @return this builder
*/
- ResourceBuilder resource(String relativePath, Object ... properties);
+ ResourceBuilder resource(String path, Object ... properties);
/** Create a file under the current parent resource
* @param filename The name of the created file
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 48b9be9..4905387 100644
--- a/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
+++ b/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java
@@ -85,12 +85,16 @@ public class ResourceBuilderImpl implements ResourceBuilder {
return this;
}
- private void checkRelativePath(String relativePath) {
- if(relativePath.startsWith("/")) {
- throw new IllegalArgumentException("Path is not relative:" + relativePath);
+ private boolean isAbsolutePath(String path) {
+ return path.startsWith("/") && !path.contains("..");
+ }
+
+ private void checkRelativePath(String path) {
+ if(path.startsWith("/")) {
+ throw new IllegalArgumentException("Path is not relative:" + path);
}
- if(relativePath.contains("..")) {
- throw new IllegalArgumentException("Path contains invalid pattern '..': " + relativePath);
+ if(path.contains("..")) {
+ throw new IllegalArgumentException("Path contains invalid pattern '..': " + path);
}
}
@@ -104,19 +108,28 @@ public class ResourceBuilderImpl implements ResourceBuilder {
}
@Override
- public ResourceBuilder resource(String relativePath, Object... properties) {
+ public ResourceBuilder resource(String path, Object... properties) {
Resource r = null;
- checkRelativePath(relativePath);
- final String parentPath = parentPath(relativePath);
+
+ final String parentPath;
+ final String fullPath;
+ if (isAbsolutePath(path)) {
+ parentPath = ResourceUtil.getParent(path);
+ fullPath = path;
+ }
+ else {
+ checkRelativePath(path);
+ parentPath = parentPath(path);
+ fullPath = currentParent.getPath() + "/" + path;
+ }
final Resource myParent = ensureResourceExists(parentPath);
- final String fullPath = currentParent.getPath() + "/" + relativePath;
try {
r = currentParent.getResourceResolver().getResource(fullPath);
final Map<String, Object> props = MapArgsConverter.toMap(properties);
if(r == null) {
r = currentParent.getResourceResolver().create(myParent,
- ResourceUtil.getName(relativePath), props);
+ ResourceUtil.getName(fullPath), props);
} else {
// Resource exists, set our properties
final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class);
@@ -129,13 +142,11 @@ public class ResourceBuilderImpl implements ResourceBuilder {
}
} catch(PersistenceException pex) {
throw new RuntimeException(
- "PersistenceException while creating Resource " + relativePath
- + " under " + currentParent.getPath(), pex);
+ "PersistenceException while creating Resource " + fullPath, pex);
}
if(r == null) {
- throw new RuntimeException("Failed to get or create resource " + relativePath
- + " under " + currentParent.getPath());
+ throw new RuntimeException("Failed to get or create resource " + fullPath);
} else if(hierarchyMode) {
currentParent = r;
}
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 608a813..6d3cf52 100644
--- a/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
+++ b/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java
@@ -200,11 +200,6 @@ public class ResourceBuilderImplTest {
}
@Test(expected=IllegalArgumentException.class)
- public void absolutePathFails() throws Exception {
- getBuilder(testRootPath).resource("/absolute");
- }
-
- @Test(expected=IllegalArgumentException.class)
public void aboveParentFails() throws Exception {
getBuilder(testRootPath).resource("../foo");
}
@@ -332,4 +327,23 @@ public class ResourceBuilderImplTest {
// Resource is created at root in this case
A.assertResource("/d/e/f");
}
+
+ @Test
+ public void absolutePath() throws Exception {
+ new ResourceBuilderService()
+ .forResolver(resourceResolver)
+ .resource("/a/b/c")
+ .resource("/a/b/f")
+ .resource("/g/h/i")
+ .resource("j/l/m")
+ .resource("/o/p/q");
+
+ // absolute paths are supported and can be mixed with relative paths
+ A.assertResource("/a/b/c");
+ A.assertResource("/a/b/f");
+ A.assertResource("/g/h/i");
+ A.assertResource("/g/h/i/j/l/m");
+ A.assertResource("/o/p/q");
+ }
+
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.