MobileClientCloudServiceTargetHandler.java 12.7 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  org.apache.commons.lang3.StringUtils
 *  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.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.mobilecloudservices.impl;

import com.adobe.cq.mobile.mobilecloudservices.impl.AbstractMobileClientCloudServiceHandler;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
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.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="cloudservice.id", value={"testandtarget"})})
public class MobileClientCloudServiceTargetHandler
extends AbstractMobileClientCloudServiceHandler {
    public static final String ID_TARGET_CLOUD_SERVICE = "testandtarget";
    private static final String PROPERTY_CLIENTCODE = "target.clientcode";
    private static final String PROPERTY_EMAIL = "target.email";
    private static final String PROPERTY_PASSWORD = "target.password";
    private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/testandtarget";
    private static final String DEFAULT_FRAMEWORK_NAME = "framework";
    private static final String DEFAULT_CONFIG_TEMPLATE = "/libs/cq/analytics/templates/testandtarget";
    private static final String DEFAULT_CONFIG_TITLE = "Provisioned Mobile Target Configuration";
    private static final String SEGMENTS_POLL_CONFIG_NAME = "segmentsPollConfig";
    private static final String PUBLIC_NAME = "public";
    private static final String DEFAULT_FRAMEWORK_TEMPLATE = "/libs/cq/analytics/templates/tntframework";
    private static final String DEFAULT_FRAMEWORK_TITLE = "Provisioned Mobile Target Framework";
    private static final String MAPPINGS_SLING_RT = "cq/analytics/components/mappings/maparsys";
    private static final String CQMAPPINGS_SLING_RT = "cq/analytics/components/mappings/cbmappings";
    private final String[] mandatoryProperties = new String[]{"target.clientcode", "target.email", "target.password"};
    @Reference
    private CryptoSupport cryptoSupport;
    private final Logger logger;

    public MobileClientCloudServiceTargetHandler() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
        this.logger.info("Creating Target Cloud Service Configuration...");
        this.checkProperties(properties, i18n);
        try {
            String pageName;
            String clientCode = this.getStringProperty(properties, "target.clientcode");
            String pageTitle = this.getStringProperty(properties, "configName");
            if (StringUtils.isBlank((CharSequence)pageTitle)) {
                pageTitle = "Provisioned Mobile Target Configuration: " + clientCode;
            }
            if (this.isSetup(resourceResolver, pageName = this.getName(properties, clientCode))) {
                this.logger.debug("Found provisioned configuration for Target, skipping creation.");
                return null;
            }
            Page newConfiguration = this.createTargetConfiguration(pageName, properties, pageTitle, resourceResolver);
            return resourceResolver.getResource(newConfiguration.getPath());
        }
        catch (Exception e) {
            this.logger.error("Creation of Target Cloud Service Configuration failed: ", (Object)e.getMessage());
            throw new MobileAppException("Creation of Target Cloud Service Configuration failed: " + e.getMessage(), e);
        }
    }

    @Override
    public void checkProperties(Map<String, Object> properties, I18n i18n) throws MobileAppException {
        ArrayList<String> missing = new ArrayList<String>(this.mandatoryProperties.length + 1);
        for (String nextMandatoryProperty : this.mandatoryProperties) {
            String value = this.getStringProperty(properties, nextMandatoryProperty);
            if (!StringUtils.isBlank((CharSequence)value)) continue;
            missing.add(nextMandatoryProperty);
        }
        if (!missing.isEmpty()) {
            String message = "Missing mandatory parameters: ";
            if (i18n != null) {
                message = i18n.get(message);
            }
            message = message + StringUtils.join(missing, (String)", ");
            this.logger.error(message);
            throw new MobileAppException(message);
        }
    }

    @Override
    public String getServicePath() {
        return "/etc/cloudservices/testandtarget";
    }

    private Page createTargetConfiguration(String pageName, Map<String, Object> properties, String pageTitle, ResourceResolver resolver) throws MobileAppException {
        Page clientCodePage = this.getOrCreateConfigurationPage(pageName, properties, pageTitle, resolver);
        this.createFramework(resolver, clientCodePage);
        return clientCodePage;
    }

    private Page getOrCreateConfigurationPage(String pageName, Map<String, Object> properties, String pageTitle, ResourceResolver resolver) throws MobileAppException {
        try {
            Resource content;
            PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
            Page companyPage = pageManager.getPage("/etc/cloudservices/testandtarget/" + pageName);
            if (companyPage == null && (content = (companyPage = pageManager.create("/etc/cloudservices/testandtarget", pageName, "/libs/cq/analytics/templates/testandtarget", pageTitle)).getContentResource()) != null) {
                Resource publicConfig;
                ModifiableValueMap vm = (ModifiableValueMap)content.adaptTo(ModifiableValueMap.class);
                vm.put((Object)"apiKind", (Object)"REST");
                vm.put((Object)"email", (Object)this.getStringProperty(properties, "target.email"));
                String password = this.getStringProperty(properties, "target.password");
                vm.put((Object)"password", (Object)this.cryptoSupport.protect(password));
                Resource segmentsPollConfig = resolver.getResource(content, "segmentsPollConfig");
                if (segmentsPollConfig == null) {
                    HashMap<String, Object> pollConfigProps = new HashMap<String, Object>();
                    pollConfigProps.put("jcr:mixinTypes", new String[]{"cq:PollConfig"});
                    pollConfigProps.put("enabled", true);
                    pollConfigProps.put("interval", 60);
                    pollConfigProps.put("source", "adobe-target-segments:" + companyPage.getPath());
                    pollConfigProps.put("target", "/etc/segmentation/adobe-target/" + this.getStringProperty(properties, "target.clientcode"));
                    resolver.create(content, "segmentsPollConfig", pollConfigProps);
                }
                if ((publicConfig = resolver.getResource(content, "public")) == null) {
                    HashMap<String, String> publicProps = new HashMap<String, String>();
                    publicProps.put("clientcode", this.getStringProperty(properties, "target.clientcode"));
                    publicProps.put("jcr:title", pageTitle);
                    resolver.create(content, "public", publicProps);
                }
            }
            return companyPage;
        }
        catch (CryptoException e) {
            this.logger.error("Unable to encrypt secret.", (Throwable)e);
            throw new MobileAppException("Unable to encrypt secret", (Throwable)e);
        }
        catch (WCMException e) {
            this.logger.error("Unable to create configuration.", (Throwable)e);
            throw new MobileAppException("Unable to create configuration", (Throwable)e);
        }
        catch (PersistenceException e) {
            this.logger.error("Unable to create the segments poll configuration.", (Throwable)e);
            throw new MobileAppException("Unable to create the segments poll configuration", (Throwable)e);
        }
    }

    private Page createFramework(ResourceResolver resourceResolver, Page parentPage) throws MobileAppException {
        try {
            Resource content;
            PageManager pageManager = (PageManager)resourceResolver.adaptTo(PageManager.class);
            Page frameworkPage = pageManager.create(parentPage.getPath(), "framework", "/libs/cq/analytics/templates/tntframework", "Provisioned Mobile Target Framework");
            if (frameworkPage != null && (content = frameworkPage.getContentResource()) != null) {
                Resource mappings = ResourceUtil.getOrCreateResource((ResourceResolver)resourceResolver, (String)(content.getPath() + "/" + "public" + "/mappings"), (String)"cq/analytics/components/mappings/maparsys", (String)"cq/analytics/components/mappings/maparsys", (boolean)false);
                if (mappings != null && !ResourceUtil.isNonExistingResource((Resource)mappings)) {
                    HashMap<String, String> vm = new HashMap<String, String>();
                    vm.put("sling:resourceType", "cq/analytics/components/mappings/cbmappings");
                    vm.put("jcr:primaryType", "cq:Component");
                    vm.put("cq:componentPath", "cq/personalization/components/contextstores/profiledata");
                    vm.put("cq:componentName", "Profile Data");
                    vm.put("cq:componentIcon", "/libs/cq/ui/widgets/themes/default/icons/16x16/components.png");
                    vm.put("profile.age", "profile.age");
                    vm.put("profile.gender", "profile.gender");
                    ResourceUtil.getOrCreateResource((ResourceResolver)resourceResolver, (String)(mappings.getPath() + "/cq_personalization_components_contextstores_profiledata"), vm, (String)"cq/analytics/components/mappings/cbmappings", (boolean)true);
                }
                Resource publicRes = content.getChild("public");
                ModifiableValueMap mvp = (ModifiableValueMap)resourceResolver.getResource(publicRes.getPath()).adaptTo(ModifiableValueMap.class);
                mvp.put((Object)"jcr:title", content.getValueMap().get("jcr:title", String.class));
            }
            return frameworkPage;
        }
        catch (PersistenceException e) {
            this.logger.error("Unable to create mapping.", (Throwable)e);
            throw new MobileAppException("Unable to create mapping", (Throwable)e);
        }
        catch (WCMException e) {
            this.logger.error("Unable to create framework.", (Throwable)e);
            throw new MobileAppException("Unable to create framework", (Throwable)e);
        }
    }

    private boolean isSetup(ResourceResolver resourceResolver, String pageName) {
        String defaultPath = "/etc/cloudservices/testandtarget";
        defaultPath = defaultPath + "/" + pageName;
        defaultPath = defaultPath + "/framework";
        return resourceResolver.getResource(defaultPath = defaultPath + "/jcr:content/mappings/cq_personalization_components_contextstores_profiledata") != null;
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }
}