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:37:54 UTC

[sling-org-apache-sling-fsresource] 06/09: SLING-6877 FSResource: Allow to adapt to Node from FileResource

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

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

commit 635d2361336375f59b3940d61d44ba1cea9f64df
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue May 23 23:26:49 2017 +0000

    SLING-6877 FSResource: Allow to adapt to Node from FileResource
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/branches/fsresource-1.x@1795979 13f79535-47bb-0310-9956-ffa450edef68
---
 .../fsprovider/internal/mapper/FileResource.java    | 21 +++++++++++++++------
 .../sling/fsprovider/internal/JsonContentTest.java  |  5 +++++
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java b/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
index 4ee4a20..83516c9 100644
--- a/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
+++ b/src/main/java/org/apache/sling/fsprovider/internal/mapper/FileResource.java
@@ -28,6 +28,8 @@ import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.jcr.Node;
+
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 import org.apache.commons.lang3.builder.ToStringStyle;
@@ -40,8 +42,8 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.fsprovider.internal.ContentFileExtensions;
 import org.apache.sling.fsprovider.internal.FsMode;
+import org.apache.sling.fsprovider.internal.mapper.jcr.FsNode;
 import org.apache.sling.fsprovider.internal.mapper.valuemap.ValueMapDecorator;
-import org.apache.sling.fsprovider.internal.parser.ContentElement;
 import org.apache.sling.fsprovider.internal.parser.ContentFileCache;
 import org.apache.sling.jcr.contentparser.ParserOptions;
 import org.slf4j.Logger;
@@ -203,6 +205,13 @@ public final class FileResource extends AbstractResource {
         else if (type == ValueMap.class) {
             return (AdapterType) getValueMap();
         }
+        else if (type == Node.class) {
+            ContentFile contentFile = getNodeDescriptorContentFile();
+            if (contentFile != null) {
+                // support a subset of JCR API for content file resources
+                return (AdapterType)new FsNode(contentFile, getResourceResolver());
+            }
+        }
         return super.adaptTo(type);
     }
 
@@ -229,9 +238,9 @@ public final class FileResource extends AbstractResource {
                 props.put("jcr:created", lastModifed);
                 
                 // overlay properties with those from node descriptor content file, if it exists
-                ContentElement content = getNodeDescriptorContent();
-                if (content != null) {
-                    for (Map.Entry<String, Object> entry : content.getProperties().entrySet()) {
+                ContentFile contentFile = getNodeDescriptorContentFile();
+                if (contentFile != null) {
+                    for (Map.Entry<String, Object> entry : contentFile.getValueMap().entrySet()) {
                         // skip primary type if it is the default type assigned by contentparser when none is defined
                         if (StringUtils.equals(entry.getKey(), "jcr:primaryType")
                                 && StringUtils.equals((String)entry.getValue(), ParserOptions.DEFAULT_PRIMARY_TYPE)) {
@@ -247,14 +256,14 @@ public final class FileResource extends AbstractResource {
         return valueMap;
     }
     
-    private ContentElement getNodeDescriptorContent() {
+    private ContentFile getNodeDescriptorContentFile() {
         if (contentFileExtensions == null || contentFileCache == null) {
             return null;
         }
         for (String fileNameSuffix : contentFileExtensions.getSuffixes()) {
             File fileWithSuffix = new File(file.getPath() + fileNameSuffix);
             if (fileWithSuffix.exists() && fileWithSuffix.canRead()) {
-                return contentFileCache.get(resourcePath, fileWithSuffix);
+                return new ContentFile(fileWithSuffix, resourcePath, null, contentFileCache);
             }
         }
         return null;
diff --git a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
index 7fb7ec8..553080f 100644
--- a/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
+++ b/src/test/java/org/apache/sling/fsprovider/internal/JsonContentTest.java
@@ -278,6 +278,11 @@ public class JsonContentTest {
         assertArrayEquals(new String[] { "mix:language" }, props.get("jcr:mixinTypes", String[].class));
 
         assertNull(fsroot.getChild("folder2/folder21/file21a.txt.xml"));
+        
+        Node node = file21a.adaptTo(Node.class);
+        assertNotNull(node);
+        assertEquals("/my/super/type", node.getProperty("sling:resourceSuperType").getString());
+        assertEquals("en", node.getProperty("jcr:language").getString());
     }
 
 }

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