ReplicationServlet.java 5.73 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.replication.ReplicationActionType
 *  com.day.cq.replication.ReplicationException
 *  com.day.cq.replication.ReplicationStatus
 *  com.day.cq.replication.Replicator
 *  javax.jcr.Session
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.commons.lang3.time.DateFormatUtils
 *  org.apache.commons.lang3.time.FastDateFormat
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.slf4j.Logger
 */
package com.day.crx.delite.impl.servlets;

import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationStatus;
import com.day.cq.replication.Replicator;
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.Calendar;
import javax.jcr.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;

public class ReplicationServlet
extends AbstractServlet {
    private Replicator replicator;

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

    protected void doService(HttpServletRequest req, HttpServletResponse res, Session session) throws ServletException, IOException {
        this.initReplicator();
        if (req.getMethod().equals("GET")) {
            this.doGet(req, res, session);
            return;
        }
        if (req.getMethod().equals("POST")) {
            String action = req.getParameter("action");
            if ("replicate".equals(action)) {
                this.replicate(req, res, session);
            } else if ("replicatedelete".equals(action)) {
                this.replicatedelete(req, res, session);
            }
            return;
        }
        res.sendError(405);
    }

    private void initReplicator() {
        if (this.replicator == null) {
            this.replicator = (Replicator)this.bundleContext.getService(this.bundleContext.getServiceReference(Replicator.class.getName()));
        }
    }

    private void doGet(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
        RequestData req = new RequestData(request);
        String path = req.getParameter("path");
        if (StringUtils.isBlank((CharSequence)path)) {
            res.sendError(404);
            return;
        }
        try {
            ReplicationStatus status = this.replicator.getReplicationStatus(session, path);
            if (status == null) {
                res.sendError(404);
                return;
            }
            res.setContentType("application/json");
            res.setCharacterEncoding("UTF-8");
            JSONWriter writer = new JSONWriter((Writer)res.getWriter());
            writer.object();
            writer.key("isActivated").value(status.isActivated());
            writer.key("isDeactivated").value(status.isDeactivated());
            writer.key("isDelivered").value(status.isDelivered());
            writer.key("isPending").value(status.isPending());
            writer.key("lastPublished").value((Object)this.format(status.getLastPublished()));
            writer.key("lastPublishedBy").value((Object)status.getLastPublishedBy());
            writer.key("lastReplicationAction").value((Object)status.getLastReplicationAction());
            writer.endObject();
        }
        catch (JSONException e) {
            this.logger.error("Error occur getting replication status: " + path, (Throwable)e);
            res.sendError(500, e.getMessage());
        }
    }

    private String format(Calendar calendar) {
        if (calendar == null) {
            return null;
        }
        return DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(calendar);
    }

    private void replicate(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
        RequestData req = new RequestData(request);
        String path = req.getParameter("path");
        if (StringUtils.isBlank((CharSequence)path)) {
            res.sendError(404);
            return;
        }
        try {
            this.replicator.replicate(session, ReplicationActionType.ACTIVATE, path);
        }
        catch (ReplicationException e) {
            this.logger.error("Error occur replicating (activate): " + path, (Throwable)e);
            res.sendError(500, e.getMessage());
        }
    }

    private void replicatedelete(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
        RequestData req = new RequestData(request);
        String path = req.getParameter("path");
        if (StringUtils.isBlank((CharSequence)path)) {
            res.sendError(404);
            return;
        }
        try {
            this.replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
        }
        catch (ReplicationException e) {
            this.logger.error("Error occur replicating (deactivate): " + path, (Throwable)e);
            res.sendError(500, e.getMessage());
        }
    }
}