public Resource getResource()

in org.apache.sling.ddr/core/src/main/java/org/apache/sling/ddr/core/DeclarativeDynamicResourceProviderHandler.java [145:201]


    public Resource getResource(ResolveContext ctx, String path, ResourceContext resourceContext, Resource parent) {
        ResourceResolver contextResourceResolver = ctx.getResourceResolver();
        log.info("Get Resource, path: '{}', parent: '{}', provider root: '{}'", path, parent, providerRootPath);
        String resourcePath;
        if(path.startsWith(SLASH)) {
            resourcePath = path;
        } else {
            resourcePath = parent.getPath() + SLASH + path;
        }
        Resource answer = null;
        if(resourcePath.startsWith(providerRootPath)) {
            answer = getResource(contextResourceResolver, resourcePath);
        } else if(resourcePath.startsWith(targetRootPath)) {
            log.info("Before Getting Resource from Parent, path: '{}'", resourcePath);
            ResourceProvider parentResourceProvider = ctx.getParentResourceProvider();
            ResolveContext parentResolveContext = ctx.getParentResolveContext();
            log.info("Parent Resource Provider: '{}'", parentResourceProvider);
            log.info("Parent Resolve Context: '{}'", parentResolveContext);
            if (parentResourceProvider != null && parentResolveContext != null) {
                answer = parentResourceProvider.getResource(parentResolveContext, resourcePath, resourceContext, parent);
            }
            log.info("After Getting Resource from Parent, path: '{}', resource: '{}'", resourcePath, answer);
            if(answer == null) {
                synchronized (lock) {
                    Reference mappedPath = mappings.get(resourcePath);
                    if (mappedPath == null) {
                        // Obtain parent path and list children then try to re-obtain the mapping, if not found then there is no mapping
                        int index = resourcePath.lastIndexOf('/');
                        if (index > 0 && index < resourcePath.length() - 1) {
                            String parentPath = resourcePath.substring(0, index);
                            obtainChildren(contextResourceResolver, parentPath, false);
                            mappedPath = mappings.get(resourcePath);
                        }
                    }
                    if (mappedPath != null) {
                        Resource source = getResource(
                            contextResourceResolver, mappedPath.isRef() ?
                                mappedPath.getReference():
                                mappedPath.getSource()
                        );
                        int index = resourcePath.lastIndexOf('/');
                        String parentPath = "";
                        if (index > 0 && index < resourcePath.length() - 1) {
                            parentPath = resourcePath.substring(0, index);
                        }
                        answer = createSyntheticFromResource(
                            source, resourcePath, parentPath.equals(targetRootPath)
                        );
                    }
                }
            }
        } else {
            answer = getResource(contextResourceResolver, resourcePath);
        }
        log.info("Return resource: '{}'", answer);
        return answer;
    }