AbstractMobileClientCloudServicePushHandler.java 9.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  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.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.cq.mobile.platform.impl.utils.MobileCloudServiceConfigsUtil;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import org.apache.commons.lang.StringUtils;
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;

public abstract class AbstractMobileClientCloudServicePushHandler
extends AbstractMobileClientCloudServiceHandler {
    public static final String PLUGIN_RESOURCE = "plugin";
    public static final String PLUGIN_NAME = "name";
    public static final String PLUGIN_SPEC = "spec";
    public static final String PLUGIN_IS_PUSH = "isPush";
    public static final String PLUGIN_TAG_ROOT = "/widget/plugins/";
    public static final String PN_CLOUD_CONFIG = "cloudConfig";
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractMobileClientCloudServicePushHandler.class);
    public static final String ERROR_CONFIG_VERIFICATION = "Configuration check failed.  Please check the logs for more detailed information";
    public static final String ERROR_PLUGIN_VERSIONS_MISSING = "Cloud config must contain a plugin folder with attributes for 'name' and 'spec'.  Push plugins must contain these two data fields, and one or more of them are missing.";
    private static final String ERROR_MISSING_JCR_CONTENT_NODE = "Unable to find the app's content resource.  Please check the logs for more detailed information.";
    private static final String ERROR_NOT_SAVED = "Unable to save plugin information to app config.  Please check the logs for more detailed information.";
    private static final String ERROR_PLUGIN_DIRECTIVE_NOT_DELETED = "Unable to remove plugin directive.  Please check the logs for more detailed information.";

    @Override
    public void saveCloudConfigProperty(Resource resource, String propertyName, String configPath) throws MobileAppException {
        ResourceResolver resolver = resource.getResourceResolver();
        MobileResource mobileResource = (MobileResource)resource.adaptTo(MobileResource.class);
        this.verifyConfigInfo(mobileResource, resolver, configPath);
        try {
            mobileResource = MobileUtil.getAppInstance(mobileResource);
        }
        catch (MobileOperationException e) {
            String message = "Unable to obtain instance (shell) of mobile resource " + resource.getPath();
            throw new MobileAppException(message);
        }
        Resource appInstance = resolver.getResource(mobileResource.getPath());
        MobileCloudServiceConfigsUtil.removePushCloudServices(appInstance);
        super.saveCloudConfigProperty(resource, propertyName, configPath);
        this.updatePushPluginInfo(appInstance, resolver, configPath);
    }

    private void verifyConfigInfo(MobileResource mobileResource, ResourceResolver resolver, String configPath) {
        if (!mobileResource.isA(MobileResourceType.INSTANCE.getType(), MobileResourceType.GROUP.getType(), MobileResourceType.CONTENT.getType())) {
            LOGGER.error("The resource " + mobileResource.getPath() + " must be a mobile resource.");
            throw new MobileAppException("Configuration check failed.  Please check the logs for more detailed information");
        }
        if (StringUtils.isEmpty((String)configPath)) {
            LOGGER.error("Missing mandatory parameter cloudConfig");
            throw new MobileAppException("Configuration check failed.  Please check the logs for more detailed information");
        }
        Resource configResource = resolver.resolve(configPath);
        if (ResourceUtil.isNonExistingResource((Resource)configResource)) {
            LOGGER.error("Unable to read " + configPath + ".  The path does not exist.");
            throw new MobileAppException("Configuration check failed.  Please check the logs for more detailed information");
        }
        Resource pluginResource = configResource.getParent().getChild("plugin");
        if (pluginResource == null || ResourceUtil.isNonExistingResource((Resource)pluginResource)) {
            LOGGER.error("Unable to read plugin information from child of {0}.  The plugin info is not where is should be.", (Object)configResource.getParent().getPath());
            throw new MobileAppException("Configuration check failed.  Please check the logs for more detailed information");
        }
    }

    private void updatePushPluginInfo(Resource appInstance, ResourceResolver resolver, String configPath) throws MobileAppException {
        Resource configResource = resolver.resolve(configPath);
        Resource pluginResource = configResource.getParent().getChild("plugin");
        ValueMap pluginInfo = pluginResource.getValueMap();
        String pluginName = (String)pluginInfo.get("name", String.class);
        String pluginSpec = (String)pluginInfo.get("spec", String.class);
        if (StringUtils.isEmpty((String)pluginName) || StringUtils.isEmpty((String)pluginSpec)) {
            throw new MobileAppException("Cloud config must contain a plugin folder with attributes for 'name' and 'spec'.  Push plugins must contain these two data fields, and one or more of them are missing.");
        }
        Resource appContent = appInstance.getChild("jcr:content");
        if (appContent == null) {
            LOGGER.error("Unable to find the app's content resource under {0}.  Internal error - an app instance should always have a jcr:content node.", (Object)appInstance.getPath());
            throw new MobileAppException("Unable to find the app's content resource.  Please check the logs for more detailed information.");
        }
        try {
            this.removePushPluginDirectives(resolver, appContent, "/widget/plugins/");
            String pluginPath = appContent.getPath() + "/widget/plugins/" + pluginName;
            ResourceUtil.getOrCreateResource((ResourceResolver)resolver, (String)pluginPath, (String)"nt:unstructured", (String)"nt:unstructured", (boolean)true);
            ModifiableValueMap pluginAttributes = (ModifiableValueMap)resolver.getResource(pluginPath).adaptTo(ModifiableValueMap.class);
            pluginAttributes.put((Object)"spec", (Object)pluginSpec);
            pluginAttributes.put((Object)"isPush", (Object)true);
        }
        catch (PersistenceException e) {
            LOGGER.error("Unable to save plugin information to app config at {0}.  The information could not be saved.  Permissions issues might cause this.", (Object)appContent.getPath());
            throw new MobileAppException("Unable to save plugin information to app config.  Please check the logs for more detailed information.");
        }
    }

    @Override
    public void unlinkCloudConfigProperty(Resource resource, String propertyName) throws MobileAppException {
        try {
            ResourceResolver resolver = resource.getResourceResolver();
            Resource jcrContent = resource.getChild("jcr:content");
            this.removePushPluginDirectives(resolver, jcrContent, "/widget/plugins/");
        }
        catch (PersistenceException e) {
            LOGGER.error("Unable to delete {0}.  {1}", (Object)resource.getPath(), (Object)e.getLocalizedMessage());
            throw new MobileAppException("Unable to remove plugin directive.  Please check the logs for more detailed information.");
        }
        super.unlinkCloudConfigProperty(resource, propertyName);
    }

    private void removePushPluginDirectives(ResourceResolver resolver, Resource jcrContent, String pluginParentFolder) throws PersistenceException {
        String relativeParentPluginFolder = pluginParentFolder.substring(1);
        Resource pushFolder = jcrContent.getChild(relativeParentPluginFolder);
        if (pushFolder == null) {
            return;
        }
        Iterable existingPlugins = pushFolder.getChildren();
        for (Resource plugin : existingPlugins) {
            Boolean isPushPlugin = (Boolean)plugin.getValueMap().get("isPush", (Object)false);
            if (!isPushPlugin.booleanValue()) continue;
            resolver.delete(plugin);
        }
        if (!resolver.hasChildren(pushFolder)) {
            resolver.delete(pushFolder);
        }
    }
}