OverlayNodeServlet.java 6.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.nodetype.NodeType
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang3.StringEscapeUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 */
package com.day.crx.delite.impl.servlets;

import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;

public class OverlayNodeServlet
extends AbstractServlet {
    private static final String PARAM_PATH = "path";
    private static final String PARAM_OVERLAY_LOCATION = "overlayLocation";
    private static final String PARAM_MATCH_TYPE = "matchType";

    public OverlayNodeServlet(BundleContext bc) {
        super(bc);
    }

    protected void doService(HttpServletRequest request, HttpServletResponse response, Session session) throws ServletException, IOException {
        block10 : {
            RequestData requestData = new RequestData(request);
            ResourceResolver resolver = (ResourceResolver)request.getAttribute("org.apache.sling.auth.core.ResourceResolver");
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            JSONWriter writer = new JSONWriter((Writer)response.getWriter());
            String path = requestData.getParameter("path");
            if (StringUtils.isBlank((CharSequence)path)) {
                response.sendError(404);
                return;
            }
            Resource resource = resolver.getResource(path);
            if (resource == null) {
                response.sendError(404);
                return;
            }
            String overlayLocation = requestData.getParameter("overlayLocation");
            if (StringUtils.isBlank((CharSequence)overlayLocation)) {
                response.sendError(404);
                return;
            }
            boolean matchType = "true".equals(requestData.getParameter("matchType"));
            try {
                try {
                    Node overlayNode = null;
                    ArrayList<Resource> intermediateResources = new ArrayList<Resource>();
                    String currentSearchPath = this.getCurrentSearchPath(resolver, resource.getPath());
                    if (currentSearchPath != null) {
                        writer.object();
                        Resource resourceToOverlay = resource;
                        this.logger.debug("Node to overlay    : {}", (Object)resourceToOverlay.getPath());
                        this.logger.debug("Overlay location   : {}", (Object)overlayLocation);
                        this.logger.debug("Current search path: {}", (Object)currentSearchPath);
                        String overlayPath = this.getOverlayPath(resourceToOverlay.getPath(), currentSearchPath, overlayLocation);
                        writer.key("overlayPath").value((Object)overlayPath);
                        while (resolver.getResource(overlayPath) == null) {
                            intermediateResources.add(0, resourceToOverlay);
                            resourceToOverlay = resourceToOverlay.getParent();
                            overlayPath = this.getOverlayPath(resourceToOverlay.getPath(), currentSearchPath, overlayLocation);
                        }
                        for (Resource currentResource : intermediateResources) {
                            overlayNode = JcrUtils.getOrCreateByPath((String)currentResource.getPath().replaceFirst(currentSearchPath, overlayLocation), (String)(matchType ? ((Node)currentResource.adaptTo(Node.class)).getPrimaryNodeType().getName() : "nt:unstructured"), (Session)session);
                            this.logger.debug("Created overlay    : {}", (Object)overlayNode.getPath());
                        }
                        writer.key("success").value(true);
                        writer.endObject();
                        session.save();
                        break block10;
                    }
                    response.sendError(404);
                }
                catch (RepositoryException e) {
                    writer.object();
                    writer.key("success").value(false);
                    writer.key("message").value((Object)StringEscapeUtils.escapeHtml4((String)e.getMessage()));
                    writer.endObject();
                }
            }
            catch (JSONException e) {
                this.logger.error("Unable to write response", (Throwable)e);
                response.sendError(500, "Unable to send status while overlaying node");
            }
        }
    }

    private String getCurrentSearchPath(ResourceResolver resolver, String path) {
        String currentSearchPath = null;
        for (String searchPath : resolver.getSearchPath()) {
            if (!path.startsWith(searchPath)) continue;
            currentSearchPath = searchPath;
        }
        return currentSearchPath;
    }

    private String getOverlayPath(String path, String currentSearchPath, String overlayPath) {
        return path.replaceFirst(currentSearchPath, overlayPath);
    }
}