PageResourceProviderFactory.java 3.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.rest.ApiResourceProvider
 *  com.adobe.granite.rest.ApiResourceProviderFactory
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 */
package com.adobe.cq.rest.content.impl;

import com.adobe.cq.rest.content.impl.PageResourceProvider;
import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.ApiResourceProviderFactory;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;

@Component
@Service
@Properties(value={@Property(name="provider.type", value={"content"})})
public class PageResourceProviderFactory
implements ApiResourceProviderFactory {
    @Property(label="Included", description="Paths exposed by this ResourceProvider under /api/content/<suffix> in the form of <path>=<api suffix>.", value={"/content=sites", "/etc/commerce/products=products"})
    private static final String CFG_INCLUDES = "include.paths";
    @Property(label="Excluded", description="Paths not exposed by this ResourceProvider.", value={"/content/catalogs", "/content/communities", "/content/dam", "/content/launches", "/content/mac", "/content/publications", "/content/usergenerated", "/etc/commerce/products/csvimporter"})
    private static final String CFG_EXCLUDES = "exclude.paths";
    private Map<String, String> inclusionMapping = new HashMap<String, String>();
    private String[] excludePaths;

    public String getContextPath() {
        return "content";
    }

    public ApiResourceProvider getResourceProvider(String rootContextPath) {
        return new PageResourceProvider(rootContextPath, this.inclusionMapping, this.excludePaths);
    }

    @Activate
    protected void activate(ComponentContext context) {
        Dictionary dict = context.getProperties();
        this.inclusionMapping = PropertiesUtil.toMap(dict.get("include.paths"), (String[])new String[0]);
        this.excludePaths = PropertiesUtil.toStringArray(dict.get("exclude.paths"), (String[])new String[0]);
    }

    public String unmap(String apiContextPath, String apiPath) {
        String resourcePath = apiPath.replaceFirst(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(apiContextPath + "/" + suffix, root);
    }

    public String map(String apiContextPath, 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, apiContextPath + "/" + suffix);
    }
}