WCMBindingsValuesProvider.java 10.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.components.ComponentContext
 *  com.day.cq.wcm.api.components.EditContext
 *  com.day.cq.wcm.api.designer.Cell
 *  com.day.cq.wcm.api.designer.Design
 *  com.day.cq.wcm.api.designer.Designer
 *  com.day.cq.wcm.api.designer.Style
 *  com.day.cq.wcm.api.policies.ContentPolicy
 *  com.day.cq.wcm.api.policies.ContentPolicyManager
 *  com.day.cq.wcm.commons.WCMUtils
 *  javax.servlet.ServletRequest
 *  org.apache.sling.api.SlingHttpServletRequest
 *  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.apache.sling.api.scripting.SlingScriptHelper
 *  org.apache.sling.scripting.api.BindingsValuesProvider
 */
package com.day.cq.wcm.scripting.impl;

import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.components.Component;
import com.day.cq.wcm.api.components.ComponentContext;
import com.day.cq.wcm.api.components.EditContext;
import com.day.cq.wcm.api.designer.Cell;
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.api.policies.ContentPolicy;
import com.day.cq.wcm.api.policies.ContentPolicyManager;
import com.day.cq.wcm.commons.WCMUtils;
import com.day.cq.wcm.scripting.impl.ContentPolicyStyle;
import java.util.HashMap;
import javax.script.Bindings;
import javax.servlet.ServletRequest;
import org.apache.sling.api.SlingHttpServletRequest;
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.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.scripting.api.BindingsValuesProvider;

