AudienceManagerConfigurationAdapter.java 6.25 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  com.day.cq.wcm.api.Page
 *  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.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  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.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.aam;

import com.adobe.cq.aam.client.spi.AudienceManagerConfiguration;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import java.util.Arrays;

@Component(immediate=1, metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="adaptables", value={"org.apache.sling.api.resource.Resource"}), @Property(name="adapters", value={"com.adobe.cq.aam.client.spi.AudienceManagerConfiguration"})})
public class AudienceManagerConfigurationAdapter
implements AdapterFactory {
    private static final String CQ_CLOUDSERVICECONFIGS = "cq:cloudserviceconfigs";
    private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerConfigurationAdapter.class);
    @Reference
    private ConfigurationManagerFactory configManagerFactory;

    @Activate
    public void activate() {
        LOGGER.debug("Activate");
    }

    @Deactivate
    public void deactivate() {
        LOGGER.debug("Deactivate");
    }

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        LOGGER.debug("Adapt atempt {} {} ", adaptable, type);
        if (type == AudienceManagerConfiguration.class && adaptable instanceof Resource) {
            Resource resource = (Resource)adaptable;
            ResourceResolver resourceResolver = resource.getResourceResolver();
            Page thisPage = this.getParentPage(resource);
            if (thisPage != null) {
                ConfigurationManager configManager;
                Configuration config;
                LOGGER.debug("Got Page {} {} ", (Object)thisPage.getPath());
                Object[] cloudServiceConfigs = this.getStringValues(thisPage, "cq:cloudserviceconfigs");
                LOGGER.debug("Got Cloud service configs {} ", (Object)Arrays.toString(cloudServiceConfigs));
                if (cloudServiceConfigs.length != 0 && (config = (configManager = this.configManagerFactory.getConfigurationManager(resourceResolver)).getConfiguration("audiencemanager", (String[])cloudServiceConfigs)) != null) {
                    try {
                        return (AdapterType)new AudienceManagerConfiguration(config.getContentResource());
                    }
                    catch (IllegalArgumentException e) {
                        LOGGER.debug("Resource {} is not a resource of type {} ", (Object)config.getPath(), (Object)"cq/personalization/audiencemanager/components/cloudservices/audiencemanagerpage");
                    }
                }
            } else {
                LOGGER.debug("Resource {} not adaptable to a Page ", (Object)resource.getPath());
            }
        }
        LOGGER.debug("No Config found");
        return null;
    }

    private String[] getStringValues(Page page, String propertyName) {
        ValueMap properties = page.getProperties();
        if (properties instanceof InheritanceValueMap) {
            LOGGER.debug("Page has inheritable value map");
            return (String[])((InheritanceValueMap)properties).getInherited(propertyName, String[].class);
        }
        LOGGER.debug("Page doe not have value map, wont check for inheritance.");
        return this.getValues((Node)page.getContentResource().adaptTo(Node.class), propertyName);
    }

    private String[] getValues(Node node, String propertyName) {
        block4 : {
            try {
                if (node == null || !node.hasProperty(propertyName)) break block4;
                javax.jcr.Property p = node.getProperty(propertyName);
                if (p.isMultiple()) {
                    Value[] v = p.getValues();
                    String[] vs = new String[v.length];
                    for (int i = 0; i < v.length; ++i) {
                        vs[i] = v[i].getString();
                    }
                    break block4;
                }
                return new String[]{p.getString()};
            }
            catch (RepositoryException e) {
                LOGGER.debug(e.getMessage(), (Throwable)e);
            }
        }
        return new String[0];
    }

    private Page getParentPage(Resource resource) {
        Page page = (Page)resource.adaptTo(Page.class);
        while (page == null && resource != null) {
            if ((resource = resource.getParent()) == null) continue;
            page = (Page)resource.adaptTo(Page.class);
        }
        return page;
    }

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

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