TemplateUtils.java 15.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.Template
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.components.ComponentContext
 *  com.day.cq.wcm.api.policies.ContentPolicy
 *  com.day.cq.wcm.api.policies.ContentPolicyMapping
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyMapping;
import com.day.cq.wcm.core.impl.TemplateConfImpl;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TemplateUtils {
    private static final Logger log = LoggerFactory.getLogger(TemplateUtils.class);
    private static final String MAPPING_RESOURCE_TYPE = "wcm/core/components/policies/mapping";
    private static final String JCR_CONTENT_PATH_CHUNK = "/jcr:content/";
    public static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
    public static final String PN_CQ_POLICY = "cq:policy";

    protected static Resource getDialog(ResourceResolver resolver, Component c, String dialogName) {
        Component sc;
        Resource resource = (Resource)c.adaptTo(Resource.class);
        Resource dialog = resolver.getResource(resource, "cq:" + dialogName);
        if (dialog == null && (dialog = resolver.getResource(resource, dialogName)) == null && (sc = c.getSuperComponent()) != null) {
            dialog = TemplateUtils.getDialog(resolver, sc, dialogName);
        }
        return dialog;
    }

    public static boolean isAuthoredTemplate(Resource resource) {
        boolean isAuthored = false;
        if (resource != null) {
            isAuthored = resource.getChild("initial") != null && resource.getChild("structure") != null && resource.getChild("policies") != null;
        }
        return isAuthored;
    }

    public static Template getTemplate(ComponentContext componentContext) {
        if (componentContext == null) {
            return null;
        }
        Page page = componentContext.getPage();
        return page != null ? page.getTemplate() : null;
    }

    public static boolean isPageAuthoredTemplated(Page page) {
        if (page != null) {
            Template template = page.getTemplate();
            return template != null && template.hasStructureSupport();
        }
        return false;
    }

    public static boolean isPageOfAuthoredTemplate(Page page) {
        return TemplateUtils.isPageOfAuthoredTemplate((Resource)page.adaptTo(Resource.class));
    }

    public static boolean isPageOfAuthoredTemplate(Resource resource) {
        if (resource == null) {
            return false;
        }
        boolean isNamedAccordingly = "structure".equals(resource.getName()) || "initial".equals(resource.getName());
        boolean isOfPageResourceType = "cq:Page".equals(resource.getValueMap().get((Object)"jcr:primaryType"));
        Resource parent = resource.getParent();
        boolean isParentOfTemplateResourceType = false;
        boolean isParentAuthoredTempalte = false;
        if (parent != null) {
            isParentOfTemplateResourceType = "cq:Template".equals(resource.getParent().getValueMap().get((Object)"jcr:primaryType"));
            isParentAuthoredTempalte = TemplateUtils.isAuthoredTemplate(resource.getParent());
        }
        return isNamedAccordingly && isOfPageResourceType && isParentOfTemplateResourceType && isParentAuthoredTempalte;
    }

    public static boolean isTemplateStructureComponent(ComponentContext componentContext) {
        return TemplateUtils.isTemplateStructureResource(componentContext.getResource());
    }

    public static boolean isTemplateStructureResource(Resource resource) {
        Resource structureResource = TemplateUtils.getStructureResource(resource);
        if (structureResource != null && ResourceUtil.isStarResource((Resource)structureResource)) {
            structureResource = structureResource.getParent();
        }
        return structureResource != null;
    }

    public static boolean isTemplateStructureComponentLocked(ComponentContext componentContext) {
        return TemplateUtils.isTemplateStructureResourceLocked(TemplateUtils.getStructureResource(componentContext.getResource()));
    }

    public static boolean isTemplateStructureResourceLocked(Resource structureResource) {
        boolean locked = false;
        if (structureResource != null) {
            ValueMap properties;
            if (ResourceUtil.isStarResource((Resource)structureResource)) {
                structureResource = structureResource.getParent();
            }
            if (structureResource != null && (properties = (ValueMap)structureResource.adaptTo(ValueMap.class)) != null) {
                Boolean editable = (Boolean)properties.get("editable", Boolean.class);
                locked = editable == null || editable == false;
            }
        }
        return locked;
    }

    private static boolean isPageAuthoredTemplateOfName(Page page, String name) {
        if (StringUtils.isEmpty((CharSequence)name)) {
            return false;
        }
        boolean isTemplateStructurePage = false;
        Template template = page.getTemplate();
        if (page != null && template != null && template.hasStructureSupport() && name.equals(page.getName())) {
            isTemplateStructurePage = true;
        }
        return isTemplateStructurePage;
    }

    public static boolean isTemplateInitialPage(ComponentContext componentContext) {
        return componentContext != null && TemplateUtils.isPageAuthoredTemplateOfName(componentContext.getPage(), "initial");
    }

    public static boolean isTemplateInitialPage(Page page) {
        return TemplateUtils.isPageAuthoredTemplateOfName(page, "initial");
    }

    public static boolean isTemplateStructurePage(ComponentContext componentContext) {
        return componentContext != null && TemplateUtils.isPageAuthoredTemplateOfName(componentContext.getPage(), "structure");
    }

    public static boolean isTemplateStructurePage(Page page) {
        return TemplateUtils.isPageAuthoredTemplateOfName(page, "structure");
    }

    public static String getComponentRelativePath(Resource resource) {
        if (resource == null) {
            return null;
        }
        ResourceResolver resourceResolver = resource.getResourceResolver();
        PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
        if (pageManager == null) {
            return null;
        }
        Page page = pageManager.getContainingPage(resource);
        if (page == null) {
            return null;
        }
        String resourcePath = resource.getPath();
        if (resourcePath.equals(page.getPath())) {
            return resourcePath;
        }
        resourcePath = (resourcePath = resourcePath.substring(page.getPath().length(), resourcePath.length())).startsWith("/") && resourcePath.length() > 1 ? resourcePath.substring(1, resourcePath.length()) : resourcePath;
        return resourcePath;
    }

    public static String getPageContentRelativePath(Resource resource) {
        if (resource == null) {
            return null;
        }
        PageManager pageManager = (PageManager)resource.getResourceResolver().adaptTo(PageManager.class);
        Page page = pageManager.getContainingPage(resource);
        if (page != null) {
            if (resource.getPath().equals(page.getContentResource().getPath())) {
                return "";
            }
            return resource.getPath().replace(page.getContentResource().getPath() + "/", "");
        }
        return null;
    }

    private static Resource getTemplatePageRoot(Template template, String childName) {
        Resource templateResource;
        Resource templatePageRoot = null;
        if (template != null && (templateResource = (Resource)template.adaptTo(Resource.class)) != null) {
            templatePageRoot = templateResource.getChild(childName);
        }
        return templatePageRoot;
    }

    public static Resource getStructureResource(Resource resource) {
        return TemplateUtils.getAspectResource(resource, "structure");
    }

    private static Resource getAspectResource(Resource resource, String aspectName) {
        Resource templateStructureRoot;
        Resource structureResource = null;
        ResourceResolver resourceResolver = resource.getResourceResolver();
        PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
        if (pageManager == null) {
            return null;
        }
        Page page = pageManager.getContainingPage(resource);
        if (page == null) {
            return null;
        }
        Template template = page.getTemplate();
        if (template != null && template.hasStructureSupport() && (templateStructureRoot = TemplateUtils.getTemplatePageRoot(template, aspectName)) != null) {
            if (resource.getPath().startsWith(templateStructureRoot.getPath())) {
                return resource;
            }
            String componentRelativePath = TemplateUtils.getComponentRelativePath(resource);
            if (StringUtils.isNotBlank((CharSequence)componentRelativePath)) {
                structureResource = templateStructureRoot.getChild(componentRelativePath);
            }
        }
        return structureResource;
    }

    public static void removePropertyRecursive(Node node, String propertyName) throws RepositoryException {
        if (node != null && node.hasNodes()) {
            NodeIterator iter = node.getNodes();
            while (iter.hasNext()) {
                Node child = iter.nextNode();
                String childName = child.getName();
                if ("cq:responsive".equals(childName)) continue;
                if (!"jcr:content".equals(childName)) {
                    child.setProperty(propertyName, (String[])null);
                }
                TemplateUtils.removePropertyRecursive(child, propertyName);
            }
        }
    }

    public static Resource getCorrespondingStructureResource(Template template, Resource resource) {
        Resource templateStructureRoot;
        if (resource != null && template != null && template.hasStructureSupport() && (templateStructureRoot = TemplateUtils.getTemplatePageRoot(template, "structure")) != null) {
            if (resource.getPath().startsWith(templateStructureRoot.getPath())) {
                return resource;
            }
            String resourceRelativePath = TemplateUtils.getPageContentRelativePath(resource);
            if (StringUtils.isNotEmpty((CharSequence)resourceRelativePath)) {
                return templateStructureRoot.getChild("jcr:content/" + resourceRelativePath);
            }
        }
        return null;
    }

    public static String getPageContentRelativePath(String resourcePath) {
        int jcrContentLength = "/jcr:content/".length();
        int jcrIndex = resourcePath.indexOf("/jcr:content/");
        return jcrIndex > -1 && jcrIndex + jcrContentLength < resourcePath.length() ? resourcePath.substring(jcrIndex + jcrContentLength, resourcePath.length()) : "";
    }

    public static Resource getPolicyResource(ResourceResolver resolver, String policyMappingResourcePath) {
        ContentPolicyMapping contentPolicyMapping;
        ContentPolicy policy;
        Resource policyMappingResource = resolver.getResource(policyMappingResourcePath);
        if (policyMappingResource != null && (contentPolicyMapping = (ContentPolicyMapping)policyMappingResource.adaptTo(ContentPolicyMapping.class)) != null && (policy = contentPolicyMapping.getPolicy()) != null) {
            return (Resource)policy.adaptTo(Resource.class);
        }
        return null;
    }

    public static boolean hasPolicyMapping(ResourceResolver resolver, String policyMappingResourcePath) {
        return TemplateUtils.getPolicyResource(resolver, policyMappingResourcePath) != null;
    }

    public static String getRelativePath(Resource resource) {
        if (resource == null) {
            return null;
        }
        return TemplateUtils.getRelativePath(resource.getResourceResolver(), resource.getPath());
    }

    public static String getRelativePath(ResourceResolver resolver, String path) {
        if (StringUtils.isEmpty((CharSequence)path)) {
            return null;
        }
        String[] searchPaths = resolver.getSearchPath();
        for (int i = 0; i < searchPaths.length; ++i) {
            String searchPath = searchPaths[i];
            if (!path.startsWith(searchPath)) continue;
            return path.substring(searchPath.length(), path.length());
        }
        return path;
    }

    public static String getPolicyDirectoryPath(ComponentContext componentContext) {
        return TemplateUtils.getPolicyDirectoryPath(componentContext.getResource());
    }

    public static String getPolicyDirectoryPath(Resource resource) {
        TemplateConfImpl conf;
        Resource policyDirectory;
        Resource templateResource;
        ResourceResolver resourceResolver = resource.getResourceResolver();
        PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
        if (pageManager == null) {
            return null;
        }
        Page page = pageManager.getContainingPage(resource);
        if (page == null) {
            return null;
        }
        Template template = page.getTemplate();
        if (template != null && template.hasStructureSupport() && (templateResource = (Resource)template.adaptTo(Resource.class)) != null && (conf = new TemplateConfImpl(templateResource)) != null && (policyDirectory = conf.getItemResource("wcm/policies")) != null) {
            return policyDirectory.getPath();
        }
        return null;
    }

    public static boolean isAuthoredTemplateRestricted(ComponentContext componentContext) {
        Page page = componentContext.getPage();
        Template template = TemplateUtils.getTemplate(componentContext);
        String resourcePath = componentContext.getResource().getPath();
        if (template != null && TemplateUtils.isAuthoredTemplate((Resource)template.adaptTo(Resource.class))) {
            String structureResourcePath = resourcePath.indexOf("/structure/jcr:content/") > -1 || resourcePath.indexOf("/structure/_jcr_content/") > -1 ? resourcePath : template.getPath() + "/" + "structure" + resourcePath.replace(page.getPath(), "");
            structureResourcePath = structureResourcePath.replace("/*", "");
            Resource structureResource = componentContext.getResource().getResourceResolver().getResource(structureResourcePath);
            if (structureResource != null) {
                Boolean editable = (Boolean)structureResource.getValueMap().get("editable", Boolean.class);
                if (!TemplateUtils.isPageOfAuthoredTemplate(page) && (resourcePath.startsWith(template.getPath()) || structureResource != null && editable == null || !editable.booleanValue())) {
                    return true;
                }
            }
        }
        return false;
    }
}