FacetListServlet.java 6.83 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  com.day.cq.wcm.webservicesupport.ConfigurationManager
 *  com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  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.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.impl.servlet;

import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
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.Value;
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.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.methods", value={"GET"}), @Property(name="sling.servlet.selectors", value={"facetlist"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.resourceTypes", value={"cq/searchpromote/components/facet"})})
public class FacetListServlet
extends SlingAllMethodsServlet {
    private final Logger log;
    @Reference
    private ConfigurationManagerFactory configManagerFactory;
    @Reference
    private SearchPromoteService searchpromote;

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

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Configuration config = null;
        try {
            config = this.getConfiguration(request, "search-promote");
        }
        catch (Exception e) {
            this.log.error("Could not get configuration", (Throwable)e);
        }
        String memberId = (String)config.get("memberid", (Object)"");
        String accountNo = (String)config.get("accountno", (Object)"");
        PrintWriter out = response.getWriter();
        JSONWriter writer = new JSONWriter((Writer)out);
        try {
            String facetList = this.searchpromote.getFacetList(memberId, accountNo);
            JSONObject facets = new JSONObject(facetList);
            out.write(facets.toString());
        }
        catch (Exception e) {
            this.log.error(e.getMessage(), (Throwable)e);
            try {
                writer.object();
                writer.endObject();
            }
            catch (JSONException ignored) {
                this.log.error(ignored.getMessage(), (Throwable)ignored);
            }
        }
        finally {
            out.flush();
        }
    }

    private Configuration getConfiguration(SlingHttpServletRequest request, String servicename) throws RepositoryException {
        String pagePath = request.getResource().getPath();
        if (pagePath.indexOf("/jcr:content") > -1) {
            pagePath = pagePath.substring(0, pagePath.indexOf("/jcr:content"));
        }
        ResourceResolver resolver = request.getResource().getResourceResolver();
        PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
        Page currentPage = pageManager.getPage(pagePath);
        ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resolver);
        Resource configRes = configManager.getConfigurationResource(currentPage.getContentResource());
        if (configRes != null) {
            String configPath = this.getConfigurationPath(configRes, servicename);
            return configManager.getConfiguration(configPath);
        }
        return null;
    }

    private String getConfigurationPath(Resource resource, String servicename) throws RepositoryException {
        Resource cloudconfigsContent = resource.getChild("jcr:content");
        Node cloudconfigsNode = (Node)cloudconfigsContent.adaptTo(Node.class);
        javax.jcr.Property prop = cloudconfigsNode.getProperty("cq:cloudserviceconfigs");
        String configPath = null;
        if (prop.isMultiple()) {
            for (Value val : prop.getValues()) {
                String path = val.getString();
                if (path == null || path.indexOf(servicename) <= -1) continue;
                configPath = path;
            }
        } else {
            String path = prop.getValue().getString();
            if (path != null && path.indexOf(servicename) > -1) {
                configPath = path;
            }
        }
        return configPath;
    }

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

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

    protected void bindSearchpromote(SearchPromoteService searchPromoteService) {
        this.searchpromote = searchPromoteService;
    }

    protected void unbindSearchpromote(SearchPromoteService searchPromoteService) {
        if (this.searchpromote == searchPromoteService) {
            this.searchpromote = null;
        }
    }
}