SitecatalystComponentServlet.java 9.36 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.components.Component
 *  com.day.cq.wcm.api.components.ComponentEditConfig
 *  com.day.cq.wcm.api.components.VirtualComponent
 *  com.day.text.Text
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.felix.scr.annotations.Component
 *  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.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl.servlets;

import com.day.cq.analytics.impl.ComponentFilter;
import com.day.cq.analytics.impl.ComponentService;
import com.day.cq.wcm.api.components.ComponentEditConfig;
import com.day.cq.wcm.api.components.VirtualComponent;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Component;
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.io.JSONWriter;
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/sitecatalystcomponent"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class SitecatalystComponentServlet
extends SlingSafeMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(SitecatalystComponentServlet.class);
    private static final String ICON = "icon";
    private static final String THUMBNAIL = "thumbnail";
    private static final String DESCRIPTION = "description";
    private static final String RESOURCE_TYPE = "resourceType";
    private static final String DIV_ATTRIBUTES = "divAttributes";
    private static final String TITLE = "title";
    private static final String GROUP = "group";
    private static final String PATH = "path";
    private static final String VIRTUAL = "virtual";
    private static final String DIALOG = "dialog";
    private static final String CONFIG = "config";
    private static final String TEMPLATE_PATH = "templatePath";
    private static final String CELL_NAMES = "cellNames";
    private static final String IS_CONTAINER = "isContainer";
    private static final String CQ_COMPONENT_GROUP = "cq:componentGroup";
    private static final String CQ_COMPONENT_NAME = "cq:componentName";
    @Reference
    private ComponentService componentService;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            ResourceResolver resolver = request.getResourceResolver();
            String prefix = request.getContextPath();
            if (prefix == null) {
                prefix = "";
            }
            ComponentFilter componentFilter = ComponentFilter.fromRequest((HttpServletRequest)request);
            String json = this.collectComponents(resolver, this.componentService.getComponents(resolver, componentFilter), prefix);
            response.getWriter().write(json);
            log.debug("Provided components for mode {} ", (Object)componentFilter);
        }
        catch (JSONException e) {
            log.error("Error while generating component json", (Throwable)e);
            throw new ServletException(e.getMessage(), (Throwable)e);
        }
    }

    private String collectComponents(ResourceResolver resolver, Collection<com.day.cq.wcm.api.components.Component> components, String prefix) throws JSONException {
        StringWriter ret = new StringWriter();
        JSONWriter w = new JSONWriter((Writer)ret);
        w.object();
        for (com.day.cq.wcm.api.components.Component c : components) {
            String dialogPath;
            ComponentEditConfig ed;
            String templatePath;
            String key = c.getResourceType();
            if (c instanceof VirtualComponent) {
                String p = c.getPath();
                key = p.substring(p.indexOf("/", 1) + 1);
            }
            w.key(key).object();
            w.key("path").value((Object)c.getPath());
            String title = null;
            String group = null;
            Resource analyticsResource = resolver.getResource(c.getPath() + "/analytics");
            if (analyticsResource != null) {
                ValueMap analyticsProperties = (ValueMap)analyticsResource.adaptTo(ValueMap.class);
                group = (String)analyticsProperties.get("cq:componentGroup", null);
                title = (String)analyticsProperties.get("cq:componentName", null);
            }
            if (group == null) {
                group = c.getComponentGroup();
            }
            if (group != null) {
                w.key("group").value((Object)group);
            }
            if (title == null || title.trim().length() == 0) {
                title = c.getTitle();
            }
            if (title == null) {
                title = Text.getName((String)c.getPath());
            }
            if (c instanceof VirtualComponent) {
                w.key("virtual").value(true);
            }
            w.key("title").value((Object)title);
            w.key("resourceType").value((Object)c.getResourceType());
            this.writeDivAttributes(w, c);
            if (c.getDescription() != null) {
                w.key("description").value((Object)c.getDescription());
            }
            if (c.getThumbnailPath() != null) {
                w.key("thumbnail").value((Object)(prefix + c.getThumbnailPath()));
            }
            if (c.getIconPath() != null) {
                w.key("icon").value((Object)(prefix + c.getIconPath()));
            }
            if ((ed = c.getEditConfig()) != null && !ed.isDefault()) {
                w.key("config");
                ed.write(w);
            }
            if ((dialogPath = c.getDialogPath()) != null) {
                if (!dialogPath.endsWith(".json")) {
                    dialogPath = dialogPath + ".infinity.json";
                }
                w.key("dialog").value((Object)dialogPath);
            }
            if ((templatePath = c.getTemplatePath()) != null) {
                w.key("templatePath").value((Object)templatePath);
            }
            w.key("cellNames");
            w.array();
            w.value((Object)c.getCellName());
            for (com.day.cq.wcm.api.components.Component component = c.getSuperComponent(); component != null; component = component.getSuperComponent()) {
                w.value((Object)component.getName());
            }
            w.endArray();
            if (c.isContainer()) {
                w.key("isContainer").value(c.isContainer());
            }
            w.endObject();
        }
        w.endObject();
        return ret.toString();
    }

    private void writeDivAttributes(JSONWriter w, com.day.cq.wcm.api.components.Component c) throws JSONException {
        HashMap<String, String> attrs = new HashMap<String, String>(c.getHtmlTagAttributes());
        if (!attrs.containsKey("class")) {
            StringBuffer clazz = new StringBuffer();
            String delim = "";
            for (com.day.cq.wcm.api.components.Component cc = c; cc != null; cc = cc.getSuperComponent()) {
                String name = cc.getCellName();
                if (name.length() <= 0) continue;
                clazz.append(delim);
                clazz.append(name);
                delim = " ";
            }
            if (clazz.length() > 0) {
                attrs.put("class", clazz.toString());
            }
        }
        if (!attrs.isEmpty()) {
            w.key("divAttributes").object();
            for (Map.Entry e : attrs.entrySet()) {
                w.key((String)e.getKey()).value(e.getValue());
            }
            w.endObject();
        }
    }

    protected void bindComponentService(ComponentService componentService) {
        this.componentService = componentService;
    }

    protected void unbindComponentService(ComponentService componentService) {
        if (this.componentService == componentService) {
            this.componentService = null;
        }
    }
}