CommandServlet.java 2.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.servlets.AbstractCommandServlet
 *  com.day.cq.commons.servlets.HtmlStatusResponseHelper
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletResponse
 *  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.servlets.HtmlResponse
 */
package com.day.cq.analytics.testandtarget.impl.servlets;

import com.day.cq.analytics.testandtarget.TestandtargetCommand;
import com.day.cq.analytics.testandtarget.impl.servlets.TargetCommandService;
import com.day.cq.commons.servlets.AbstractCommandServlet;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
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.servlets.HtmlResponse;

@SlingServlet(name="com.day.cq.analytics.testandtarget.impl.servlets.CommandServlet", paths={"/libs/cq/analytics/testandtarget/command"}, methods={"POST"})
public class CommandServlet
extends AbstractCommandServlet {
    @Reference
    private TargetCommandService commandService;

    protected boolean hasCommand(SlingHttpServletRequest request) throws ServletException {
        String cmd = request.getParameter("cmd");
        return cmd != null;
    }

    protected void performCommand(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            TestandtargetCommand cmd;
            String command = request.getParameter("cmd");
            HtmlResponse htmlResponse = null;
            htmlResponse = this.commandService != null ? ((cmd = this.commandService.getCommand(command)) != null ? cmd.performCommand(request, response) : HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Unknown action " + command))) : HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)"TargetCommandService not available");
            if (htmlResponse != null) {
                htmlResponse.send((HttpServletResponse)response, false);
            }
        }
        catch (Exception e) {
            throw new ServletException(e.getMessage(), (Throwable)e);
        }
    }

    protected void bindCommandService(TargetCommandService targetCommandService) {
        this.commandService = targetCommandService;
    }

    protected void unbindCommandService(TargetCommandService targetCommandService) {
        if (this.commandService == targetCommandService) {
            this.commandService = null;
        }
    }
}