UpdateAccountOptionsCommand.java 5.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  com.google.gson.JsonObject
 *  javax.servlet.ServletException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.HtmlResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl.servlets.commands;

import com.day.cq.analytics.testandtarget.TestandtargetCommand;
import com.day.cq.analytics.testandtarget.TestandtargetException;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class UpdateAccountOptionsCommand
implements TestandtargetCommand {
    private static final Logger LOG = LoggerFactory.getLogger(UpdateAccountOptionsCommand.class);
    @Reference
    private TestandtargetPrivateService testandtargetService;
    @Reference
    private ConfigurationManagerFactory cfgManagerFactory;

    @Override
    public String getName() {
        return "updateAccountOptions";
    }

    @Override
    public HtmlResponse performCommand(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String configPath = request.getParameter("configPath");
        if (StringUtils.isEmpty((String)"configPath")) {
            this.sendError(response, "The required parameter <configPath> is missing from the request", 400);
            return null;
        }
        Configuration configuration = this.cfgManagerFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(configPath);
        if (configuration == null) {
            this.sendError(response, "No Adobe Target cloud service configuration found at path " + configPath + ".", 500);
            return null;
        }
        try {
            this.testandtargetService.updateAccountOptions(configuration);
            this.sendOk(response);
        }
        catch (TestandtargetException e) {
            LOG.error(e.getMessage(), (Throwable)e);
            this.sendError(response, "Error occured while updating the account options. Please see server log for details.", 500);
        }
        return null;
    }

    private void sendError(SlingHttpServletResponse response, String msg, int status) {
        response.setContentType("application/json");
        response.setStatus(status);
        JsonObject responseJson = new JsonObject();
        responseJson.addProperty("status", "error");
        responseJson.addProperty("message", msg);
        try {
            PrintWriter out = response.getWriter();
            out.print(responseJson.toString());
        }
        catch (IOException e) {
            LOG.error(e.getMessage(), (Throwable)e);
        }
    }

    private void sendOk(SlingHttpServletResponse response) {
        response.setContentType("application/json");
        response.setStatus(200);
        JsonObject responseJson = new JsonObject();
        responseJson.addProperty("status", "success");
        try {
            PrintWriter out = response.getWriter();
            out.print(responseJson.toString());
        }
        catch (IOException e) {
            LOG.error(e.getMessage(), (Throwable)e);
        }
    }

    protected void bindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        this.testandtargetService = testandtargetPrivateService;
    }

    protected void unbindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
        if (this.testandtargetService == testandtargetPrivateService) {
            this.testandtargetService = null;
        }
    }

    protected void bindCfgManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        this.cfgManagerFactory = configurationManagerFactory;
    }

    protected void unbindCfgManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        if (this.cfgManagerFactory == configurationManagerFactory) {
            this.cfgManagerFactory = null;
        }
    }
}