FormConstraintsServlet.java 4.74 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.servlets.AbstractPredicateServlet
 *  com.day.cq.wcm.foundation.forms.FormsManager
 *  com.day.cq.wcm.foundation.forms.FormsManager$ComponentDescription
 *  javax.servlet.ServletException
 *  org.apache.commons.collections.Predicate
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.mcm.campaign.servlets;

import com.day.cq.commons.servlets.AbstractPredicateServlet;
import com.day.cq.mcm.campaign.impl.IntegrationConfig;
import com.day.cq.wcm.foundation.forms.FormsManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Reference;
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.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(paths={"/libs/mcm/campaign/content/formConstraints"}, extensions={"json"}, methods={"GET"})
public class FormConstraintsServlet
extends AbstractPredicateServlet {
    private final Logger logger;
    @Reference
    private IntegrationConfig integrationConfig;

    public FormConstraintsServlet() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp, Predicate predicate) throws ServletException, IOException {
        String type = req.getParameter("type");
        if (type == null) {
            this.logger.error("Missing 'type' parameter.");
            resp.sendError(500, "Missing 'type' parameter");
            return;
        }
        try {
            JSONWriter w = new JSONWriter((Writer)resp.getWriter());
            resp.setContentType("application/json");
            resp.setCharacterEncoding("utf-8");
            this.writeConstraints(w, type, req);
        }
        catch (Exception e) {
            this.logger.error("Error while generating JSON list", (Throwable)e);
            resp.sendError(500, e.toString());
        }
    }

    private void writeJson(Iterator<FormsManager.ComponentDescription> descIter, JSONWriter w, boolean writeEmpty) throws JSONException {
        w.array();
        if (writeEmpty) {
            w.object();
            w.key("value").value((Object)"");
            w.key("text").value((Object)"None");
            w.endObject();
        }
        while (descIter.hasNext()) {
            FormsManager.ComponentDescription desc = descIter.next();
            w.object();
            w.key("value");
            w.value((Object)desc.getResourceType());
            w.key("text");
            w.value((Object)desc.getTitle());
            if (desc.getHint() != null) {
                w.key("qtip");
                w.value((Object)desc.getHint());
            }
            w.endObject();
        }
        w.endArray();
    }

    private void writeConstraints(JSONWriter w, String type, SlingHttpServletRequest req) throws JSONException {
        List<String> validConstraints = this.integrationConfig.getFormConstraints(type);
        FormsManager formsManager = (FormsManager)req.getResourceResolver().adaptTo(FormsManager.class);
        Iterator descriptions = formsManager.getConstraints();
        ArrayList<FormsManager.ComponentDescription> constraints = new ArrayList<FormsManager.ComponentDescription>();
        while (descriptions.hasNext()) {
            FormsManager.ComponentDescription toCheck = (FormsManager.ComponentDescription)descriptions.next();
            String resourceType = toCheck.getResourceType();
            if (!validConstraints.contains(resourceType)) continue;
            constraints.add(toCheck);
        }
        this.writeJson(constraints.iterator(), w, true);
    }

    protected void bindIntegrationConfig(IntegrationConfig integrationConfig) {
        this.integrationConfig = integrationConfig;
    }

    protected void unbindIntegrationConfig(IntegrationConfig integrationConfig) {
        if (this.integrationConfig == integrationConfig) {
            this.integrationConfig = null;
        }
    }
}