This is an automated email from the ASF dual-hosted git repository.
enorman pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-bundleresource-impl.git
The following commit(s) were added to refs/heads/master by this push:
new 54c2d69 SLING-10140 Handle defining resources through JSON when the
resourceRoot is a mapped file
54c2d69 is described below
commit 54c2d6949c3c0e4714c51d30ef2841af38e52e35
Author: Eric Norman <[email protected]>
AuthorDate: Sat Feb 13 18:32:55 2021 -0800
SLING-10140 Handle defining resources through JSON when the resourceRoot
is a mapped file
---
.../sling/bundleresource/impl/BundleResource.java | 14 ++++++++++++-
.../bundleresource/impl/BundleResourceTest.java | 24 ++++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
b/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
index 8b0f70a..2bb0cde 100644
--- a/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
+++ b/src/main/java/org/apache/sling/bundleresource/impl/BundleResource.java
@@ -120,7 +120,19 @@ public class BundleResource extends AbstractResource {
}
}
if ( this.mappedPath.getJSONPropertiesExtension() != null ) {
- final String propsPath =
mappedPath.getEntryPath(resourcePath.concat(this.mappedPath.getJSONPropertiesExtension()));
+ String propsPath =
mappedPath.getEntryPath(resourcePath.concat(this.mappedPath.getJSONPropertiesExtension()));
+ if (propsPath == null &&
resourcePath.equals(mappedPath.getResourceRoot())) {
+ // SLING-10140 - Handle the special case when the resourceRoot
points to a file.
+ // In that case, the JSONProperties sibling entry may still
exist
+ // in the bundle but it would not be contained within the
mappedPath set.
+
+ // Start with mapped path for the original resource
+ String entryPath = mappedPath.getEntryPath(resourcePath);
+ if (entryPath != null) {
+ // and then add the extension for the candidate sibling
path
+ propsPath =
entryPath.concat(this.mappedPath.getJSONPropertiesExtension());
+ }
+ }
if ( propsPath != null ) {
try {
diff --git
a/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
b/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
index f97f81b..892c1d3 100644
--- a/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
+++ b/src/test/java/org/apache/sling/bundleresource/impl/BundleResourceTest.java
@@ -68,6 +68,13 @@ public class BundleResourceTest {
when(cache.getEntry(path)).thenReturn(url);
}
+ void addContent(BundleResourceCache cache, String path, String content)
throws IOException {
+ final URL url = new URL("resource:" + path);
+
+ ResourceURLStreamHandler.addContents(path, content);
+ when(cache.getEntry(path)).thenReturn(url);
+ }
+
@Test public void testFileResource() throws MalformedURLException {
final BundleResourceCache cache = getBundleResourceCache();
when(cache.getEntry("/libs/foo/test.json")).thenReturn(new
URL("file:/libs/foo/test.json"));
@@ -90,4 +97,21 @@ public class BundleResourceTest {
assertEquals(JcrConstants.NT_FILE,
vm.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class));
assertEquals("foo", vm.get("test", String.class));
}
+
+ /**
+ * SLING-10140 - Verify that when the resourceRoot is a mapped file, that
the sibling entry with the
+ * JSONPropertiesExtension is loaded
+ */
+ @Test public void testJSONResourceForMappedFile() throws IOException {
+ final BundleResourceCache cache = getBundleResourceCache();
+ addContent(cache, "/SLING_INF/libs/foo/test.txt", "Hello Text");
+ addContent(cache, "/SLING-INF/libs/foo/test.txt.json",
Collections.singletonMap("test", (Object)"foo"));
+ final BundleResource rsrc = new BundleResource(null, cache,
+ new PathMapping("/libs/foo/test.txt",
"/SLING-INF/libs/foo/test.txt", "json"), "/libs/foo/test.txt", null, false);
+ assertEquals(JcrConstants.NT_FILE, rsrc.getResourceType());
+ assertNull(rsrc.getResourceSuperType());
+ final ValueMap vm = rsrc.getValueMap();
+ assertEquals(JcrConstants.NT_FILE,
vm.get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class));
+ assertEquals("foo", vm.get("test", String.class));
+ }
}