MobileClientCloudServicePGBuildHandler.java 7.63 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.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.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={"phonegap-build"})})
public class MobileClientCloudServicePGBuildHandler
extends AbstractMobileClientCloudServiceHandler {
    public static final String ID_PGBUILD_CLOUD_SERVICE = "phonegap-build";
    private static final String PROPERTY_PG_BUILD_ID = "pgBuildUserId";
    private static final String PROPERTY_PG_BUILD_PASSWORD = "pgBuildPassword";
    private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/phonegap-build";
    private static final String DEFAULT_RESOURCE_TYPE = "mobileapps/components/phonegap/phonegap-build-cloudservice-config";
    private static final String DEFAULT_CQ_TEMPLATE = "/libs/mobileapps/templates/phonegap/phonegap-build-cloudservice-config";
    private static final String DEFAULT_CONFIG_TITLE = "Provisioned PhoneGap Build Configuration";
    private String[] mandatoryProperties = new String[]{"pgBuildUserId", "pgBuildPassword"};
    @Reference
    private CryptoSupport cryptoSupport;
    private final Logger logger;

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

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

    private Page createPGBuildConfiguration(String pageName, Map<String, Object> properties, String title, ResourceResolver resolver) throws MobileAppException {
        Page pgBuildPage;
        PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
        I18n i18n = (I18n)resolver.adaptTo(I18n.class);
        try {
            pgBuildPage = pm.create("/etc/cloudservices/phonegap-build", pageName, "/libs/mobileapps/templates/phonegap/phonegap-build-cloudservice-config", title, true);
        }
        catch (WCMException wcmEx) {
            this.logger.error("Could not create PG Build cloud configuration: " + pageName);
            throw new MobileAppException(i18n.get("Could not create PG Build cloud configuration: {0}", "config name", new Object[]{pageName}));
        }
        Session session = (Session)resolver.adaptTo(Session.class);
        String userId = session.getUserID();
        Calendar now = Calendar.getInstance();
        String password = this.getStringProperty(properties, "pgBuildPassword");
        String user = this.getStringProperty(properties, "pgBuildUserId");
        ModifiableValueMap mvp = (ModifiableValueMap)pgBuildPage.getContentResource().adaptTo(ModifiableValueMap.class);
        try {
            mvp.put((Object)"pgBuildPassword", (Object)this.cryptoSupport.protect(password));
            mvp.put((Object)"pgBuildUserId", (Object)user);
            mvp.put((Object)"sling:resourceType", (Object)"mobileapps/components/phonegap/phonegap-build-cloudservice-config");
            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)"phonegap-build");
            if (session.hasPendingChanges()) {
                session.save();
            }
        }
        catch (CryptoException cryptoEx) {
            this.logger.error("Could not set the PG Build cloud configuration password.");
            throw new MobileAppException(i18n.get("Could not set the PG Build cloud configuration password."));
        }
        catch (RepositoryException repEx) {
            this.logger.error("Error saving properties on new cloud config: " + pgBuildPage.getPath());
            throw new MobileAppException(i18n.get("Error saving properties on new cloud config: {0}", "path to config", new Object[]{pgBuildPage.getPath()}));
        }
        return pgBuildPage;
    }

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

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