private CacheableResource getOrBuildResource()

in it-is-cloudy-here/org-apache-sling-remote-resourceprovider/src/main/java/org/apache/sling/remote/resourceprovider/impl/RemoteResourceProvider.java [300:369]


    private CacheableResource getOrBuildResource(@NotNull String slingPath, @NotNull Map<String, Object> authenticationInfo) {
        String user = extractUser(authenticationInfo);
        if (requiresAuthentication && user == null) {
            return null;
        }
        if (isNegativeHit(slingPath, user)) {
            return null;
        }
        CacheableResource cacheableResource = queryCaches(slingPath, user);
        if (cacheableResource == null) {
            String storagePath = remoteStorageProvider.storagePath(slingPath);
            RemoteResourceReference resource = remoteStorageProvider.findResource(slingPath, authenticationInfo);
            if (resource != null) {
                if (storagePath.equals(resource.getPath())) {
                    if (resource.getType() == RemoteResourceReference.Type.FILE) {
                        cacheableResource = buildResource(slingPath, resource);
                    } else if (resource.getType() == RemoteResourceReference.Type.DIRECTORY) {
                        Directory directory = remoteStorageProvider.getDirectory(resource, authenticationInfo);
                        if (directory != null) {
                            cacheableResource = buildResource(authenticationInfo, slingPath, resource, directory);
                        }
                    }
                } else if (resource.getType() == RemoteResourceReference.Type.DIRECTORY) {
                    Directory directory = remoteStorageProvider.getDirectory(resource, authenticationInfo);
                    if (directory != null && storagePath.startsWith(resource.getPath())) {
                        String relativePath = storagePath.substring(resource.getPath().length());
                        for (RemoteResourceReference r : directory.getChildren()) {
                            if (SLING_META_FILE.equals(r.getName())) {
                                File metaFile = remoteStorageProvider.getFile(r, authenticationInfo);
                                if (metaFile != null) {
                                    AtomicReference<CacheableResource> resourceReference = new AtomicReference<>();
                                    try {
                                        jsonParser.parse((String path, Map<String, Object> properties) -> {
                                                    if (relativePath.equals(path)) {
                                                        resourceReference
                                                                .set(new CacheableResource(remoteStorageProvider, r, slingPath,
                                                                        properties));
                                                    }
                                                },
                                                metaFile.getInputStream(),
                                                JSON_PARSER_OPTIONS);
                                        cacheableResource = resourceReference.get();
                                    } catch (IOException e) {
                                        LOGGER.error("Unable to parse file " + metaFile.getPath(), e);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                if (cacheableResource != null) {
                    populateCaches(cacheableResource, user);
                } else {
                    markNegativeHit(slingPath, user);
                }
            } else {
                markNegativeHit(slingPath, user);
            }
        }
        if (requiresAuthentication) {
            if (accessMappings.containsKey(slingPath) &&
                    accessMappings.get(slingPath).contains(user)) {
                return cacheableResource;
            }
            return null;
        } else {
            return cacheableResource;
        }
    }