BlueprintServlet.java 11.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ResourceStatusProvider
 *  com.day.cq.commons.TidyJSONWriter
 *  com.day.cq.commons.servlets.AbstractPredicateServlet
 *  com.day.cq.commons.servlets.HtmlStatusResponseHelper
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.BlueprintManagerFactory
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.api.RolloutManager$Trigger
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RangeIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.collections.Predicate
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.sling.api.request.RequestParameterMap
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.HtmlResponse
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.msm.impl.servlets;

import com.day.cq.commons.ResourceStatusProvider;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.commons.servlets.AbstractPredicateServlet;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.BlueprintManagerFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerFactoryImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerImpl;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RangeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service(value={ResourceStatusProvider.class, Servlet.class})
@SlingServlet(generateService=0, resourceTypes={"sling/servlet/default"}, selectors={"blueprint"}, extensions={"conf", "json"}, methods={"POST", "GET"})
public class BlueprintServlet
extends AbstractPredicateServlet
implements ResourceStatusProvider {
    private static final long serialVersionUID = 1739398700022982003L;
    private static final Logger log = LoggerFactory.getLogger(BlueprintServlet.class);
    private static final String TIDY_PARAM = "tidy";
    private static final String RETURN_RELATIONSHIPS_PARAM = "returnRelationships";
    private static final String DISPLAY_LIMIT = "maxSize";
    private static final String START = "start";
    private static final String TRIGGER = "msm:trigger";
    @org.apache.felix.scr.annotations.Property(propertyPrivate=1)
    private static final long DEFAULT_LIST_LIMIT = 2500;
    @Reference
    private LiveRelationshipManager relationshipManager = null;
    @Reference(referenceInterface=BlueprintManagerFactory.class, target="(service.pid=com.day.cq.wcm.msm.impl.BlueprintManagerFactoryImpl)")
    private BlueprintManagerFactoryImpl blueprintManagerFactory = null;

    protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp, Predicate predicate) throws ServletException, IOException {
        StringWriter buf = new StringWriter();
        TidyJSONWriter writer = new TidyJSONWriter((Writer)buf);
        writer.setTidy("true".equals(req.getParameter("tidy")));
        boolean writeRelations = "true".equals(req.getParameter("returnRelationships"));
        long skip = BlueprintServlet.readIntParameter(req, "start", 0);
        long limit = BlueprintServlet.readIntParameter(req, "maxSize", 2500);
        RolloutManager.Trigger triggerFilter = null;
        if (req.getRequestParameter("msm:trigger") != null) {
            try {
                triggerFilter = RolloutManager.Trigger.fromName((String)req.getRequestParameter("msm:trigger").getString());
            }
            catch (IllegalArgumentException ignore) {
                // empty catch block
            }
        }
        try {
            Resource resource = req.getResource();
            RangeIterator relations = this.relationshipManager.getLiveRelationships(resource, null, triggerFilter);
            Node sourceNode = Utils.getNode(req.getResourceResolver(), resource.getPath());
            BlueprintServlet.writeBlueprintInfo((JSONWriter)writer, relations, sourceNode, writeRelations, skip, limit);
        }
        catch (Exception e) {
            throw new ServletException((Throwable)e);
        }
        resp.setContentType("application/json");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print(buf.getBuffer().toString());
    }

    public void writeStatus(SlingHttpServletRequest request, JSONWriter writer, Resource resource) throws JSONException {
        writer.key("blueprint");
        try {
            RangeIterator relations = this.relationshipManager.getLiveRelationships(resource, null, null);
            Node sourceNode = Utils.getNode(resource.getResourceResolver(), resource.getPath());
            BlueprintServlet.writeBlueprintInfo(writer, relations, sourceNode, false, 0, 2500);
        }
        catch (RepositoryException e) {
            log.error("Error during retrieval of blueprint info.", (Throwable)e);
        }
        catch (WCMException e) {
            log.error("Error during retrieval of blueprint info.", (Throwable)e);
        }
    }

    private static void writeBlueprintInfo(JSONWriter writer, RangeIterator relations, Node sourceNode, boolean writeRelations, long skip, long limit) throws JSONException, RepositoryException {
        writer.object();
        writer.key("msm:targets");
        writer.array();
        limit += skip;
        long total = 0;
        try {
            while (relations.hasNext()) {
                long pos = relations.getPosition();
                LiveRelationship relation = (LiveRelationship)relations.next();
                if (pos >= skip) {
                    if (writeRelations) {
                        writer.object();
                        writer.key("msm:targetPath").value((Object)relation.getTargetPath());
                        writer.key("msm:liveRelationship");
                        relation.write(writer);
                        writer.endObject();
                    } else {
                        writer.value((Object)Text.getRelativeParent((String)relation.getTargetPath(), (int)1));
                    }
                }
                if (pos + 1 != limit) continue;
                break;
            }
            if ((total = relations.getSize()) < 0) {
                total = relations.getPosition();
                if (relations.hasNext()) {
                    ++total;
                }
            }
        }
        catch (Exception e) {
            log.error("Error computing relationship", (Throwable)e);
        }
        writer.endArray();
        writer.key("total").value(total);
        writer.key("cq:rolloutConfigs");
        writer.array();
        Node configNode = null;
        if (sourceNode != null && sourceNode.hasNode("cq:BlueprintSyncConfig")) {
            configNode = sourceNode.getNode("cq:BlueprintSyncConfig");
        }
        if (configNode != null) {
            Value[] values;
            for (Value v : values = configNode.getProperty("cq:rolloutConfigs").getValues()) {
                writer.value((Object)v.getString());
            }
        }
        writer.endArray();
        writer.endObject();
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        HtmlResponse htmlResponse;
        try {
            Resource resource = request.getResource();
            String[] rolloutConfigs = request.getParameterValues("cq:rolloutConfigs");
            if (rolloutConfigs != null && rolloutConfigs.length == 1 && "".equals(rolloutConfigs[0])) {
                rolloutConfigs = null;
            }
            Resource configResource = resource.getName().equals("jcr:content") ? resource.getParent() : resource;
            ResourceResolver resolver = request.getResourceResolver();
            BlueprintManagerImpl bpm = this.blueprintManagerFactory.getBlueprintManager(resolver);
            BlueprintImpl blueprint = bpm.getContainingBlueprint(configResource.getPath());
            if (blueprint != null) {
                String relPath = "";
                if (configResource.getPath().length() > blueprint.getSitePath().length()) {
                    relPath = relPath + configResource.getPath().substring(blueprint.getSitePath().length() + 1);
                }
                blueprint.setRolloutConfigs(relPath, rolloutConfigs);
                resolver.commit();
            }
            htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)"Blueprint config updated");
        }
        catch (Exception e) {
            log.error("Error while updating blueprint config", (Throwable)e);
            htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Error while updating blueprint config: " + e.getMessage()));
        }
        htmlResponse.send((HttpServletResponse)response, true);
    }

    public void bindBlueprintManagerFactory(BlueprintManagerFactory factory) {
        this.blueprintManagerFactory = (BlueprintManagerFactoryImpl)factory;
    }

    public void unbindBlueprintManagerFactory(BlueprintManagerFactory factory) {
        this.blueprintManagerFactory = null;
    }

    private static long readIntParameter(SlingHttpServletRequest req, String parameterName, long defaultValue) {
        if (req.getRequestParameterMap().containsKey((Object)parameterName)) {
            try {
                return Long.valueOf(req.getRequestParameter(parameterName).getString());
            }
            catch (NumberFormatException ignore) {
                // empty catch block
            }
        }
        return defaultValue;
    }

    protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        this.relationshipManager = liveRelationshipManager;
    }

    protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
        if (this.relationshipManager == liveRelationshipManager) {
            this.relationshipManager = null;
        }
    }
}