MobileUtil.java 6.36 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.cq.commons.Externalizer
 *  javax.jcr.Session
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.platform.impl.utils;

import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.commons.Externalizer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MobileUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private static final Logger LOGGER = LoggerFactory.getLogger(MobileUtil.class);
    public static final String OAUTH_CLIENT_ID = "oauth:clientId";
    public static final String OAUTH_CLIENT_SECRET = "oauth:clientSecret";
    public static final String OAUTH_CLIENT_REDIRECT = "oauth:redirectURI";

    public static String getDateAsString(Date date) {
        String stringDate = "";
        if (date != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            stringDate = formatter.format(date);
        }
        return stringDate;
    }

    public static ResourceResolver getResourceResolver(ResourceResolverFactory resourceResolverFactory, Session session) {
        ResourceResolver resolver = null;
        HashMap<String, Session> authInfo = new HashMap<String, Session>();
        authInfo.put("user.jcr.session", session);
        try {
            resolver = resourceResolverFactory.getResourceResolver(authInfo);
        }
        catch (LoginException ex) {
            LOGGER.error("Unable to obtain the resource resolver", (Throwable)ex);
        }
        return resolver;
    }

    public static String getPubishServerURL(ResourceResolver resourceResolver, MobileResource shellMobileResource) {
        Resource shellResource = (Resource)shellMobileResource.adaptTo(Resource.class);
        Resource shellJCRContentResource = shellResource.getChild("jcr:content");
        String publishServer = (String)shellJCRContentResource.getValueMap().get("serverURL", (Object)"");
        if (publishServer.isEmpty()) {
            Externalizer externalizer = (Externalizer)resourceResolver.adaptTo(Externalizer.class);
            publishServer = externalizer.publishLink(resourceResolver, "");
        }
        return publishServer;
    }

    public static Map<String, String> getOAuthClient(CryptoSupport cryptoSupport, ResourceResolver resourceResolver, MobileResource shellMobileResource) {
        String path = (String)shellMobileResource.getProperties().get("oauthClient", String.class);
        if (StringUtils.isNotBlank((CharSequence)path)) {
            Resource oauthClient = resourceResolver.getResource(path);
            ValueMap props = oauthClient.getValueMap();
            String clientId = (String)props.get("oauth:clientId", String.class);
            String clientSecret = (String)props.get("oauth:clientSecret", String.class);
            String redirectURI = (String)props.get("oauth:redirectURI", String.class);
            try {
                if (cryptoSupport.isProtected(clientSecret)) {
                    clientSecret = cryptoSupport.unprotect(clientSecret);
                }
            }
            catch (Exception e) {
                LOGGER.error("Unable to decode the client secret for the oauth client " + path);
                return null;
            }
            HashMap<String, String> result = new HashMap<String, String>();
            result.put("clientId", clientId);
            result.put("clientSecret", clientSecret);
            result.put("redirectURI", redirectURI);
            return result;
        }
        return null;
    }

    public static MobileResource getAppInstance(MobileResource mobileResource) throws NoSuchElementException, MobileOperationException {
        MobileResource appInstance;
        if (mobileResource == null) {
            throw new MobileOperationException("No resource provided for application instance lookup.");
        }
        if (mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
            return mobileResource;
        }
        Resource resource = (Resource)mobileResource.adaptTo(Resource.class);
        MobileResourceLocator locator = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
        MobileResource group = mobileResource;
        if (!mobileResource.isA(MobileResourceType.GROUP.getType())) {
            group = locator.findAncestorResourceByType(resource, MobileResourceType.GROUP.getType(), new String[0]);
        }
        Iterable<MobileResource> resources = locator.findResourcesByType((Resource)group.adaptTo(Resource.class), MobileResourceType.INSTANCE.getType());
        try {
            appInstance = resources.iterator().next();
        }
        catch (NoSuchElementException e) {
            LOGGER.error("Unable to find App Instance for " + group.getPath());
            throw e;
        }
        return appInstance;
    }

    public static String normalizePath(String path, Resource source) {
        if (StringUtils.isBlank((CharSequence)path) || source == null) {
            return path;
        }
        if (path.startsWith(".")) {
            path = source.getPath() + "/" + path;
            path = ResourceUtil.normalize((String)path);
        }
        return path;
    }
}