This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git
The following commit(s) were added to refs/heads/master by this push:
new fe35d53 SLING-12781 Always expose resource type via ValueMap (#45)
fe35d53 is described below
commit fe35d53a1b0f8ff1a7616909f16539b759bd0bdd
Author: Konrad Windszus <[email protected]>
AuthorDate: Tue Jun 17 14:07:05 2025 +0200
SLING-12781 Always expose resource type via ValueMap (#45)
---
.../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, 38 insertions(+), 43 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 8c16fbe..a025342 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,6 +19,7 @@
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.
@@ -41,8 +42,10 @@ 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.
*/
- public static final String SLING_RESOURCE_TYPE_PROPERTY =
"sling:resourceType";
+ @Deprecated
+ public static final String SLING_RESOURCE_TYPE_PROPERTY =
ResourceResolver.PROPERTY_RESOURCE_TYPE;
/**
* 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 1b6ae65..7384046 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,15 +26,18 @@ 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;
@@ -297,6 +300,11 @@ 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 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..0514072 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(ResourceResolver.PROPERTY_RESOURCE_TYPE,
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..59cf205 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(ResourceResolver.PROPERTY_RESOURCE_TYPE, String.class)
+ "/" + 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 402d0a4..9e3210c 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(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY);
+ final Object resourceTypeObject =
properties.get(ResourceResolver.PROPERTY_RESOURCE_TYPE);
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 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..3fa404e 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);
@@ -171,6 +166,7 @@ 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";
@@ -181,13 +177,14 @@ public class JcrNodeResourceTest extends
JcrItemResourceTestBase {
assertEquals(typeName, jnr.getResourceType());
assertEquals(otherSuperTypeName, jnr.getResourceSuperType());
- // remove direct property to get supertype again
+ // remove direct property to clear 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 c004b88..d0785dc 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,6 +65,7 @@ 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();
@@ -77,7 +78,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());
}
}