public class WCMBindingsValuesProvider
implements BindingsValuesProvider {
    public static final String NAME_COMPONENT_CONTEXT = "componentContext";
    public static final String NAME_EDIT_CONTEXT = "editContext";
    public static final String NAME_PROPERTIES = "properties";
    public static final String NAME_PAGE_MANAGER = "pageManager";
    public static final String NAME_CURRENT_PAGE = "currentPage";
    public static final String NAME_RESOURCE_PAGE = "resourcePage";
    public static final String NAME_PAGE_PROPERTIES = "pageProperties";
    public static final String NAME_COMPONENT = "component";
    public static final String NAME_DESIGNER = "designer";
    public static final String NAME_CURRENT_DESIGN = "currentDesign";
    public static final String NAME_RESOURCE_DESIGN = "resourceDesign";
    public static final String NAME_CURRENT_STYLE = "currentStyle";
    public static final String NAME_XSSAPI = "xssAPI";
    public static final String NAME_CURRENT_CONTENT_POLICY = "currentContentPolicy";
    public static final String NAME_CURRENT_CONTENT_POLICY_PROPS = "currentContentPolicyProperties";
    private static final String REQ_ATTR_PREFIX = WCMBindingsValuesProvider.class.getName() + ":design:";
    private static final String REQ_ATTR_PAGE_CACHE = WCMBindingsValuesProvider.class.getName() + ":pageCache";
    private XSSAPI xssAPI;

    WCMBindingsValuesProvider(XSSAPI xssAPI) {
        this.xssAPI = xssAPI;
    }

    public void addBindings(Bindings bindings) {
        ContentPolicyManager policyManager;
        SlingHttpServletRequest request = (SlingHttpServletRequest)bindings.get("request");
        SlingScriptHelper sling = (SlingScriptHelper)bindings.get("sling");
        if (request == null || sling == null) {
            return;
        }
        Resource resource = request.getResource();
        ResourceResolver resolver = request.getResourceResolver();
        if (resolver == null) {
            return;
        }
        XSSAPI requestSpecificXssAPI = this.xssAPI.getRequestSpecificAPI(request);
        ComponentContext componentContext = WCMUtils.getComponentContext((ServletRequest)request);
        EditContext editContext = componentContext == null ? null : componentContext.getEditContext();
        ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
        PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
        HierarchyNodeInheritanceValueMap pageProperties = null;
        Page currentPage = null;
        Page resourcePage = null;
        if (pageManager != null) {
            resourcePage = WCMBindingsValuesProvider.getContainingPage(pageManager, resource, request);
            Page page = currentPage = componentContext == null ? null : componentContext.getPage();
            if (currentPage == null) {
                currentPage = resourcePage;
            }
            pageProperties = currentPage == null ? null : new HierarchyNodeInheritanceValueMap(currentPage.getContentResource());
        }
        Component component = WCMUtils.getComponent((Resource)resource);
        Designer designer = (Designer)request.getResourceResolver().adaptTo(Designer.class);
        if (designer != null) {
            Design resourceDesign;
            Design currentDesign;
            if (currentPage == null) {
                currentDesign = null;
            } else {
                String currentDesignKey = REQ_ATTR_PREFIX + currentPage.getPath();
                Object cachedCurrentDesign = request.getAttribute(currentDesignKey);
                if (cachedCurrentDesign != null) {
                    currentDesign = (Design)cachedCurrentDesign;
                } else {
                    currentDesign = designer.getDesign(currentPage);
                    request.setAttribute(currentDesignKey, (Object)currentDesign);
                }
            }
            if (resourcePage == null) {
                resourceDesign = null;
            } else {
                String resourceDesignkey = REQ_ATTR_PREFIX + resourcePage.getPath();
                Object cachedresourceDesign = request.getAttribute(resourceDesignkey);
                if (cachedresourceDesign != null) {
                    resourceDesign = (Design)cachedresourceDesign;
                } else {
                    resourceDesign = designer.getDesign(resourcePage);
                    request.setAttribute(resourceDesignkey, (Object)resourceDesign);
                }
            }
            Style currentStyle = currentDesign == null || componentContext == null ? null : currentDesign.getStyle(componentContext.getCell());
            bindings.put("designer", (Object)designer);
            if (currentDesign != null) {
                bindings.put("currentDesign", (Object)currentDesign);
            }
            if (resourceDesign != null) {
                bindings.put("resourceDesign", (Object)resourceDesign);
            }
            if (currentStyle != null) {
                bindings.put("currentStyle", (Object)currentStyle);
            }
        }
        ContentPolicy currentPolicy = null;
        ValueMap currentPolicyProperties = null;
        ContentPolicyStyle contentPolicyStyle = null;
        if (componentContext != null && (policyManager = (ContentPolicyManager)resolver.adaptTo(ContentPolicyManager.class)) != null && (currentPolicy = policyManager.getPolicy(componentContext)) != null) {
            currentPolicyProperties = currentPolicy.getProperties();
            contentPolicyStyle = new ContentPolicyStyle(currentPolicy, componentContext.getCell());
        }
        if (requestSpecificXssAPI != null) {
            bindings.put("xssAPI", (Object)requestSpecificXssAPI);
        }
        if (componentContext != null) {
            bindings.put("componentContext", (Object)componentContext);
        }
        if (editContext != null) {
            bindings.put("editContext", (Object)editContext);
        }
        if (properties != null) {
            bindings.put("properties", (Object)properties);
        }
        if (pageManager != null) {
            bindings.put("pageManager", (Object)pageManager);
        }
        if (currentPage != null) {
            bindings.put("currentPage", (Object)currentPage);
        }
        if (resourcePage != null) {
            bindings.put("resourcePage", (Object)resourcePage);
        }
        if (pageProperties != null) {
            bindings.put("pageProperties", (Object)pageProperties);
        }
        if (component != null) {
            bindings.put("component", (Object)component);
        }
        if (currentPolicy != null) {
            bindings.put("currentContentPolicy", (Object)currentPolicy);
        }
        if (currentPolicyProperties != null) {
            bindings.put("currentContentPolicyProperties", (Object)currentPolicyProperties);
        }
        if (contentPolicyStyle != null) {
            bindings.put("currentStyle", (Object)contentPolicyStyle);
        }
    }

    private static Page getContainingPage(PageManager pageManager, Resource resource, SlingHttpServletRequest request) {
        Page page;
        String cacheKey = WCMBindingsValuesProvider.getContainingPageCacheKey(resource);
        if (cacheKey == null) {
            page = null;
        } else {
            HashMap<String, Page> pageCache = (HashMap<String, Page>)request.getAttribute(REQ_ATTR_PAGE_CACHE);
            if (pageCache == null) {
                pageCache = new HashMap<String, Page>();
                request.setAttribute(REQ_ATTR_PAGE_CACHE, pageCache);
            }
            if ((page = (Page)pageCache.get(cacheKey)) == null && !pageCache.containsKey(cacheKey)) {
                page = pageManager.getContainingPage(resource);
                pageCache.put(cacheKey, page);
            }
        }
        return page;
    }

    static String getContainingPageCacheKey(Resource resource) {
        String path;
        int idx;
        String string = path = resource != null ? resource.getPath() : null;
        if (path != null && (idx = path.indexOf("/jcr:content")) >= 0) {
            path = path.substring(0, idx);
        }
        return path;
    }
}