AppInstanceProviderImpl.java 11.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Externalizer
 *  com.day.cq.contentsync.config.Config
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  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.phonegap.impl;

import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.appcache.impl.CacheUpdate;
import com.adobe.cq.mobile.platform.impl.AbstractMobileAppProvider;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.cq.mobile.platform.impl.MobileAppProvider;
import com.adobe.cq.mobile.platform.impl.WidgetConfigDocument;
import com.day.cq.commons.Externalizer;
import com.day.cq.contentsync.config.Config;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
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;
import org.w3c.dom.Element;

public class AppInstanceProviderImpl
extends AbstractMobileAppProvider
implements MobileAppProvider {
    private static final Logger log = LoggerFactory.getLogger(AppInstanceProviderImpl.class);
    private static final String DEFAULT_ICON_PATH = "/libs/mobileapps/gui/components/dashboard/tiles/info/thumbnail/icon.png";
    private static final String NN_CONFIG_XML = "config.xml";
    private static final String NN_STORE = "store";
    private final AppCacheManagerAdapterFactory appCacheManagerAdapterFactory;
    private final Resource contentRes;
    private ValueMap properties;
    private final List<String> appPlatforms;

    public AppInstanceProviderImpl(Resource resource, AppCacheManagerAdapterFactory appCacheManagerAdapterFactory, List<String> appPlatforms) {
        super(resource);
        if (appPlatforms == null) {
            appPlatforms = new ArrayList<String>();
        }
        this.appPlatforms = appPlatforms;
        this.appCacheManagerAdapterFactory = appCacheManagerAdapterFactory;
        this.contentRes = this.getContentResource();
        this.properties = null;
    }

    @Override
    public ValueMap getProperties() {
        Config defaultConfig;
        if (this.properties != null) {
            return this.properties;
        }
        ValueMap properties = super.getProperties();
        if (!this.addWidgetConfigProperties(properties)) {
            try {
                properties.clear();
                properties.putAll((Map)super.getProperties());
                properties.put((Object)"widgetConfigPath", (Object)"");
            }
            catch (UnsupportedOperationException ex) {
                // empty catch block
            }
        }
        this.addResourceProperties(this.contentRes.getChild("store"), properties, "store");
        String serverURL = (String)properties.get("serverURL", String.class);
        if (StringUtils.isEmpty((CharSequence)serverURL)) {
            Externalizer externalizer = (Externalizer)this.resolver.adaptTo(Externalizer.class);
            properties.put((Object)"serverURL", (Object)externalizer.publishLink(this.resolver, "/"));
        }
        if ((defaultConfig = this.getDefaultConfig()) != null) {
            AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter(this.resolver.adaptTo(Session.class), AppCacheManager.class);
            CacheUpdate update = appCacheManager.getLastUpdate(defaultConfig);
            long updateTime = 0;
            try {
                if (update != null) {
                    updateTime = update.getCreated().getTimeInMillis();
                }
            }
            catch (Exception e) {
                log.error("Unable to determine update time for {}", (Object)defaultConfig.getPath());
            }
            properties.put((Object)"updateTimestamp", (Object)updateTime);
        }
        this.addRPSProperties(properties);
        return properties;
    }

    private boolean addWidgetConfigProperties(ValueMap properties) {
        Resource configRes = this.findWidgetConfigResource((String)properties.get("widgetConfigPath", String.class));
        if (configRes == null) {
            log.debug("Unable to locate widget config resource.");
            return true;
        }
        try {
            WidgetConfigDocument widgetConfig = WidgetConfigDocument.loadConfigDocument(configRes.getPath(), this.contentRes);
            if (widgetConfig != null) {
                Resource iconRes;
                properties.put((Object)"widgetConfigPath", (Object)configRes.getPath());
                properties.put((Object)"widget/id", (Object)widgetConfig.getId());
                properties.put((Object)"widget/version", (Object)widgetConfig.getVersion());
                properties.put((Object)"widget/name/text", (Object)widgetConfig.getName());
                properties.put((Object)"widget/description/text", (Object)widgetConfig.getDescription());
                Map<String, String> author = widgetConfig.getAuthor();
                if (author != null) {
                    properties.put((Object)"widget/author/text", (Object)author.get("value"));
                    properties.put((Object)"widget/author/href", (Object)author.get("href"));
                    properties.put((Object)"widget/author/email", (Object)author.get("email"));
                }
                properties.put((Object)"widget/preferences/phonegap-version/value", (Object)widgetConfig.getPreferenceValue("phonegap-version"));
                properties.put((Object)"widget/access/origin", (Object)widgetConfig.getAccessOrigin());
                properties.put((Object)"widget/content/src", (Object)widgetConfig.getContentSource());
                List<String> platforms = widgetConfig.getPlatforms();
                if (platforms.isEmpty()) {
                    platforms = this.appPlatforms;
                }
                String[] p = platforms.toArray(new String[platforms.size()]);
                properties.put((Object)"widget/platforms", (Object)p);
                properties.put((Object)"widget/plugins", widgetConfig.getPlugins());
                Element iconElement = widgetConfig.getDefaultIconElement();
                if (iconElement != null) {
                    properties.put((Object)"widget/icon/src", (Object)iconElement.getAttribute("src"));
                    properties.put((Object)"widget/icon/fileReference", (Object)iconElement.getAttribute("fileReference"));
                }
                if ((iconRes = this.findIconResource(widgetConfig)) != null) {
                    properties.put((Object)"iconPath", (Object)iconRes.getPath());
                }
                return true;
            }
        }
        catch (MobileAppException e) {
            log.error("Unable to add widget config properties", (Throwable)e);
        }
        return false;
    }

    public Resource getWidgetResource() {
        return this.contentRes.getChild("widget");
    }

    private Resource findContentResource(String basePath, String childPath) {
        Resource baseResource = this.contentRes.getChild(basePath);
        if (baseResource != null) {
            Resource childContentResource = baseResource.getChild(childPath);
            if ((childContentResource == null || ResourceUtil.isNonExistingResource((Resource)childContentResource)) && !"www".equals(baseResource.getName())) {
                Resource r;
                Iterable allChildren = baseResource.getChildren();
                Iterator i$ = allChildren.iterator();
                while (i$.hasNext() && ("jcr:content".equals((r = (Resource)i$.next()).getName()) || (childContentResource = this.findContentResource(r.getPath(), childPath)) == null || ResourceUtil.isNonExistingResource((Resource)childContentResource))) {
                }
            }
            return childContentResource;
        }
        return null;
    }

    private Resource findWidgetConfigResource(String path) {
        Resource configRes = null;
        if (StringUtils.isNotEmpty((CharSequence)path) && (configRes = this.resolver.getResource(path)) != null) {
            return configRes;
        }
        String appContentPath = (String)this.contentRes.getValueMap().get("appAssetPath", String.class);
        if (StringUtils.isNotEmpty((CharSequence)appContentPath)) {
            configRes = this.findContentResource(appContentPath, "config.xml");
        }
        if (configRes == null || ResourceUtil.isNonExistingResource((Resource)configRes)) {
            configRes = this.findContentResource("pge-app/app-content/phonegap", "config.xml");
        }
        return configRes;
    }

    private Resource findIconResource(WidgetConfigDocument configDoc) {
        try {
            String iconSrc = configDoc.getDefaultIconPath();
            if (StringUtils.isNotBlank((CharSequence)iconSrc)) {
                Resource iconRes = this.resolver.getResource(iconSrc);
                if (iconRes == null) {
                    String appContentPath = (String)this.contentRes.getValueMap().get("appAssetPath", String.class);
                    iconRes = this.findContentResource(appContentPath, iconSrc);
                }
                if (iconRes == null) {
                    iconRes = this.findContentResource("pge-app/app-content/images", iconSrc);
                }
                if (iconRes == null) {
                    iconRes = this.findContentResource("pge-app/app-content/phonegap", iconSrc);
                }
                if (iconRes != null) {
                    return iconRes;
                }
            }
        }
        catch (MobileAppException e) {
            log.error("Unable to find an icon for this mobile resource.", (Throwable)e);
        }
        return this.resolver.getResource("/libs/mobileapps/gui/components/dashboard/tiles/info/thumbnail/icon.png");
    }

    private ValueMap addRPSProperties(ValueMap instanceVM) {
        Resource rpsCloudService;
        String notificationConfig;
        if (instanceVM.containsKey((Object)"notificationConfig") && !StringUtils.isEmpty((CharSequence)(notificationConfig = (String)instanceVM.get("notificationConfig", String.class))) && !ResourceUtil.isNonExistingResource((Resource)(rpsCloudService = this.resolver.resolve(notificationConfig)))) {
            this.addResourceProperties(rpsCloudService.getChild("jcr:content"), instanceVM, "rps");
            Resource rpsCloudParentContent = rpsCloudService.getParent().getChild("jcr:content");
            ValueMap parentContentVM = rpsCloudParentContent.getValueMap();
            String pushServiceId = (String)parentContentVM.get("pushServiceId", String.class);
            if (pushServiceId != null) {
                instanceVM.put((Object)"rps/pushServiceId", (Object)pushServiceId);
            }
        }
        return instanceVM;
    }

    private void addResourceProperties(Resource res, ValueMap props, String namespace) {
        if (res != null) {
            if (namespace == null) {
                namespace = res.getName();
            }
            ValueMap resProps = res.getValueMap();
            for (Map.Entry entry : resProps.entrySet()) {
                if (((String)entry.getKey()).contains(":")) continue;
                props.put((Object)(namespace + "/" + (String)entry.getKey()), entry.getValue());
            }
        }
    }
}