PageResourceProvider.java 6.23 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.rest.ApiResourceProvider
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.rest.content.impl;

import com.adobe.cq.rest.content.impl.PageResourceFactory;
import com.adobe.granite.rest.ApiResourceProvider;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PageResourceProvider
implements ApiResourceProvider {
    private static final Logger log = LoggerFactory.getLogger(PageResourceProvider.class);
    public static final String TYPE = "content";
    public static final String CONTENT_ROOT = "/content";
    private PageResourceFactory resourceFactory;
    private String apiContextPath;
    private Map<String, String> inclusionMapping;
    private String[] excludePaths;

    public PageResourceProvider(String contextPath, Map<String, String> inclusionMapping, String[] excludePaths) {
        this.inclusionMapping = inclusionMapping;
        this.excludePaths = excludePaths;
        this.apiContextPath = contextPath + "/" + "content";
        this.resourceFactory = new PageResourceFactory(this.apiContextPath);
    }

    public Resource getResource(ResourceResolver resolver, String path) {
        Resource resource = null;
        if (!StringUtils.isEmpty((String)path)) {
            String resourcePath = this.unmap(this.apiContextPath + path);
            if (resourcePath != null) {
                resource = resolver.getResource(resourcePath);
            }
        } else {
            resource = resolver.getResource("/content");
        }
        if (resource == null) {
            return null;
        }
        return this.resourceFactory.createResource(resource, this.apiContextPath + path);
    }

    public Iterator<Resource> listChildren(Resource parent) {
        log.debug("Listing children for resource {}.", (Object)parent);
        String parentPath = parent.getPath();
        if (!StringUtils.isEmpty((String)parentPath)) {
            ResourceResolver resolver = parent.getResourceResolver();
            if (this.apiContextPath.equals(parentPath)) {
                return this.getRootChildren(resolver);
            }
            String resourcePath = this.unmap(parentPath);
            Resource parentResource = resolver.getResource(resourcePath);
            if (parentResource == null) {
                log.warn("Parent resource {} not found", (Object)resourcePath);
                return null;
            }
            return this.getResourceChildren(parentResource);
        }
        return null;
    }

    public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
        throw new UnsupportedOperationException("Operation not supported for resource");
    }

    public void delete(ResourceResolver resolver, String path) throws PersistenceException {
        throw new UnsupportedOperationException("Operation not supported for resource");
    }

    private Iterator<Resource> getRootChildren(ResourceResolver resolver) {
        LinkedList<Resource> children = new LinkedList<Resource>();
        for (Map.Entry<String, String> e : this.inclusionMapping.entrySet()) {
            String root = e.getKey();
            Resource parent = resolver.getResource(root);
            if (parent == null) {
                log.warn("Resource for path {} not found.", (Object)root);
                continue;
            }
            children.addAll(this.getChildren(parent));
        }
        return children.iterator();
    }

    private Iterator<Resource> getResourceChildren(Resource parent) {
        return this.getChildren(parent).iterator();
    }

    private List<Resource> getChildren(Resource parent) {
        LinkedList<Resource> children = new LinkedList<Resource>();
        Iterator it = parent.listChildren();
        while (it.hasNext()) {
            Resource childRes;
            String apiPath;
            Resource child = (Resource)it.next();
            String[] includePaths = this.inclusionMapping.keySet().toArray(new String[0]);
            if (!this.matches(includePaths, child.getPath()) || this.matches(this.excludePaths, child.getPath()) || (apiPath = this.map(child.getPath())) == null || (childRes = this.resourceFactory.createResource(child, apiPath)) == null) continue;
            children.add(childRes);
        }
        return children;
    }

    private boolean matches(String[] paths, String path) {
        for (String p : paths) {
            if (!path.startsWith(p)) continue;
            return true;
        }
        return false;
    }

    String unmap(String apiPath) {
        String resourcePath = apiPath.replaceFirst(this.apiContextPath, "").replaceFirst("/", "");
        String root = "/content";
        String suffix = null;
        for (Map.Entry<String, String> entry : this.inclusionMapping.entrySet()) {
            String path = entry.getKey();
            String map = entry.getValue();
            if (!resourcePath.startsWith(map)) continue;
            root = path;
            suffix = map;
        }
        if (suffix == null) {
            return null;
        }
        return apiPath.replaceFirst(this.apiContextPath + "/" + suffix, root);
    }

    String map(String resourcePath) {
        String root = "/content";
        String suffix = null;
        for (Map.Entry<String, String> entry : this.inclusionMapping.entrySet()) {
            String path = entry.getKey();
            String map = entry.getValue();
            if (!resourcePath.startsWith(path)) continue;
            root = path;
            suffix = map;
        }
        if (suffix == null) {
            return null;
        }
        return resourcePath.replaceFirst(root, this.apiContextPath + "/" + suffix);
    }
}