AbstractOptionsServlet.java 4.5 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
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 */
package com.day.cq.analytics.sitecatalyst.impl.servlets;

import com.day.cq.analytics.sitecatalyst.SitecatalystWebservice;
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 javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

@Component
public class AbstractOptionsServlet
extends SlingAllMethodsServlet {
    protected static final String PARAM_CONFIG_PATH = "configPath";
    @Reference
    protected ConfigurationManagerFactory configManagerFactory;
    @Reference
    protected SitecatalystWebservice catalystService;

    protected Configuration getConfiguration(SlingHttpServletRequest request, String servicename) throws RepositoryException {
        String configPath = request.getParameter("configPath");
        ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(request.getResourceResolver());
        if (configPath != null) {
            return configManager.getConfiguration(configPath);
        }
        Resource resource = request.getResource();
        ResourceResolver resolver = resource.getResourceResolver();
        PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
        Page currentPage = pageManager.getContainingPage(resource);
        Configuration configuration = (Configuration)currentPage.adaptTo(Configuration.class);
        if (configuration != null) {
            return configuration;
        }
        Resource configRes = configManager.getConfigurationResource(currentPage.getContentResource());
        if (configRes != null) {
            return configManager.getConfiguration(this.getConfigurationPath(configRes, servicename));
        }
        return null;
    }

    protected String getConfigurationPath(Resource resource, String servicename) throws RepositoryException {
        Resource cloudconfigsContent = resource.getChild("jcr:content");
        Node cloudconfigsNode = (Node)cloudconfigsContent.adaptTo(Node.class);
        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 bindCatalystService(SitecatalystWebservice sitecatalystWebservice) {
        this.catalystService = sitecatalystWebservice;
    }

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