MobileClientCloudServicePushwooshHandler.java 7.67 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-pushwoosh"})})
public class MobileClientCloudServicePushwooshHandler
extends AbstractMobileClientCloudServicePushHandler {
    public static final String ID_PW_CLOUD_SERVICE = "rps-pushwoosh";
    private static final String PROPERTY_PW_APP_ID = "appID";
    private static final String PROPERTY_PW_ACCESS_TOKEN = "appAPIAccessToken";
    private static final String PROPERTY_PW_GCM_ID = "gcmID";
    private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/rps-pushwoosh";
    private static final String DEFAULT_RESOURCE_TYPE = "mobileapps/components/rps-pushwoosh";
    private static final String DEFAULT_CQ_TEMPLATE = "/libs/mobileapps/templates/rps-pushwoosh";
    private static final String DEFAULT_CONFIG_TITLE = "Provisioned Pushwoosh Configuration";
    private String[] mandatoryProperties = new String[]{"appID", "appAPIAccessToken", "gcmID"};
    @Reference
    private CryptoSupport cryptoSupport;
    private final Logger logger;

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

    @Override
    public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
        this.logger.info("Creating Pushwoosh Cloud Service Configuration...");
        this.checkProperties(properties, i18n);
        String pageTitle = this.getStringProperty(properties, "configName");
        if (StringUtils.isBlank((CharSequence)pageTitle)) {
            pageTitle = "Provisioned Pushwoosh Configuration";
        }
        String pageName = this.getName(properties, "Provisioned Pushwoosh Configuration");
        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 {
        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)", ");
            throw new MobileAppException(message);
        }
    }

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

    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-pushwoosh", pageName, "/libs/mobileapps/templates/rps-pushwoosh", title, true);
        }
        catch (WCMException wcmEx) {
            this.logger.error("Could not create Pushwoosh cloud configuration: " + pageName);
            throw new MobileAppException(i18n.get("Could not create Pushwoosh cloud configuration: {0}", "config name", new Object[]{pageName}));
        }
        Session session = (Session)resolver.adaptTo(Session.class);
        String userId = session.getUserID();
        Calendar now = Calendar.getInstance();
        String appId = this.getStringProperty(properties, "appID");
        String accessToken = this.getStringProperty(properties, "appAPIAccessToken");
        String gcmId = this.getStringProperty(properties, "gcmID");
        ModifiableValueMap mvp = (ModifiableValueMap)configPage.getContentResource().adaptTo(ModifiableValueMap.class);
        try {
            mvp.put((Object)"appID", (Object)appId);
            mvp.put((Object)"appAPIAccessToken", (Object)this.cryptoSupport.protect(accessToken));
            mvp.put((Object)"gcmID", (Object)gcmId);
            mvp.put((Object)"sling:resourceType", (Object)"mobileapps/components/rps-pushwoosh");
            mvp.put((Object)"jcr:lastModified", (Object)now);
            mvp.put((Object)"jcr:lastModifiedBy", (Object)userId);
            mvp.put((Object)"cq:lastModified", (Object)now);
            mvp.put((Object)"cq:lastModifiedBy", (Object)userId);
            mvp.put((Object)"cq:cloudservicename", (Object)"rps-pushwoosh");
            if (session.hasPendingChanges()) {
                session.save();
            }
        }
        catch (CryptoException cryptoEx) {
            this.logger.error("Could not set the Pushwoosh cloud configuration password.");
            throw new MobileAppException(i18n.get("Could not set the Pushwoosh 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;
        }
    }
}