DynamicMediaConfig.java 5.73 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.AgentConfig
 *  com.scene7.is.util.callbacks.Option
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  scala.Tuple3
 */
package com.adobe.cq.dam.s7imaging.config;

import com.adobe.cq.dam.aod.replication.DamLayout;
import com.adobe.cq.dam.s7imaging.impl.catalog.JcrUtil;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropExtractor;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.PropKey;
import com.adobe.cq.dam.s7imaging.impl.jcr.props.StandardExtractors;
import com.day.cq.replication.AgentConfig;
import com.scene7.is.util.callbacks.Option;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import scala.Tuple3;

public class DynamicMediaConfig {
    private static final Pattern validTenantIdChars = Pattern.compile("[\\w\\s.\\-]+");
    private static final PropKey<String> CompanyName = PropKey.required("companyName", StandardExtractors.toString);
    private static final String DefaultMacroPath = "/etc/dam/imageserver/macros";
    private static final String TenantMacroSuffix = "presets/image";
    private static final String TenantCatalogSuffix = "imageserver/configuration/jcr:content/settings";

    public static Option<String> defaultCompanyName(AgentConfig config) {
        return Option.some((Object)((String)config.getProperties().get((Object)"companyName")));
    }

    public static String tenantId(ResourceResolver resolver, String path) {
        assert (path.startsWith("/"));
        assert (path.equals("/") || !path.endsWith("/"));
        Iterator i$ = DamLayout.TenantDomainPath.unapply(path).iterator();
        if (i$.hasNext()) {
            Tuple3 v = (Tuple3)i$.next();
            return (String)v._1();
        }
        return "";
    }

    public static Option<Resource> imagePresets(Resource catalogSettings) {
        String path = catalogSettings.getPath();
        Iterator i$ = DamLayout.CatalogPath.unapply(path).iterator();
        if (i$.hasNext()) {
            String tenantId = (String)i$.next();
            return JcrUtil.getResource(catalogSettings.getResourceResolver(), DamLayout.imagePresetRoot(tenantId));
        }
        throw new AssertionError((Object)("Invalid catalog settings path: '" + path + '\''));
    }

    public static Option<Resource> catalog(ResourceResolver resolver, String _rootId) throws RepositoryException {
        String rootId = DynamicMediaConfig.fixNetPath(_rootId);
        assert (!rootId.startsWith("/"));
        assert (!rootId.endsWith("/"));
        Iterator i$ = DamLayout.TenantDomainPath.unapply("" + '/' + rootId).iterator();
        if (i$.hasNext()) {
            Tuple3 v = (Tuple3)i$.next();
            String tenant = (String)v._1();
            Iterator i$2 = ((Option)v._3()).iterator();
            if (i$2.hasNext()) {
                String suffix = (String)i$2.next();
                assert (!suffix.isEmpty());
                return Option.none();
            }
            return Option.some((Object)DynamicMediaConfig.tenantCatalog(resolver, tenant));
        }
        if (rootId.isEmpty()) {
            return JcrUtil.getResource(resolver, DynamicMediaConfig.catalogPath(""));
        }
        return Option.none();
    }

    public static List<String> tenantRootIds(String tenantId) {
        assert (!tenantId.isEmpty());
        return Arrays.asList(DamLayout.contentRoot(tenantId).substring(1), DamLayout.viewerPresetRoot(tenantId).substring(1));
    }

    public static String imagePresetPath(String tenantId) {
        if (tenantId.isEmpty()) {
            return "/etc/dam/imageserver/macros";
        }
        return "/etc/dam/tenants/" + tenantId + '/' + "presets/image";
    }

    private static String fixNetPath(String path) {
        return path.replaceAll(" / +$ ", " ");
    }

    private static Resource tenantCatalog(ResourceResolver resolver, String tenantId) throws RepositoryException {
        String path = DynamicMediaConfig.catalogPath(tenantId);
        Iterator i$ = JcrUtil.getResource(resolver, path).iterator();
        if (i$.hasNext()) {
            Resource resource = (Resource)i$.next();
            return resource;
        }
        String m = "Catalog for '" + tenantId + "' does not exist. Publish catalog settings at '" + path + "'";
        throw new RepositoryException(m);
    }

    private static String catalogPath(String tenantId) {
        if (tenantId.isEmpty()) {
            return "/etc/dam/imageserver/configuration/jcr:content/settings";
        }
        return "/etc/dam/tenants/" + tenantId + '/' + "imageserver/configuration/jcr:content/settings";
    }

    private static void validateTenantId(String id) {
        if (id.equals("..")) {
            DynamicMediaConfig.illegalId(id, "must not be '..'");
        }
        if (id.startsWith(" ")) {
            DynamicMediaConfig.illegalId(id, "must not start with white space");
        }
        if (id.endsWith(" ")) {
            DynamicMediaConfig.illegalId(id, "must not end with white space");
        }
        if (!validTenantIdChars.matcher(id).matches()) {
            DynamicMediaConfig.illegalId(id, "can only contain numbers, letters, white space, '.', '-' and '_'");
        }
    }

    private static Option<String> illegalId(String id, String message) {
        throw new IllegalArgumentException("Illegal tenant ID (" + message + ") but was: '" + id + "'");
    }

    private DynamicMediaConfig() {
    }
}