ApiEndpointResourceProvider.java 5.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.ModifyingResourceProvider
 *  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.granite.rest.impl;

import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.ApiResourceProviderFactory;
import com.adobe.granite.rest.impl.ApiEndpointResource;
import com.adobe.granite.rest.impl.ApiEndpointResourceProviderFactory;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifyingResourceProvider;
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 ApiEndpointResourceProvider
implements ModifyingResourceProvider {
    private static final Logger log = LoggerFactory.getLogger(ApiEndpointResourceProvider.class);
    private String apiContextPath = "";
    private ApiEndpointResourceProviderFactory apiEndpointProviderFactory;

    public ApiEndpointResourceProvider(ApiEndpointResourceProviderFactory apiEndpointProviderFactory) {
        this.apiEndpointProviderFactory = apiEndpointProviderFactory;
        this.apiContextPath = apiEndpointProviderFactory.getRootContextPath();
    }

    public Resource getResource(ResourceResolver resolver, String path) {
        return this.getResource(resolver, null, path);
    }

    public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
        if (this.apiContextPath.equals(path)) {
            try {
                Collection<ApiResourceProviderFactory> providerFactories = this.apiEndpointProviderFactory.getApiProviderFactories();
                return new ApiEndpointResource(resolver, path, providerFactories);
            }
            catch (Exception e) {
                log.error("Unable to return ApiEndpointResource", (Throwable)e);
                return null;
            }
        }
        String providerContextPath = this.getResourceProviderContextPath(path);
        String resourcePath = this.getResourcePath(path);
        ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
        if (resourceProvider != null) {
            return resourceProvider.getResource(resolver, resourcePath);
        }
        return null;
    }

    public Iterator<Resource> listChildren(Resource parent) {
        String path = parent.getPath();
        String providerContextPath = this.getResourceProviderContextPath(path);
        ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
        if (resourceProvider != null) {
            return resourceProvider.listChildren(parent);
        }
        return null;
    }

    public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
        String providerContextPath = this.getResourceProviderContextPath(path);
        String resourcePath = this.getResourcePath(path);
        ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
        if (resourceProvider != null) {
            return resourceProvider.create(resolver, resourcePath, properties);
        }
        return null;
    }

    public void delete(ResourceResolver resolver, String path) throws PersistenceException {
        String providerContextPath = this.getResourceProviderContextPath(path);
        String resourcePath = this.getResourcePath(path);
        ApiResourceProvider resourceProvider = this.getResourceProviderByContext(providerContextPath);
        if (resourceProvider != null) {
            resourceProvider.delete(resolver, resourcePath);
        }
    }

    public void revert(ResourceResolver resolver) {
    }

    public void commit(ResourceResolver resolver) throws PersistenceException {
    }

    public boolean hasChanges(ResourceResolver resolver) {
        return false;
    }

    private ApiResourceProvider getResourceProviderByContext(String contextPath) {
        if (contextPath == null) {
            return null;
        }
        Collection<ApiResourceProviderFactory> providerFactories = this.apiEndpointProviderFactory.getApiProviderFactories();
        for (ApiResourceProviderFactory providerFactory : providerFactories) {
            if (!contextPath.equals(providerFactory.getContextPath())) continue;
            return providerFactory.getResourceProvider(this.apiContextPath);
        }
        return null;
    }

    private String getResourcePath(String path) {
        String resourcePath = "";
        if (StringUtils.countMatches((String)path, (String)"/") > 2) {
            path = StringUtils.removeStart((String)path, (String)this.apiContextPath);
            path = StringUtils.removeStart((String)path, (String)"/");
            resourcePath = path = path.substring(path.indexOf("/"));
        }
        return resourcePath;
    }

    private String getResourceProviderContextPath(String path) {
        String providerType = null;
        if (path.matches("^/(.*?)/(.*?)(/.*)?")) {
            path = StringUtils.removeStart((String)path, (String)this.apiContextPath);
            int idx = (path = StringUtils.removeStart((String)path, (String)"/")).indexOf("/");
            providerType = idx > 0 ? path.substring(0, idx) : path;
        }
        return providerType;
    }
}