|
35 | 35 | import org.apache.sling.resourceresolver.impl.helper.URI;
|
36 | 36 | import org.apache.sling.resourceresolver.impl.helper.URIException;
|
37 | 37 | import org.apache.sling.resourceresolver.impl.params.ParsedParameters;
|
| 38 | +import org.jetbrains.annotations.NotNull; |
38 | 39 | import org.slf4j.Logger;
|
39 | 40 | import org.slf4j.LoggerFactory;
|
40 | 41 |
|
@@ -231,35 +232,44 @@ private List<String> loadAliasesIfApplicable(final Resource nonDecoratedResource
|
231 | 232 | return mappedPaths;
|
232 | 233 | }
|
233 | 234 |
|
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(); |
236 | 243 |
|
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 |
245 | 258 | path = ResourceUtil.getParent(path);
|
246 |
| - if ("/".equals(path)) { |
247 |
| - path = null; |
248 |
| - } |
249 | 259 | }
|
250 | 260 | }
|
251 | 261 |
|
252 | 262 | /**
|
253 | 263 | * 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 |
255 | 265 | * @return collection of aliases for that resource
|
256 | 266 | */
|
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) { |
260 | 270 | return Collections.emptyList();
|
261 | 271 | } else {
|
262 |
| - return mapEntries.getAliasMap(parentPath).getOrDefault(ResourceUtil.getName(path), Collections.emptyList()); |
| 272 | + return mapEntries.getAliasMap(parent).getOrDefault(resource.getName(), Collections.emptyList()); |
263 | 273 | }
|
264 | 274 | }
|
265 | 275 |
|
|
0 commit comments