Skip to content

Commit 1dbc1bc

Browse files
authored
alias refactoring - use Resource instead of Path when getting aliases - fix recursion when Resource parent not accessible (#183)
* Reapply "SLING-12787: ResourceResolver: alias refactoring - use Resource instead of Path when getting aliases (#178)" This reverts commit 1366946. * SLING-12787: ResourceResolver: alias refactoring - use Resource instead of Path when getting aliases - fix recursion when Resource parent not accessible * SLING-12787: ResourceResolver: alias refactoring - use Resource instead of Path when getting aliases - typo, and making IntelliJ happier
1 parent 1366946 commit 1dbc1bc

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ public class ResourceResolverImpl extends SlingAdaptable implements ResourceReso
8383

8484
public static final String PROP_ALIAS = "sling:alias";
8585

86-
// The suffix of a resource being a content node of some parent
87-
// such as nt:file. The slash is included to prevent false
88-
// positives for the String.endsWith check for names like
89-
// "xyzjcr:content"
90-
public static final String JCR_CONTENT_LEAF = "/jcr:content";
91-
9286
protected static final String PARENT_RT_CACHEKEY = ResourceResolverImpl.class.getName() + ".PARENT_RT";
9387

9488
/** The factory which created this resource resolver. */

src/main/java/org/apache/sling/resourceresolver/impl/mapping/ResourceMapperImpl.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.sling.resourceresolver.impl.helper.URI;
3636
import org.apache.sling.resourceresolver.impl.helper.URIException;
3737
import org.apache.sling.resourceresolver.impl.params.ParsedParameters;
38+
import org.jetbrains.annotations.NotNull;
3839
import org.slf4j.Logger;
3940
import org.slf4j.LoggerFactory;
4041

@@ -231,35 +232,44 @@ private List<String> loadAliasesIfApplicable(final Resource nonDecoratedResource
231232
return mappedPaths;
232233
}
233234

234-
private void resolveAliases(Resource res, PathGenerator pathBuilder) {
235-
String path = res.getPath();
235+
/*
236+
* Populate a {@linkplain PathGenerator} based on the aliases of this resource, plus all ancestors
237+
* @param resource the resource from which to start
238+
* @param pathGenerator path generator to populate
239+
*/
240+
private void resolveAliases(@NotNull Resource resource, @NotNull PathGenerator pathGenerator) {
241+
Resource current = resource;
242+
String path = current.getPath();
236243

237-
while (path != null) {
238-
Collection<String> aliases = Collections.emptyList();
239-
// read alias only if we can read the resources and it's not a jcr:content leaf
240-
if (!path.endsWith(ResourceResolverImpl.JCR_CONTENT_LEAF)) {
241-
aliases = readAliases(path);
242-
}
243-
// build the path from the name segments or aliases
244-
pathBuilder.insertSegment(aliases, ResourceUtil.getName(path));
244+
while (path != null && !"/".equals(path)) {
245+
String name = ResourceUtil.getName(path);
246+
247+
// read aliases only if it's not a jcr:content resource, and we actually have a resource
248+
Collection<String> aliases =
249+
current == null || name.equals("jcr:content") ? Collections.emptyList() : readAliases(current);
250+
251+
// build the path segment from the name and the discovered aliases
252+
pathGenerator.insertSegment(aliases, name);
253+
254+
// current can already be or can become null here due to missing access rights
255+
current = current != null ? current.getParent() : null;
256+
257+
// traverse up
245258
path = ResourceUtil.getParent(path);
246-
if ("/".equals(path)) {
247-
path = null;
248-
}
249259
}
250260
}
251261

252262
/**
253263
* Resolve the aliases for the given resource by a lookup in MapEntries
254-
* @param path path for which to lookup aliases
264+
* @param resource resource for which to lookup aliases
255265
* @return collection of aliases for that resource
256266
*/
257-
private Collection<String> readAliases(String path) {
258-
String parentPath = ResourceUtil.getParent(path);
259-
if (parentPath == null) {
267+
private @NotNull Collection<String> readAliases(@NotNull Resource resource) {
268+
Resource parent = resource.getParent();
269+
if (parent == null) {
260270
return Collections.emptyList();
261271
} else {
262-
return mapEntries.getAliasMap(parentPath).getOrDefault(ResourceUtil.getName(path), Collections.emptyList());
272+
return mapEntries.getAliasMap(parent).getOrDefault(resource.getName(), Collections.emptyList());
263273
}
264274
}
265275

src/test/java/org/apache/sling/resourceresolver/impl/MockedResourceResolverImplTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.apache.sling.api.resource.ResourceMetadata;
4141
import org.apache.sling.api.resource.ResourceResolver;
4242
import org.apache.sling.api.resource.ResourceResolverFactory;
43+
import org.apache.sling.api.resource.ResourceUtil;
4344
import org.apache.sling.api.resource.ValueMap;
4445
import org.apache.sling.api.wrappers.ValueMapDecorator;
4546
import org.apache.sling.resourceresolver.impl.mapping.MapEntries;
@@ -499,7 +500,7 @@ private List<ValueMap> buildValueMapCollection(int howMany, String pathPrefix) {
499500
}
500501

501502
/**
502-
* Build a resource with path, children and resource resolver.
503+
* Build a resource with parent, path, children and resource resolver.
503504
* @param fullpath
504505
* @param children
505506
* @param resourceResolver
@@ -512,9 +513,18 @@ private Resource buildResource(
512513
ResourceResolver resourceResolver,
513514
ResourceProvider<?> provider,
514515
String... properties) {
516+
517+
// build a mocked parent resource so that getParent() can return something meaningful (it is null when we are
518+
// already at root level)
519+
Resource parentResource = fullpath == null || "/".equals(fullpath)
520+
? null
521+
: buildResource(
522+
ResourceUtil.getParent(fullpath), Collections.emptyList(), resourceResolver, provider, null);
523+
515524
Resource resource = mock(Resource.class);
516525
Mockito.when(resource.getName()).thenReturn(getResourceName(fullpath));
517526
Mockito.when(resource.getPath()).thenReturn(fullpath);
527+
Mockito.when(resource.getParent()).thenReturn(parentResource);
518528
ResourceMetadata resourceMetadata = new ResourceMetadata();
519529
Mockito.when(resource.getResourceMetadata()).thenReturn(resourceMetadata);
520530
Mockito.when(resource.listChildren()).thenReturn(children.iterator());

0 commit comments

Comments
 (0)