JSONCreatePageServlet.java 4.15 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  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.api.servlets.SlingAllMethodsServlet
 *  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.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
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.api.servlets.SlingAllMethodsServlet;
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/createPage"}, methods={"POST"})
public class JSONCreatePageServlet
extends SlingAllMethodsServlet {
    private static final String METADATA_PARAM = "metadataURL";
    private static final String SEEDDATA_PARAM = "dataURL";
    private final Logger log;

    public JSONCreatePageServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String parentPath = request.getParameter("parentPath");
        String pageLabel = request.getParameter("label");
        String template = request.getParameter("template");
        String pageTitle = request.getParameter("title");
        String metaDataURL = request.getParameter("metadataURL");
        String dataURL = request.getParameter("dataURL");
        try {
            PageManager pageManager = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
            Page page = pageManager.create(parentPath, pageLabel, template, pageTitle);
            Node node = (Node)page.adaptTo(Node.class);
            node = node.getNode("jcr:content");
            Session session = null;
            try {
                session = node.getSession();
                if (metaDataURL != null && metaDataURL.length() > 0) {
                    node.setProperty("acMetaDataURL", metaDataURL);
                }
                if (dataURL != null && dataURL.length() > 0) {
                    node.setProperty("acDataURL", dataURL);
                }
                session.save();
            }
            catch (RepositoryException re) {
                node.remove();
                throw re;
            }
            finally {
                if (session != null) {
                    session.refresh(false);
                }
            }
            response.setContentType("application/json");
            JSONWriter writer = new JSONWriter((Writer)response.getWriter());
            try {
                writer.object();
                writer.key("path").value((Object)page.getPath());
                writer.endObject();
            }
            catch (JSONException je) {
                throw new ServletException((Throwable)je);
            }
        }
        catch (RepositoryException re) {
            response.sendError(500);
            this.log.error("Repository issue while creating a new page", (Throwable)re);
        }
        catch (WCMException wcme) {
            response.sendError(500);
            this.log.error("WCM issue while creating a new page", (Throwable)wcme);
        }
    }
}