SitecatalystServlet.java 7.96 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
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.sitecatalyst.impl.servlets;

import com.day.cq.analytics.sitecatalyst.SitecatalystException;
import com.day.cq.analytics.sitecatalyst.SitecatalystWebservice;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.paths", value={"/libs/cq/analytics/sitecatalyst/service"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class SitecatalystServlet
extends SlingAllMethodsServlet {
    private final Logger log;
    private static final String PARAMETER_METHOD = "method";
    private static final String PARAMETER_CONFIG_PATH = "configPath";
    private static final String PARAMETER_COMPANY = "company";
    private static final String PARAMETER_USERNAME = "username";
    private static final String PARAMETER_PASSWORD = "password";
    private static final String PARAMETER_SERVER = "server";
    private static final String PARAMETER_RSID = "rsid";
    @Reference
    private ConfigurationManagerFactory configManagerFactory;
    @Reference
    protected SitecatalystWebservice catalystService;

    public SitecatalystServlet() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block8 : {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            String method = request.getParameter("method");
            try {
                if ("Connect".equals(method)) {
                    this.handleConnect(request, out);
                    break block8;
                }
                this.handleAPICall(request, out, method);
            }
            catch (SitecatalystException e) {
                this.log.error("Call to SiteCatalyst method '" + request.getParameter("method") + "' failed", (Throwable)e);
                out.write("{error: '" + e.getMessage() + "'}");
            }
            catch (Exception e) {
                this.log.error("Exception occured while calling Sitecatalyst service", (Throwable)e);
                throw new ServletException("Exception occured while calling Sitecatalyst service", (Throwable)e);
            }
            finally {
                out.flush();
            }
        }
    }

    private void handleConnect(SlingHttpServletRequest request, Writer out) throws Exception {
        String jsonResponse = "{}";
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String server = request.getParameter("server");
        try {
            String secret = this.catalystService.getLoginKey(server, company, username, password);
            JSONObject json = new JSONObject();
            if (password != null) {
                json.put("secret", (Object)secret);
            }
            jsonResponse = json.toString();
        }
        catch (JSONException e) {
            this.log.error(e.getMessage(), (Throwable)e);
        }
        out.write(jsonResponse);
    }

    private void handleAPICall(SlingHttpServletRequest request, Writer out, String method) throws Exception {
        String jsonResponse = "{}";
        String rsid = request.getParameter("rsid");
        String configPath = request.getParameter("configPath");
        Configuration configuration = this.configManagerFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(configPath);
        if (configuration == null) {
            throw new SitecatalystException("Configuration has not been found");
        }
        if (rsid == null) {
            rsid = (String)configuration.getInherited("reportsuite", (Object)"");
        }
        if (method.equals("Company.GetReportSuites")) {
            jsonResponse = this.catalystService.getReportSuites(configuration);
        } else if (method.equals("Company.GetTrackingServer")) {
            String[] rsidArray = rsid.split(",");
            if (rsidArray.length > 0) {
                jsonResponse = this.catalystService.getTrackingServer(configuration, rsidArray[0]);
            }
        } else if (method.equals("ReportSuite.GetEVars")) {
            jsonResponse = this.catalystService.getEvars(configuration, rsid);
        } else if (method.equals("ReportSuite.GetTrafficVars")) {
            jsonResponse = this.catalystService.getTrafficVars(configuration, rsid);
        } else if (method.equals("ReportSuite.GetSuccessEvents")) {
            jsonResponse = this.catalystService.getSuccessEvents(configuration, rsid);
        }
        out.write(jsonResponse);
    }

    private void saveConfiguration(Configuration configuration) {
        try {
            Session session;
            Resource resource = configuration.getResource();
            if (resource != null && (session = ((Node)resource.adaptTo(Node.class)).getSession()).hasPendingChanges()) {
                session.save();
            }
        }
        catch (RepositoryException e) {
            this.log.error("Could not save updated configuration", (Throwable)e);
        }
    }

    protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        this.configManagerFactory = configurationManagerFactory;
    }

    protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
        if (this.configManagerFactory == configurationManagerFactory) {
            this.configManagerFactory = null;
        }
    }

    protected void bindCatalystService(SitecatalystWebservice sitecatalystWebservice) {
        this.catalystService = sitecatalystWebservice;
    }

    protected void unbindCatalystService(SitecatalystWebservice sitecatalystWebservice) {
        if (this.catalystService == sitecatalystWebservice) {
            this.catalystService = null;
        }
    }
}