SLING-8761 use getItemOrNull for getParent
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 376d7b5..2bd4282 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
@@ -28,12 +28,16 @@
import javax.jcr.RepositoryException;
import javax.jcr.ValueFormatException;
+import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.sling.api.resource.AbstractResource;
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.api.resource.ResourceUtil;
import org.apache.sling.jcr.resource.api.JcrResourceConstants;
+import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -135,6 +139,21 @@
return result;
}
+ /**
+ * @return Parent node or null if not existing or root
+ */
+ public @Nullable Node getParentNode() {
+ String parentPath = ResourceUtil.getParent(getPath());
+ if (StringUtils.isNotBlank(parentPath)) {
+ try {
+ return (Node)((JackrabbitSession)item.getSession()).getItemOrNull(parentPath);
+ } catch (RepositoryException e) {
+ LOGGER.error("Unable to fetch {}", parentPath);
+ }
+ }
+ return null;
+ }
+
public static long getContentLength(final Property property) throws RepositoryException {
if (property.isMultiple()) {
return -1;
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 4f69583..64425da 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
@@ -30,6 +30,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
+import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import javax.jcr.AccessDeniedException;
@@ -357,18 +358,11 @@
version = child.getResourceMetadata().getParameterMap().get("v");
}
if (version == null) {
- Item item = ((JcrItemResource<?>) child).getItem();
- if ("/".equals(item.getPath())) {
+ Node parentNode = ((JcrItemResource)child).getParentNode();
+ if (parentNode == null) {
return null;
}
- Node parentNode;
- try {
- parentNode = item.getParent();
- } catch(AccessDeniedException e) {
- return null;
- }
- String parentPath = ResourceUtil.getParent(child.getPath());
- return new JcrNodeResource(ctx.getResourceResolver(), parentPath, version, parentNode,
+ return new JcrNodeResource(ctx.getResourceResolver(), parentNode.getPath(), null, parentNode,
ctx.getProviderState().getHelperData());
}
} catch (RepositoryException e) {