MboxListServlet.java 4.11 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.Gson
 *  javax.servlet.ServletException
 *  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.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingSafeMethodsServlet
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.analytics.testandtarget.impl.servlets;

import com.day.cq.analytics.testandtarget.TestandtargetService;
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.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SlingServlet(generateComponent=1, methods={"GET"}, extensions={"json"}, selectors={"mboxlist"}, resourceTypes={"cq/analytics/components/testandtargetpage", "cq/analytics/components/cbframework"}, label="Adobe Target Integration Mbox List Servlet", description="Provides the mbox names list in JSON format")
public class MboxListServlet
extends SlingSafeMethodsServlet {
    private final Logger LOGGER;
    @Reference
    private ConfigurationManagerFactory cfgManagerFactory;
    @Reference
    private TestandtargetService tntService;

    public MboxListServlet() {
        this.LOGGER = LoggerFactory.getLogger(this.getClass());
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Resource tntConfigResource = request.getResource();
        if (tntConfigResource != null) {
            ConfigurationManager cfgManager = this.cfgManagerFactory.getConfigurationManager(tntConfigResource.getResourceResolver());
            Configuration tntConfig = cfgManager.getConfiguration(tntConfigResource.getParent().getPath());
            if (tntConfig != null) {
                try {
                    List<String> mboxList = this.tntService.listMboxNames(tntConfig);
                    response.setContentType("application/json");
                    response.setStatus(200);
                    response.getWriter().write(new Gson().toJson(mboxList));
                }
                catch (Exception e) {
                    response.setStatus(500);
                    this.LOGGER.error("Can't get the mbox list for TnT configuration " + tntConfigResource.getPath(), (Throwable)e);
                }
            } else {
                this.LOGGER.warn("No Test and Target cloud configuration at path " + tntConfigResource.getPath());
            }
        }
    }

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

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

    protected void bindTntService(TestandtargetService testandtargetService) {
        this.tntService = testandtargetService;
    }

    protected void unbindTntService(TestandtargetService testandtargetService) {
        if (this.tntService == testandtargetService) {
            this.tntService = null;
        }
    }
}