This is an automated email from the ASF dual-hosted git repository. kwin pushed a commit to branch bugfix/revert-exposing-sling-resource-type in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git
commit bbe85003585b31e232304e7e70c00b4a5824ba07 Author: Konrad Windszus <[email protected]> AuthorDate: Wed Sep 24 15:43:34 2025 +0200 Revert "SLING-12781 Always expose resource type via ValueMap (#45)" This reverts commit fe35d53a1b0f8ff1a7616909f16539b759bd0bdd. --- .../jcr/resource/api/JcrResourceConstants.java | 5 +--- .../sling/jcr/resource/internal/JcrValueMap.java | 8 ------- .../internal/helper/jcr/JcrItemResource.java | 28 ++++++++++++++++++++++ .../helper/jcr/JcrItemResourceFactory.java | 2 +- .../internal/helper/jcr/JcrNodeResource.java | 5 ++-- .../internal/helper/jcr/JcrPropertyResource.java | 9 ++----- .../internal/helper/jcr/JcrResourceProvider.java | 2 +- .../helper/jcr/JcrItemResourceTestBase.java | 8 ------- .../internal/helper/jcr/JcrNodeResourceTest.java | 11 +++++---- .../helper/jcr/JcrPropertyResourceTest.java | 3 +-- 10 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/sling/jcr/resource/api/JcrResourceConstants.java b/src/main/java/org/apache/sling/jcr/resource/api/JcrResourceConstants.java index a025342..8c16fbe 100644 --- a/src/main/java/org/apache/sling/jcr/resource/api/JcrResourceConstants.java +++ b/src/main/java/org/apache/sling/jcr/resource/api/JcrResourceConstants.java @@ -19,7 +19,6 @@ package org.apache.sling.jcr.resource.api; import org.apache.sling.api.SlingConstants; -import org.apache.sling.api.resource.ResourceResolver; /** * The <code>JcrResourceConstants</code> interface provides constant values. @@ -42,10 +41,8 @@ public class JcrResourceConstants { * this bundle uses this property to defined the resource type of a loaded * resource. If this property does not exist the primary node type is used * as the resource type. - * @deprecated Use {@link ResourceResolver#PROPERTY_RESOURCE_TYPE} instead. */ - @Deprecated - public static final String SLING_RESOURCE_TYPE_PROPERTY = ResourceResolver.PROPERTY_RESOURCE_TYPE; + public static final String SLING_RESOURCE_TYPE_PROPERTY = "sling:resourceType"; /** * The name of the JCR Property that defines the resource super type (value 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 7384046..1b6ae65 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 @@ -26,18 +26,15 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; - import javax.jcr.Node; import javax.jcr.Property; import javax.jcr.PropertyIterator; import javax.jcr.RepositoryException; import javax.jcr.Value; -import org.apache.jackrabbit.JcrConstants; 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.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import org.apache.sling.jcr.resource.internal.helper.JcrPropertyMapCacheEntry; import org.jetbrains.annotations.NotNull; @@ -300,11 +297,6 @@ public class JcrValueMap implements ValueMap { try { final String key = escapeKeyName(name); Property property = NodeUtil.getPropertyOrNull(node,key); - if (property == null && name.equals(ResourceResolver.PROPERTY_RESOURCE_TYPE)) { - // 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, JcrConstants.JCR_PRIMARYTYPE); - } 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 020fa95..d81cd8e 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,6 +111,34 @@ 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 3492851..6bd3806 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, helper); + resource = new JcrPropertyResource(resourceResolver, resourcePath, version, (Property) item); } 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 0514072..57b58b4 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,9 +93,8 @@ class JcrNodeResource extends JcrItemResource<Node> { // this should be package public @NotNull String getResourceType() { if (this.resourceType == null) { try { - this.resourceType = new JcrValueMap(getNode(), this.helper) - .get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class); - } catch (final IllegalArgumentException e) { + this.resourceType = getResourceTypeForNode(getNode()); + } catch (final RepositoryException 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 59cf205..a4ec14b 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,9 +36,6 @@ 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; @@ -59,11 +56,9 @@ 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, - final @NotNull HelperData helper) throws RepositoryException { + final @NotNull Property property) throws RepositoryException { super(resourceResolver, path, version, property, new ResourceMetadata()); - this.resourceType = new JcrValueMap(property.getNode(), helper) - .get(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class) + this.resourceType = getResourceTypeForNode(property.getParent()) + "/" + property.getName(); if (PropertyType.BINARY != getProperty().getType()) { this.getResourceMetadata().setContentType("text/plain"); diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java index 9e3210c..402d0a4 100644 --- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java +++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java @@ -499,7 +499,7 @@ public class JcrResourceProvider extends ResourceProvider<JcrProviderState> { return primaryTypeObj.toString(); } - final Object resourceTypeObject = properties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE); + final Object resourceTypeObject = properties.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY); if (resourceTypeObject != null) { String resourceType = resourceTypeObject.toString(); if (looksLikeANodeType(resourceType)) { 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 e1d5b8e..f6c0392 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,16 +20,12 @@ 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 { @@ -94,8 +90,4 @@ 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 3fa404e..4b870af 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,6 +25,7 @@ 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; @@ -36,9 +37,14 @@ 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"; @@ -134,7 +140,6 @@ 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); @@ -166,7 +171,6 @@ public class JcrNodeResourceTest extends JcrItemResourceTestBase { jnr = new JcrNodeResource(null, typeNode.getPath(), null, typeNode, getHelperData()); assertEquals(JcrConstants.NT_UNSTRUCTURED, jnr.getResourceType()); assertEquals(superTypeName, jnr.getResourceSuperType()); - assertEquals(superTypeName, jnr.getValueMap().get(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY, String.class)); // overwrite super type with direct property String otherSuperTypeName = "othersupertype"; @@ -177,14 +181,13 @@ public class JcrNodeResourceTest extends JcrItemResourceTestBase { assertEquals(typeName, jnr.getResourceType()); assertEquals(otherSuperTypeName, jnr.getResourceSuperType()); - // remove direct property to clear supertype again + // remove direct property to get supertype again node.getProperty(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY).remove(); getSession().save(); jnr = new JcrNodeResource(null, node.getPath(), null, node, getHelperData()); assertEquals(typeName, jnr.getResourceType()); assertNull(jnr.getResourceSuperType()); - assertNull(jnr.getValueMap().get(JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY, String.class)); } public void testAdaptToMap() throws Exception { 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 d0785dc..c004b88 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 @@ -65,7 +65,6 @@ public class JcrPropertyResourceTest { final Property property = this.context.mock(Property.class, stringValue); this.context.checking(new Expectations() {{ ignoring(resolver); - allowing(property).getNode(); allowing(property).getParent(); allowing(property).getName(); allowing(property).isMultiple(); @@ -78,7 +77,7 @@ public class JcrPropertyResourceTest { allowing(property).getString(); will(returnValue(stringValue)); }}); - final JcrPropertyResource propResource = new JcrPropertyResource(resolver, "/path/to/string-property", null, property, JcrItemResourceTestBase.getHelperData()); + final JcrPropertyResource propResource = new JcrPropertyResource(resolver, "/path/to/string-property", null, property); assertEquals("Byte length of " + stringValue, stringByteLength, propResource.getResourceMetadata().getContentLength()); } }
