ContentPolicyStatus.java 6.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.components.ComponentContext
 *  com.day.cq.wcm.api.policies.ContentPolicyManager
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.SyntheticResource
 *  org.apache.sling.api.resource.ValueMap
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.day.cq.wcm.core.impl.TemplateUtils;
import com.day.cq.wcm.core.impl.policies.ContentPolicyManagerImpl;
import javax.servlet.http.HttpServletRequest;
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.SyntheticResource;
import org.apache.sling.api.resource.ValueMap;

public class ContentPolicyStatus {
    public final boolean usePolicy;
    public final boolean isPageUsingAuthoredTemplate;
    public final String policyDirectoryPath;
    public final String policyMappingPath;
    public final String policyPath;
    public final String policyNodeName;
    public final boolean hasPolicyMapping;
    public final boolean isAllowedComponent;
    private String resourceType;
    public String allowedComponentDesignDialogPath;

    private ContentPolicyStatus(HttpServletRequest request, ComponentContext context, boolean usePolicy, boolean isPageUsingAuthoredTemplate, String policyMappingPath, String policyPath, String policyDirectoryPath, String policyNodeName, boolean hasPolicyMapping, boolean isAllowedComponent) {
        this.usePolicy = usePolicy;
        this.isPageUsingAuthoredTemplate = isPageUsingAuthoredTemplate;
        this.policyMappingPath = policyMappingPath;
        this.policyPath = policyPath;
        this.policyDirectoryPath = policyDirectoryPath;
        this.policyNodeName = policyNodeName;
        this.hasPolicyMapping = hasPolicyMapping;
        this.isAllowedComponent = isAllowedComponent;
        Resource resource = context.getResource();
        ValueMap resourceVM = resource.getValueMap();
        if (resourceVM != null && resourceVM.containsKey((Object)"sling:resourceType")) {
            this.resourceType = (String)resourceVM.get("sling:resourceType", String.class);
        }
        if (this.isAllowedComponent) {
            ResourceResolver resolver = resource.getResourceResolver();
            String resourcePath = context.getResource().getPath();
            if (!StringUtils.isEmpty((CharSequence)resourcePath)) {
                Component component;
                Resource dialog;
                Resource targetResource = (Resource)request.getAttribute(resourcePath);
                this.resourceType = TemplateUtils.getRelativePath(targetResource);
                if (targetResource != null && (dialog = TemplateUtils.getDialog(resolver, component = (Component)targetResource.adaptTo(Component.class), "design_dialog")) != null) {
                    this.allowedComponentDesignDialogPath = dialog.getPath();
                }
            }
        }
    }

    public static ContentPolicyStatus buildContentPolicyStatus(HttpServletRequest request, ComponentContext componentContext) {
        Page page = componentContext.getPage();
        boolean usePolicy = false;
        boolean hasPolicyMapping = false;
        boolean isAllowedComponent = false;
        boolean isPageUsingAuthoredTemplate = TemplateUtils.isPageAuthoredTemplated(page) && !TemplateUtils.isTemplateStructureComponent(componentContext);
        boolean isContextAuthoredTemplate = TemplateUtils.isPageOfAuthoredTemplate(page) || TemplateUtils.isTemplateStructureComponent(componentContext);
        String policyMappingPath = null;
        String policyDirectoryPath = null;
        String policyPath = null;
        String policyNodeName = null;
        if (isPageUsingAuthoredTemplate || isContextAuthoredTemplate) {
            Resource policyResource;
            Resource resource = componentContext.getResource();
            usePolicy = true;
            ContentPolicyManagerImpl contentPolicyManager = (ContentPolicyManagerImpl)resource.getResourceResolver().adaptTo(ContentPolicyManager.class);
            policyMappingPath = contentPolicyManager.buildPolicyMappingPath(componentContext.getResource());
            hasPolicyMapping = TemplateUtils.hasPolicyMapping(resource.getResourceResolver(), policyMappingPath);
            policyDirectoryPath = ContentPolicyManagerImpl.getPolicyLocation(resource);
            if (resource instanceof SyntheticResource) {
                Resource parent = null;
                String parentPath = resource.getPath();
                ResourceResolver resolver = resource.getResourceResolver();
                while (!StringUtils.isEmpty((CharSequence)parentPath) && parentPath.indexOf("/") > -1 && parent == null) {
                    parentPath = parentPath.substring(0, parentPath.lastIndexOf("/"));
                    parent = resolver.getResource(parentPath);
                }
                if (parent != null) {
                    ValueMap parentValueMap = parent.getValueMap();
                    boolean bl = isAllowedComponent = parentValueMap.containsKey((Object)"editable") && (Boolean)parentValueMap.get("editable", Boolean.class) != false;
                }
            }
            if ((policyResource = TemplateUtils.getPolicyResource(resource.getResourceResolver(), policyMappingPath)) != null) {
                policyPath = policyResource.getPath();
                policyNodeName = policyResource.getName();
            }
        }
        return new ContentPolicyStatus(request, componentContext, usePolicy, isPageUsingAuthoredTemplate, policyMappingPath, policyPath, policyDirectoryPath, policyNodeName, hasPolicyMapping, isAllowedComponent);
    }

    public String getResourceType() {
        return this.resourceType;
    }
}