ConfigurationPostProcessor.java 4.09 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  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.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.SlingPostProcessor
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.webservicesupport.impl;

import java.util.HashSet;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
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.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.SlingPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class ConfigurationPostProcessor
implements SlingPostProcessor {
    protected final Logger logger;
    private final String PAGE_MARKER = "/jcr:content/";
    private final String RP_CANCELINHERITANCE = ":cancelInheritance";

    public ConfigurationPostProcessor() {
        this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
        this.PAGE_MARKER = "/jcr:content/";
        this.RP_CANCELINHERITANCE = ":cancelInheritance";
    }

    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
        HashSet<String> fixPaths = new HashSet<String>();
        for (Modification mod : changes) {
            switch (mod.getType()) {
                case ORDER: 
                case MOVE: 
                case COPY: {
                    break;
                }
                case MODIFY: {
                    int pageEndPos = mod.getSource().indexOf("/jcr:content/");
                    if (pageEndPos == -1) break;
                    String pagePath = mod.getSource().substring(0, pageEndPos);
                    fixPaths.add(pagePath);
                    break;
                }
            }
        }
        ResourceResolver resolver = request.getResourceResolver();
        for (String pagePath : fixPaths) {
            String contentPath = pagePath + '/' + "jcr:content";
            this.logger.debug("Checking request {} for cloud service configuration", (Object)request);
            Resource contentResource = resolver.getResource(contentPath);
            if (contentResource == null) continue;
            this.fixStructure(request, contentResource);
        }
    }

    private void fixStructure(SlingHttpServletRequest request, Resource resource) {
        boolean cancelInheritance = Boolean.valueOf(request.getParameter(":cancelInheritance"));
        if (cancelInheritance && !this.hasServiceConfigurations(request) && ResourceUtil.isA((Resource)resource.getParent(), (String)"cq:Page")) {
            try {
                Node node = (Node)resource.adaptTo(Node.class);
                node.setProperty("cq:cloudserviceconfigs", new String[0]);
                this.logger.debug("Adding empty {} property to page {}", (Object)"cq:cloudserviceconfigs", (Object)resource.getPath());
            }
            catch (RepositoryException e) {
                this.logger.error("Could not add empty property {}", (Object)"cq:cloudserviceconfigs");
            }
        }
    }

    private boolean hasServiceConfigurations(SlingHttpServletRequest request) {
        String[] parameters;
        for (String value : parameters = request.getParameterValues("./cq:cloudserviceconfigs")) {
            if ("".equals(value.trim())) continue;
            return true;
        }
        return false;
    }

}