MobileClientCloudServiceAmazonSNSHandler.java 9.9 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
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  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.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.mobilecloudservices.impl;

import com.adobe.cq.mobile.mobilecloudservices.impl.AbstractMobileClientCloudServicePushHandler;
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.Calendar;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="cloudservice.id", value={"rps-amazon"})})
public class MobileClientCloudServiceAmazonSNSHandler
extends AbstractMobileClientCloudServicePushHandler {
    public static final String ID_AMAZONSNS_CLOUD_SERVICE = "rps-amazon";
    private static final String PROPERTY_ACCOUNT_ID = "accountId";
    private static final String PROPERTY_CONFIG_REGION = "configRegion";
    private static final String PROPERTY_IDENTITY_POOL = "identityPool";
    private static final String PROPERTY_ROLE_ARN = "roleARN";
    private static final String PROPERTY_AWS_ACCESS_KEY_ID = "AWSAccessKeyId";
    private static final String PROPERTY_AWS_ACCESS_KEY = "AWSAccessKey";
    private static final String PROPERTY_TOPIC_ARN = "topicARN";
    private static final String PROPERTY_IOS_PLATFORM_APP_ARN = "iosPlatformAppARN";
    private static final String PROPERTY_ANDROID_TOPIC_ARN = "androidPlatformAppARN";
    private static final String PROPERTY_ANDROID_GCM_ID = "gcmID";
    private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/rps-amazon";
    private static final String DEFAULT_RESOURCE_TYPE = "mobileapps/components/rps-amazon";
    private static final String DEFAULT_CQ_TEMPLATE = "/libs/mobileapps/templates/rps-amazon";
    private static final String DEFAULT_CONFIG_TITLE = "Provisioned Amazon SNS Configuration";
    private String[] mandatoryProperties = new String[]{"accountId", "configRegion", "identityPool", "roleARN", "AWSAccessKeyId", "AWSAccessKey", "topicARN"};
    @Reference
    private CryptoSupport cryptoSupport;
    private final Logger logger;

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

    @Override
    public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
        this.logger.info("Creating Amazon SNS Cloud Service Configuration...");
        this.checkProperties(properties, i18n);
        String pageTitle = this.getStringProperty(properties, "configName");
        if (StringUtils.isBlank((CharSequence)pageTitle)) {
            pageTitle = "Provisioned Amazon SNS Configuration";
        }
        String pageName = this.getName(properties, "rps-amazon");
        Page newConfiguration = this.createPGBuildConfiguration(pageName, properties, pageTitle, resourceResolver);
        this.saveConfigurationPublicNode((Resource)newConfiguration.adaptTo(Resource.class));
        return resourceResolver.getResource(newConfiguration.getPath());
    }

    @Override
    public void checkProperties(Map<String, Object> properties, I18n i18n) throws MobileAppException {
        String message;
        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);
        }
        String iosPlatformAppARN = this.getStringProperty(properties, "iosPlatformAppARN");
        String androidPlatformAppARN = this.getStringProperty(properties, "androidPlatformAppARN");
        String androidGCMID = this.getStringProperty(properties, "gcmID");
        if (StringUtils.isBlank((CharSequence)iosPlatformAppARN) && (StringUtils.isBlank((CharSequence)androidPlatformAppARN) || StringUtils.isBlank((CharSequence)androidGCMID))) {
            message = "All iOS fields Or all Android fields";
            if (i18n != null) {
                message = i18n.get(message);
            }
            missing.add(message);
        }
        if (!missing.isEmpty()) {
            message = "Missing mandatory parameters: ";
            if (i18n != null) {
                message = i18n.get(message);
            }
            message = message + StringUtils.join(missing, (String)", ");
            throw new MobileAppException(message);
        }
    }

    @Override
    public String getServicePath() {
        return "/etc/cloudservices/rps-amazon";
    }

    private Page createPGBuildConfiguration(String pageName, Map<String, Object> properties, String title, ResourceResolver resolver) throws MobileAppException {
        Page configPage;
        PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
        I18n i18n = (I18n)resolver.adaptTo(I18n.class);
        try {
            configPage = pm.create("/etc/cloudservices/rps-amazon", pageName, "/libs/mobileapps/templates/rps-amazon", title, true);
        }
        catch (WCMException wcmEx) {
            this.logger.error("Could not create Amazon SNS cloud configuration: " + pageName);
            throw new MobileAppException(i18n.get("Could not create Amazon SNS cloud configuration: {0}", "config name", new Object[]{pageName}));
        }
        Session session = (Session)resolver.adaptTo(Session.class);
        String userId = session.getUserID();
        Calendar now = Calendar.getInstance();
        String accountId = this.getStringProperty(properties, "accountId");
        String configRegion = this.getStringProperty(properties, "configRegion");
        String identityPool = this.getStringProperty(properties, "identityPool");
        String roleARN = this.getStringProperty(properties, "roleARN");
        String awsAccessKeyId = this.getStringProperty(properties, "AWSAccessKeyId");
        String awsAccessKey = this.getStringProperty(properties, "AWSAccessKey");
        String topicArn = this.getStringProperty(properties, "topicARN");
        String iosPlatformAppARN = this.getStringProperty(properties, "iosPlatformAppARN");
        String androidPlatformAppARN = this.getStringProperty(properties, "androidPlatformAppARN");
        String androidGCMID = this.getStringProperty(properties, "gcmID");
        ModifiableValueMap mvp = (ModifiableValueMap)configPage.getContentResource().adaptTo(ModifiableValueMap.class);
        try {
            this.putProperty(mvp, "accountId", accountId);
            this.putProperty(mvp, "configRegion", configRegion);
            this.putProperty(mvp, "identityPool", identityPool);
            this.putProperty(mvp, "roleARN", roleARN);
            this.putProperty(mvp, "AWSAccessKeyId", awsAccessKeyId);
            this.putProperty(mvp, "AWSAccessKey", this.cryptoSupport.protect(awsAccessKey));
            this.putProperty(mvp, "topicARN", topicArn);
            this.putProperty(mvp, "iosPlatformAppARN", iosPlatformAppARN);
            this.putProperty(mvp, "androidPlatformAppARN", androidPlatformAppARN);
            this.putProperty(mvp, "gcmID", androidGCMID);
            mvp.put((Object)"cq:cloudservicename", (Object)"rps-amazon");
            this.putProperty(mvp, "sling:resourceType", "mobileapps/components/rps-amazon");
            mvp.put((Object)"jcr:lastModified", (Object)now);
            this.putProperty(mvp, "jcr:lastModifiedBy", userId);
            mvp.put((Object)"jcr:lastModified", (Object)now);
            this.putProperty(mvp, "cq:lastModifiedBy", userId);
            if (session.hasPendingChanges()) {
                session.save();
            }
        }
        catch (CryptoException cryptoEx) {
            this.logger.error("Could not set the Amazon SNS cloud configuration password.");
            throw new MobileAppException(i18n.get("Could not set the Amazon SNS cloud configuration password."));
        }
        catch (RepositoryException repEx) {
            this.logger.error("Error saving properties on new cloud config: " + configPage.getPath());
            throw new MobileAppException(i18n.get("Error saving properties on new cloud config: {0}.", "path to config", new Object[]{configPage.getPath()}));
        }
        return configPage;
    }

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

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