MappingConflictsServlet.java 13.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.search.Predicate
 *  com.day.cq.search.PredicateGroup
 *  com.day.cq.search.Query
 *  com.day.cq.search.QueryBuilder
 *  com.day.cq.search.result.Hit
 *  com.day.cq.search.result.SearchResult
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  org.apache.commons.collections.Factory
 *  org.apache.commons.collections.map.MultiValueMap
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl.servlets;

import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.analytics.sitecatalyst.FrameworkComponent;
import com.day.cq.search.Predicate;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.paths", value={"/libs/cq/analytics/sitecatalyst/mappingconflicts"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class MappingConflictsServlet
extends SlingSafeMethodsServlet {
    private final Logger log;
    private static final String SCVAR_PARAM = "scVar";
    private static final String RSID_PARAM = "rsid";
    private static final String CQ_PAGE = "cq:Page";
    private static final String CQ_COMPONENT_NAME = "cq:componentName";
    @Reference
    private ConfigurationManagerFactory configManagerFactory;
    @Reference
    private QueryBuilder queryBuilder;
    @Reference
    private SlingRepository repository;
    @Reference
    private JcrResourceResolverFactory jcrResourceResolverFactory;
    private static Map<String, Map<String, MultiValueMap>> rsMappings = new HashMap<String, Map<String, MultiValueMap>>();
    private final Factory factory;

    public MappingConflictsServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.factory = new Factory(){

            public Object create() {
                return new HashSet();
            }
        };
    }

    @Activate
    protected void activate(ComponentContext componentContext) throws Exception {
    }

    @Deactivate
    protected void deactivate(ComponentContext componentContext) throws Exception {
        rsMappings.clear();
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            String[] scVarArray = request.getParameterValues("scVar");
            String[] rsidArray = request.getParameterValues("rsid");
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Resource resource = request.getResource();
            ResourceResolver resolver = resource.getResourceResolver();
            Session session = (Session)resolver.adaptTo(Session.class);
            JSONWriter json = new JSONWriter((Writer)response.getWriter());
            json.object();
            if (scVarArray != null && rsidArray != null) {
                HashSet<String> scVarSet = new HashSet<String>(Arrays.asList(scVarArray));
                HashMap<String, MultiValueMap> conflicts = new HashMap<String, MultiValueMap>();
                for (String scVar : scVarSet) {
                    conflicts.put(scVar, MultiValueMap.decorate(new HashMap(), (Factory)this.factory));
                }
                this.scanFrameworks(resolver, session);
                for (String rsid : rsidArray) {
                    Map<String, MultiValueMap> mapping = rsMappings.get(rsid);
                    if (mapping == null) continue;
                    for (Map.Entry<String, MultiValueMap> entry : mapping.entrySet()) {
                        String scVar2 = entry.getKey();
                        MultiValueMap rsMapping = entry.getValue();
                        if (!scVarSet.contains(scVar2) || rsMapping.size() <= 1) continue;
                        ((MultiValueMap)conflicts.get(scVar2)).putAll((Map)rsMapping);
                    }
                }
                for (Map.Entry entry : conflicts.entrySet()) {
                    String scVar3 = (String)entry.getKey();
                    MultiValueMap rsMapping = (MultiValueMap)entry.getValue();
                    if (rsMapping.isEmpty()) continue;
                    json.key(scVar3).object();
                    for (Object cqVar : rsMapping.keySet()) {
                        Collection frameworks = rsMapping.getCollection(cqVar);
                        json.key(cqVar.toString()).array();
                        for (Object framework : frameworks) {
                            json.value((Object)framework.toString());
                        }
                        json.endArray();
                    }
                    json.endObject();
                }
            }
            json.endObject();
        }
        catch (Exception e) {
            this.log.error("Exception occured while scanning for mapping conflicts", (Throwable)e);
            throw new ServletException("Exception occured while scanning for mapping conflicts", (Throwable)e);
        }
    }

    private void scanFrameworks(ResourceResolver resolver, Session session) {
        PredicateGroup rootPred = new PredicateGroup("components");
        Predicate pathPred = new Predicate("rootPath", "path");
        pathPred.set("path", "/etc/cloudservices/sitecatalyst/*//");
        pathPred.set("exact", "true");
        rootPred.add(pathPred);
        Predicate typePred = new Predicate("nodeType", "type");
        typePred.set("type", "cq:Page");
        rootPred.add(typePred);
        Query query = this.queryBuilder.createQuery(rootPred, session);
        query.setHitsPerPage(0);
        query.setStart(0);
        SearchResult result = query.getResult();
        List hits = result.getHits();
        rsMappings.clear();
        for (Hit hit : hits) {
            try {
                String path = hit.getPath();
                Resource resultResource = hit.getResource();
                Resource contentResource = resultResource.getChild("jcr:content");
                Configuration configuration = this.configManagerFactory.getConfigurationManager(resolver).getConfiguration(path);
                if (contentResource == null || configuration == null) continue;
                ValueMap contentProperties = (ValueMap)contentResource.adaptTo(ValueMap.class);
                Framework framework = (Framework)contentResource.adaptTo(Framework.class);
                if (contentProperties == null || framework == null) continue;
                String frameworkPath = resultResource.getPath();
                String rsidList = (String)configuration.getInherited("reportsuite", (Object)"");
                this.addMappings(rsidList.split(","), framework, frameworkPath);
            }
            catch (RepositoryException e) {
                this.log.error("Repository Exception occured while accessing an analytics component", (Throwable)e);
            }
        }
    }

    private void addMappings(String[] rsidArray, Framework framework, String path) {
        String name = (String)framework.get("jcr:title", (Object)"");
        Map<String, FrameworkComponent> allComponents = framework.getAllComponents();
        for (String rsid : rsidArray) {
            Map<String, MultiValueMap> mapping = this.getReportSuiteMapping(rsid);
            for (Map.Entry<String, FrameworkComponent> entry : allComponents.entrySet()) {
                FrameworkComponent component = entry.getValue();
                String componentName = (String)component.get("cq:componentName", (Object)"");
                MultiValueMap cqMappings = component.getCqMappings();
                for (Object cqVar : cqMappings.keySet()) {
                    String scVarList = cqMappings.get(cqVar).toString();
                    for (String scVar : scVarList.split(",")) {
                        MultiValueMap scMapping = this.getScMapping(mapping, scVar);
                        scMapping.put(cqVar, (Object)MappingConflictsServlet.generateConflictInfo(name, path, componentName));
                    }
                }
            }
        }
    }

    private Map<String, MultiValueMap> getReportSuiteMapping(String rsid) {
        Map<String, MultiValueMap> mapping = rsMappings.get(rsid);
        if (mapping == null) {
            mapping = new HashMap<String, MultiValueMap>();
            rsMappings.put(rsid, mapping);
        }
        return mapping;
    }

    private MultiValueMap getScMapping(Map<String, MultiValueMap> mapping, String scVar) {
        MultiValueMap scMapping = mapping.get(scVar);
        if (scMapping == null) {
            scMapping = new MultiValueMap();
            mapping.put(scVar, scMapping);
        }
        return scMapping;
    }

    private static String generateConflictInfo(String frameworkName, String frameworkPath, String componentName) {
        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("frameworkName", (Object)frameworkName);
        }
        catch (JSONException var4_4) {
            // empty catch block
        }
        try {
            jsonObj.put("frameworkPath", (Object)frameworkPath);
        }
        catch (JSONException var4_5) {
            // empty catch block
        }
        try {
            jsonObj.put("componentName", (Object)componentName);
        }
        catch (JSONException var4_6) {
            // empty catch block
        }
        return jsonObj.toString();
    }

    protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        this.configManagerFactory = configurationManagerFactory;
    }

    protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        if (this.configManagerFactory == configurationManagerFactory) {
            this.configManagerFactory = null;
        }
    }

    protected void bindQueryBuilder(QueryBuilder queryBuilder) {
        this.queryBuilder = queryBuilder;
    }

    protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
        if (this.queryBuilder == queryBuilder) {
            this.queryBuilder = null;
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindJcrResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        this.jcrResourceResolverFactory = jcrResourceResolverFactory;
    }

    protected void unbindJcrResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        if (this.jcrResourceResolverFactory == jcrResourceResolverFactory) {
            this.jcrResourceResolverFactory = null;
        }
    }

}