TemplateImpl.java 14.2 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.LabeledResource
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.Template
 *  com.day.cq.wcm.commons.Constants
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.adapter.SlingAdaptable
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.commons.LabeledResource;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.Template;
import com.day.cq.wcm.commons.Constants;
import com.day.cq.wcm.core.impl.TemplateUtils;
import com.day.text.Text;
import java.util.regex.Pattern;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.adapter.SlingAdaptable;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TemplateImpl
extends SlingAdaptable
implements Template {
    public static final String NN_INITIAL = "initial";
    public static final String NN_STRUCTURE = "structure";
    public static final String NN_POLICIES = "policies";
    public static final String NT_EDITABLE = "editable";
    public static final String PN_STATUS = "status";
    public static final String PN_STATUS_VALUE_ENABLED = "enabled";
    private static final Logger log = LoggerFactory.getLogger(TemplateImpl.class);
    public static final String RESOURCE_TYPE = "cq:Template";
    private final Resource resource;
    private final Node node;
    private ValueMap cachedProperties;

    public TemplateImpl(Resource resource) {
        this.resource = resource;
        this.node = (Node)resource.adaptTo(Node.class);
        if (this.node == null) {
            throw new IllegalArgumentException("Resource argument to must be adaptable to Node");
        }
    }

    public String getPath() {
        return this.resource.getPath();
    }

    public String getName() {
        try {
            return this.node.getName();
        }
        catch (RepositoryException e) {
            return "";
        }
    }

    public String getTitle() {
        return (String)this.getProperties().get("jcr:title", String.class);
    }

    public String getShortTitle() {
        return (String)this.getProperties().get("shortTitle", String.class);
    }

    public String getDescription() {
        return (String)this.getProperties().get("jcr:description", String.class);
    }

    public String getIconPath() {
        try {
            if (this.node.hasNode("icon.png")) {
                return this.getPath() + "/" + "icon.png";
            }
        }
        catch (RepositoryException e) {
            // empty catch block
        }
        return null;
    }

    public String getThumbnailPath() {
        try {
            if (this.node.hasNode("thumbnail.png")) {
                return this.getPath() + "/" + "thumbnail.png";
            }
        }
        catch (RepositoryException e) {
            // empty catch block
        }
        return null;
    }

    public Long getRanking() {
        return (Long)this.getProperties().get("ranking", Long.class);
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (type == Resource.class) {
            return (AdapterType)this.resource;
        }
        if (type == LabeledResource.class) {
            return (AdapterType)((Object)this);
        }
        if (type == Node.class) {
            return (AdapterType)this.node;
        }
        return (AdapterType)super.adaptTo(type);
    }

    public boolean isAllowed(String parentPath) {
        if (parentPath == null) {
            return true;
        }
        Resource parentRes = this.resource.getResourceResolver().getResource(parentPath);
        return this.isAllowed(parentRes);
    }

    public boolean isAllowed(Resource parentRes) {
        String status;
        String[] allowedPaths;
        ValueMap templateProperties;
        String parentPath;
        String string = parentPath = parentRes != null ? parentRes.getPath() : null;
        if (log.isDebugEnabled()) {
            log.debug(".");
            log.debug("checking isAllowed for {} under {} ...", (Object)this.getPath(), (Object)parentPath);
        }
        if (!StringUtils.isEmpty((String)(status = (String)(templateProperties = this.getProperties()).get("status", String.class))) && !"enabled".equals(status)) {
            log.debug("not allowed because {} is not enabled", (Object)this.getPath());
            return false;
        }
        String[] allowedTemplates = Constants.EMPTY_STRING_ARRAY;
        String appName = "";
        String pagePath = null;
        if (parentRes != null) {
            Resource pageResource = parentRes;
            try {
                int depth;
                while (allowedTemplates.length == 0 && pageResource != null && (depth = ((Node)pageResource.adaptTo(Node.class)).getDepth()) != 0) {
                    Resource content;
                    if (depth == 2) {
                        appName = pageResource.getName();
                    }
                    if ((content = pageResource.getChild("jcr:content")) != null) {
                        allowedTemplates = (String[])content.getValueMap().get("cq:allowedTemplates", (Object)Constants.EMPTY_STRING_ARRAY);
                        pagePath = pageResource.getPath() + "/" + "jcr:content";
                    }
                    if (allowedTemplates.length == 0) {
                        allowedTemplates = (String[])pageResource.getValueMap().get("cq:allowedTemplates", (Object)Constants.EMPTY_STRING_ARRAY);
                    }
                    if (allowedTemplates.length != 0) continue;
                    pageResource = pageResource.getParent();
                }
            }
            catch (RepositoryException e) {
                log.warn("error during evaluation of allowed templates: " + (Object)e);
            }
            if (allowedTemplates.length > 0) {
                String path = this.getPath();
                boolean allowed = false;
                for (int i = 0; !allowed && i < allowedTemplates.length; ++i) {
                    try {
                        if (!Pattern.matches(allowedTemplates[i], path)) continue;
                        allowed = true;
                        continue;
                    }
                    catch (Exception e) {
                        log.error("Error while checking if " + path + " matches " + allowedTemplates[i], (Throwable)e);
                    }
                }
                if (!allowed) {
                    log.debug("not allowed because not included in {}/{}", (Object)pagePath, (Object)"cq:allowedTemplates");
                    return false;
                }
            }
        }
        if ((allowedPaths = (String[])templateProperties.get("allowedPaths", (Object)Constants.EMPTY_STRING_ARRAY)).length > 0) {
            boolean allowed = false;
            for (int i = 0; !allowed && i < allowedPaths.length; ++i) {
                try {
                    if (!Pattern.matches(allowedPaths[i], parentPath)) continue;
                    allowed = true;
                    continue;
                }
                catch (Exception e) {
                    log.error("Error while checking if " + parentPath + " matches " + allowedPaths[i] + " on " + this.getPath(), (Throwable)e);
                }
            }
            if (!allowed) {
                log.debug("not allowed because parent path not included in {}/{} (deprecated)", (Object)this.getPath(), (Object)"allowedPaths");
                return false;
            }
        }
        if (allowedPaths.length == 0 && allowedTemplates.length == 0 && !this.getPath().startsWith("/apps/" + appName + "/")) {
            log.debug("not allowed because not under /apps/{} (2nd level from {})", (Object)appName, (Object)parentPath);
            return false;
        }
        Page parentPage = parentRes == null ? null : (Page)parentRes.adaptTo(Page.class);
        Template parentTemplate = parentPage == null ? null : parentPage.getTemplate();
        String[] allowedParents = (String[])this.getProperties().get("allowedParents", (Object)Constants.EMPTY_STRING_ARRAY);
        if (parentTemplate == null) {
            if (log.isDebugEnabled()) {
                if (allowedParents.length == 0) {
                    log.debug("ALLOWED because no parent template found");
                } else {
                    log.debug("not allowed because no parent template found, but {}/{} is set", (Object)this.getPath(), (Object)"allowedParents");
                }
            }
            return allowedParents.length == 0;
        }
        if (allowedParents.length > 0 && !this.matches(allowedParents, parentTemplate.getPath())) {
            log.debug("not allowed because the parent template is not included in {}/{}", (Object)this.getPath(), (Object)"allowedParents");
            return false;
        }
        String[] allowedChildren = this.getAllowedChildren(parentTemplate);
        if (allowedChildren.length > 0 && !this.matches(allowedChildren, this.getPath())) {
            log.debug("not allowed because this template is not included in {}/{}", (Object)parentTemplate.getPath(), (Object)"allowedChildren");
            return false;
        }
        if (log.isDebugEnabled()) {
            if (allowedParents.length == 0 && allowedChildren.length == 0) {
                log.debug("ALLOWED (no {} or {} defined)", (Object)"allowedParents", (Object)"allowedChildren");
            } else if (allowedChildren.length > 0) {
                log.debug("ALLOWED because this template is included in {}/{}", (Object)parentTemplate.getPath(), (Object)"allowedChildren");
            } else {
                log.debug("ALLOWED because the parent template is included in {}/{}", (Object)this.getPath(), (Object)"allowedParents");
            }
        }
        return true;
    }

    private boolean matches(String[] allowed, String path) {
        for (String p : allowed) {
            try {
                if (!Pattern.matches(p, path)) continue;
                return true;
            }
            catch (Exception e) {
                log.error("Error while checking if " + path + " matches " + p + " on " + this.getPath(), (Throwable)e);
            }
        }
        return false;
    }

    private String[] getAllowedChildren(Template template) {
        Resource parentTplRes = (Resource)template.adaptTo(Resource.class);
        if (parentTplRes == null) {
            return Constants.EMPTY_STRING_ARRAY;
        }
        return (String[])((ValueMap)parentTplRes.adaptTo(ValueMap.class)).get("allowedChildren", (Object)Constants.EMPTY_STRING_ARRAY);
    }

    public boolean isAllowedChild(Template template) {
        String[] allowedChildren = (String[])this.getProperties().get("allowedChildren", (Object)Constants.EMPTY_STRING_ARRAY);
        String tpPath = template.getPath();
        if (allowedChildren.length == 0) {
            return true;
        }
        for (String path : allowedChildren) {
            try {
                if (!Pattern.matches(path, tpPath)) continue;
                return true;
            }
            catch (Exception e) {
                log.error("Error while checking if " + tpPath + " matches " + path + " on " + this.getPath(), (Throwable)e);
            }
        }
        return false;
    }

    public void write(JSONWriter jsonWriter) throws JSONException {
        jsonWriter.object();
        String title = this.getTitle();
        if (title != null && title.length() > 0) {
            jsonWriter.key("title").value((Object)title);
        } else {
            jsonWriter.key("title").value((Object)Text.getName((String)this.getPath()));
        }
        jsonWriter.key("ranking").value((Object)this.getRanking());
        String desc = this.getDescription();
        if (desc != null && desc.length() > 0) {
            jsonWriter.key("description").value((Object)desc);
        } else {
            jsonWriter.key("description").value((Object)"");
        }
        jsonWriter.key("path").value((Object)this.getPath());
        if (this.getThumbnailPath() != null) {
            jsonWriter.key("thumbnailPath").value((Object)this.getThumbnailPath());
        }
        if (this.getIconPath() != null) {
            jsonWriter.key("iconPath").value((Object)this.getIconPath());
        }
        jsonWriter.endObject();
    }

    private ValueMap internalGetProperties(Resource resource) {
        ValueMap properties = ValueMap.EMPTY;
        if (TemplateUtils.isAuthoredTemplate(resource)) {
            Resource contentResource = resource.getChild("jcr:content");
            if (contentResource != null) {
                properties = (ValueMap)contentResource.adaptTo(ValueMap.class);
            }
        } else {
            properties = (ValueMap)resource.adaptTo(ValueMap.class);
        }
        return properties;
    }

    public String getInitialContentPath() {
        Resource initialContentResource = this.getInitialContentResource();
        return initialContentResource != null ? initialContentResource.getPath() : null;
    }

    public ValueMap getProperties() {
        if (this.cachedProperties == null) {
            this.cachedProperties = this.internalGetProperties(this.resource);
        }
        return this.cachedProperties;
    }

    public boolean hasStructureSupport() {
        return this.resource.getChild("structure") != null;
    }

    public String getPageTypePath() {
        Resource initialContentResource = this.getInitialContentResource();
        return initialContentResource != null ? initialContentResource.getResourceType() : null;
    }

    private Resource getInitialContentResource() {
        if (this.hasStructureSupport()) {
            return this.resource.getChild("initial/jcr:content");
        }
        return this.resource.getChild("jcr:content");
    }
}