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

kwin pushed a commit to branch feature/expose-type-via-valuemap
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git

commit 4fd8ebdc31f7a35f32fa4a35978a42bff93a4469
Author: Konrad Windszus <[email protected]>
AuthorDate: Fri May 30 17:24:04 2025 +0200

    SLING-12781 Expose resource type via ValueMap
---
 .../sling/jcr/resource/internal/JcrValueMap.java   |  6 +++++
 .../internal/helper/jcr/JcrItemResource.java       | 28 ----------------------
 .../helper/jcr/JcrItemResourceFactory.java         |  2 +-
 .../internal/helper/jcr/JcrNodeResource.java       |  5 ++--
 .../internal/helper/jcr/JcrPropertyResource.java   |  9 +++++--
 .../helper/jcr/JcrItemResourceTestBase.java        |  8 +++++++
 .../internal/helper/jcr/JcrNodeResourceTest.java   |  7 +-----
 .../helper/jcr/JcrPropertyResourceTest.java        |  2 +-
 8 files changed, 27 insertions(+), 40 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java 
b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
index 1b6ae65..de55181 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
@@ -36,6 +36,7 @@ import org.apache.jackrabbit.util.ISO9075;
 import org.apache.jackrabbit.util.Text;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.jcr.resource.api.JcrResourceConstants;
 import org.apache.sling.jcr.resource.internal.helper.JcrPropertyMapCacheEntry;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -297,6 +298,11 @@ public class JcrValueMap implements ValueMap {
         try {
             final String key = escapeKeyName(name);
             Property property = NodeUtil.getPropertyOrNull(node,key);
+            if (property == null && 
name.equals(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY)) {
+                // special handling for the resource type property which 
according to the API must always be exposed via property sling:resourceType
+                // use value of jcr:primaryType if sling:resourceType is not 
set
+                property = NodeUtil.getPropertyOrNull(node, 
Property.JCR_PRIMARY_TYPE);
+            }
             if (property != null) {
                 return cacheProperty(property);
             }
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
index d81cd8e..020fa95 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResource.java
@@ -111,34 +111,6 @@ abstract class JcrItemResource<T extends Item> // this 
should be package private
         return item;
     }
 
-    /**
-     * Compute the resource type of the given node, using either the
-     * SLING_RESOURCE_TYPE_PROPERTY, or the node's primary node type, if the
-     * property is not set
-     */
-    @NotNull
-    protected String getResourceTypeForNode(final @NotNull Node node) throws 
RepositoryException {
-        String result = null;
-
-        Property property = NodeUtil.getPropertyOrNull(node, 
JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY);
-        if (property != null) {
-            result = property.getString();
-        }
-
-        if (result == null || result.length() == 0) {
-            // Do not load the relatively expensive NodeType object unless 
necessary. See OAK-2441 for the reason why it
-            // cannot only use getProperty here.
-            property = NodeUtil.getPropertyOrNull(node, 
Property.JCR_PRIMARY_TYPE);
-            if (property != null) {
-                result = property.getString();
-            } else {
-                result = node.getPrimaryNodeType().getName();
-            }
-        }
-
-        return result;
-    }
-
     public static long getContentLength(final @NotNull Property property) 
throws RepositoryException {
         if (property.isMultiple()) {
             return -1;
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
index 6bd3806..3492851 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceFactory.java
@@ -100,7 +100,7 @@ public class JcrItemResourceFactory {
                 }
             } else {
                 log.debug("createResource: Found JCR Property Resource at path 
'{}'", resourcePath);
-                resource = new JcrPropertyResource(resourceResolver, 
resourcePath, version, (Property) item);
+                resource = new JcrPropertyResource(resourceResolver, 
resourcePath, version, (Property) item, helper);
             }
             resource.getResourceMetadata().setParameterMap(parameters);
             return resource;
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
index 57b58b4..8aa13b4 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResource.java
@@ -93,8 +93,9 @@ class JcrNodeResource extends JcrItemResource<Node> { // this 
should be package
     public @NotNull String getResourceType() {
         if (this.resourceType == null) {
             try {
-                this.resourceType = getResourceTypeForNode(getNode());
-            } catch (final RepositoryException e) {
+                this.resourceType = new JcrValueMap(getNode(), this.helper)
+                        
.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, String.class);
+            } catch (final IllegalArgumentException e) {
                 LOGGER.error("Unable to get resource type for node " + 
getNode(), e);
                 this.resourceType = "<unknown resource type>";
             }
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
index a4ec14b..6bc8cd2 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
@@ -36,6 +36,9 @@ import org.apache.sling.adapter.annotations.Adapter;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.jcr.resource.api.JcrResourceConstants;
+import org.apache.sling.jcr.resource.internal.HelperData;
+import org.apache.sling.jcr.resource.internal.JcrValueMap;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
@@ -56,9 +59,11 @@ class JcrPropertyResource extends JcrItemResource<Property> 
{ // this should be
     public JcrPropertyResource(final @NotNull ResourceResolver 
resourceResolver,
                                final @NotNull String path,
                                final @Nullable String version,
-                               final @NotNull Property property) throws 
RepositoryException {
+                               final @NotNull Property property,
+                               final @NotNull HelperData helper) throws 
RepositoryException {
         super(resourceResolver, path, version, property, new 
ResourceMetadata());
-        this.resourceType = getResourceTypeForNode(property.getParent())
+        this.resourceType = new JcrValueMap(property.getNode(), helper)
+                .get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, 
String.class)
                 + "/" + property.getName();
         if (PropertyType.BINARY != getProperty().getType()) {
             this.getResourceMetadata().setContentType("text/plain");
diff --git 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
index f6c0392..e1d5b8e 100644
--- 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
+++ 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrItemResourceTestBase.java
@@ -20,12 +20,16 @@ package org.apache.sling.jcr.resource.internal.helper.jcr;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
 
 import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.resource.external.URIProvider;
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
 import org.apache.sling.jcr.resource.api.JcrResourceConstants;
+import org.apache.sling.jcr.resource.internal.HelperData;
 
 public abstract class JcrItemResourceTestBase extends SlingRepositoryTestBase {
 
@@ -90,4 +94,8 @@ public abstract class JcrItemResourceTestBase extends 
SlingRepositoryTestBase {
             }
         }
     }
+
+    public static HelperData getHelperData() {
+        return new HelperData(new AtomicReference<>(), new 
AtomicReference<>());
+    }
 }
diff --git 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
index 4b870af..910286d 100644
--- 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
+++ 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrNodeResourceTest.java
@@ -25,7 +25,6 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicReference;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
@@ -37,14 +36,9 @@ import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.jcr.resource.api.JcrResourceConstants;
-import org.apache.sling.jcr.resource.internal.HelperData;
 
 public class JcrNodeResourceTest extends JcrItemResourceTestBase {
 
-    private HelperData getHelperData() {
-        return new HelperData(new AtomicReference<>(), new 
AtomicReference<>());
-    }
-
     public void testLinkedFile() throws Exception {
         String fileName = "file";
         String linkedFileName = "linkedFile";
@@ -140,6 +134,7 @@ public class JcrNodeResourceTest extends 
JcrItemResourceTestBase {
 
         JcrNodeResource jnr = new JcrNodeResource(null, node.getPath(), null, 
node, getHelperData());
         assertEquals(JcrConstants.NT_UNSTRUCTURED, jnr.getResourceType());
+        assertEquals(JcrConstants.NT_UNSTRUCTURED, 
jnr.getValueMap().get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, 
String.class));
 
         String typeName = "some/resource/type";
         node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, 
typeName);
diff --git 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResourceTest.java
 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResourceTest.java
index c004b88..0bcf18a 100644
--- 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResourceTest.java
+++ 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResourceTest.java
@@ -77,7 +77,7 @@ public class JcrPropertyResourceTest {
                 allowing(property).getString();
                 will(returnValue(stringValue));
             }});
-            final JcrPropertyResource propResource = new 
JcrPropertyResource(resolver, "/path/to/string-property", null, property);
+            final JcrPropertyResource propResource = new 
JcrPropertyResource(resolver, "/path/to/string-property", null, property, 
JcrItemResourceTestBase.getHelperData());
             assertEquals("Byte length of " + stringValue, stringByteLength, 
propResource.getResourceMetadata().getContentLength());
         }
     }

Reply via email to