SiteImporterServlet.java 8.85 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  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.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.osgi.framework.BundleContext
 *  org.osgi.service.component.ComponentContext
 */
package com.day.cq.wcm.siteimporter.internal.servlet;

import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.ImporterStep;
import com.day.cq.wcm.siteimporter.ResourcesBoard;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResource;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
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.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;

@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/wcm/bin/siteimporter"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"POST"}), @Property(name="service.description", value={"Site Importer POST Servlet"})})
public class SiteImporterServlet
extends SlingAllMethodsServlet {
    private static final long serialVersionUID = 9112167990766136591L;
    private BundleContext bundleContext;
    @Reference(name="Step", cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC, referenceInterface=ImporterStep.class)
    private List<ImporterStep> steps = new ArrayList<ImporterStep>();

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        URL original;
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        try {
            original = new URL(request.getParameter("original"));
        }
        catch (MalformedURLException e) {
            throw new ServletException("Url was malformed, please correct.");
        }
        ArrayList<ImporterStep> tasks = new ArrayList<ImporterStep>();
        List<ImporterStep> list = this.steps;
        synchronized (list) {
            tasks.addAll(this.steps);
        }
        Collections.sort(tasks, new Comparator<ImporterStep>(){

            @Override
            public int compare(ImporterStep step1, ImporterStep step2) {
                return step1.getOrder().compareTo(step2.getOrder());
            }
        });
        PrintWriter out = response.getWriter();
        out.write("<html><head>");
        out.write("<style type=\"text/css\">");
        out.write("div {");
        out.write("  font-family:arial,tahoma,helvetica,sans-serif;");
        out.write("  font-size:11px;");
        out.write("  white-space:nowrap;");
        out.write("}");
        out.write(".action {");
        out.write("  display: inline;");
        out.write("  width: 120px;");
        out.write("  float: left;");
        out.write("}");
        out.write(".error {");
        out.write("  color: red;");
        out.write("  font-weight: bold;");
        out.write("}");
        out.write(".skip {");
        out.write("  color: gray;");
        out.write("  font-style: italic;");
        out.write("}");
        out.write(".heading {");
        out.write("  font-weight: bold;");
        out.write("  cursor: pointer;");
        out.write("}");
        out.write(".title {");
        out.write("  display: inline;");
        out.write("  width: 150px;");
        out.write("  float: left;");
        out.write("  margin: 0 8px 0 0;");
        out.write("  overflow: hidden;");
        out.write("}");
        out.write(".activate {");
        out.write("}");
        out.write(".ignore {");
        out.write("  color: #888888;");
        out.write("}");
        out.write(".cf {");
        out.write("  color: #888888;");
        out.write("}");
        out.write(".path {");
        out.write("  display: inline;");
        out.write("  width: 100%;");
        out.write("}");
        out.write("ol {");
        out.write("  list-style-type:none;");
        out.write("  margin:0px 0px 0px 10px;");
        out.write("  padding:0px;");
        out.write("}");
        out.write("</style>");
        out.write("<script type=\"text/javascript\">\n");
        out.write("var success = false;\n\n");
        out.write("function finish(result) {\n");
        out.write("  success = result;\n");
        out.write("  parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").reset(true);\n");
        out.write("}\n\n");
        out.write("function toggleDetails(heading) {\n");
        out.write("  var details = heading.nextSibling;\n");
        out.write("  while (details != null) {\n");
        out.write("    if (details.tagName && (details.tagName.toLowerCase() == \"ol\")) {\n");
        out.write("      if (details.style.display == \"none\") {\n");
        out.write("        details.style.display = \"block\";\n");
        out.write("      } else {\n");
        out.write("        details.style.display = \"none\";\n");
        out.write("      }\n");
        out.write("      break;\n");
        out.write("    } else {\n");
        out.write("      details = details.nextSibling;\n");
        out.write("    }\n");
        out.write("  }\n");
        out.write("}\n\n");
        out.write("parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").show();\n");
        out.write("parent.CQ.Ext.getCmp(\"cq-siteimporter-progress\").wait({interval:200, increment:40});\n");
        out.write("</script>\n");
        out.write("</head><body><div><ol style=\"margin:0\">");
        ImporterContext ctx = ImporterContext.createContext(request, response, this.bundleContext);
        long time = new Date().getTime();
        boolean success = true;
        String errMsg = "";
        Iterator it = tasks.iterator();
        while (success && it.hasNext()) {
            ImporterStep next = (ImporterStep)it.next();
            try {
                success = next.execute(ctx);
            }
            catch (Exception e) {
                success = false;
                errMsg = " " + e.getMessage();
                this.log("Error while importing site", (Throwable)e);
            }
        }
        time = (new Date().getTime() - time) / 1000;
        if (success) {
            ctx.output("Import successfully finished in " + time + " seconds", "heading");
        } else {
            ctx.repairIndent();
            ctx.error("An error occured during the import.");
            ctx.error(errMsg);
            ctx.error("Please check the log for details.");
        }
        out.write("</ol></div>");
        if (success) {
            ImporterResource origRes = ctx.getBoard().getResource(original, ctx);
            out.write("<script language=\"javascript\">finish(\"" + origRes.getDownloadLocation() + "\");</script>");
        } else {
            out.write("<script language=\"javascript\">finish(null);</script>");
        }
        out.write("</body></html>");
        ctx.dispose();
    }

    protected void activate(ComponentContext context) throws RepositoryException {
        this.bundleContext = context.getBundleContext();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void bindStep(ImporterStep step) {
        List<ImporterStep> list = this.steps;
        synchronized (list) {
            this.steps.add(step);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void unbindStep(ImporterStep step) {
        List<ImporterStep> list = this.steps;
        synchronized (list) {
            this.steps.remove(step);
        }
    }

}