LaunchUtils.java 6.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.day.text.Text
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.utils;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.day.text.Text;
import java.util.LinkedList;
import java.util.ListIterator;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LaunchUtils {
    private static final Logger log = LoggerFactory.getLogger(LaunchUtils.class);
    private static final int LAUNCH_NAME_LEVEL = 2;

    public static Resource getLaunchResource(Resource resource) {
        if (resource == null) {
            throw new IllegalArgumentException("Resource cannot be null");
        }
        while (resource != null) {
            Resource contentRes = resource.getChild("jcr:content");
            if (contentRes != null && contentRes.isResourceType("wcm/launches/components/launch")) {
                return resource;
            }
            resource = resource.getParent();
        }
        return null;
    }

    public static Resource getProductionResource(Resource resource) {
        if (!LaunchUtils.isLaunchResourcePath(resource.getPath())) {
            return resource;
        }
        Resource launchResource = LaunchUtils.getLaunchResource(resource);
        return resource.getResourceResolver().getResource(resource.getPath().substring(launchResource.getPath().length()));
    }

    public static Resource getTargetResource(Resource resource, Launch target) throws LaunchException {
        String path = LaunchUtils.getTargetResourcePath(resource, target);
        return resource.getResourceResolver().getResource(path);
    }

    public static String getTargetResourcePath(Resource resource, Launch target) throws LaunchException {
        if (!LaunchUtils.isLaunchResourcePath(resource.getPath())) {
            return target == null ? resource.getPath() : null;
        }
        Launch currentLaunch = (Launch)LaunchUtils.getLaunchResource(resource).adaptTo(Launch.class);
        String path = resource.getPath();
        while (target == null || currentLaunch.compareTo(target) != 0) {
            path = path.substring(currentLaunch.getResource().getPath().length());
            Resource launchResource = LaunchUtils.getLaunchResource(currentLaunch.getSourceRootResource());
            if (launchResource == null) {
                if (target == null) break;
                throw new LaunchException("The specified target launch is not a parent launch of the resource's launch");
            }
            currentLaunch = (Launch)launchResource.adaptTo(Launch.class);
        }
        return path;
    }

    public static Resource getLaunchResource(Launch launch, Resource resource) throws LaunchException {
        if (launch == null || resource == null) {
            return null;
        }
        String path = LaunchUtils.getLaunchResourcePath(launch, resource);
        return resource.getResourceResolver().getResource(path);
    }

    public static String getLaunchResourcePath(Launch launch, Resource resource) throws LaunchException {
        if (launch == null || resource == null) {
            return null;
        }
        Resource res = LaunchUtils.getLaunchResource(resource);
        Launch resourceLaunch = res == null ? null : (Launch)res.adaptTo(Launch.class);
        LinkedList<Launch> parents = new LinkedList<Launch>();
        Launch currentLaunch = launch;
        while (resourceLaunch == null || currentLaunch.compareTo(resourceLaunch) != 0) {
            parents.add(currentLaunch);
            Resource launchResource = LaunchUtils.getLaunchResource(currentLaunch.getSourceRootResource());
            if (launchResource == null) {
                if (resourceLaunch == null) break;
                throw new LaunchException("The specified launch is not a child launch of the resource's launch");
            }
            currentLaunch = (Launch)launchResource.adaptTo(Launch.class);
        }
        StringBuilder path = new StringBuilder(resource.getPath());
        ListIterator reverseIterator = parents.listIterator(parents.size());
        while (reverseIterator.hasPrevious()) {
            path.insert(0, ((Launch)reverseIterator.previous()).getResource().getPath());
        }
        return path.toString();
    }

    public static boolean isLaunchBasedPath(String path) {
        return Text.isDescendantOrEqual((String)"/content/launches", (String)path);
    }

    static boolean isLaunchBasedPath(String path, String launchPath) {
        return Text.isDescendantOrEqual((String)launchPath, (String)path);
    }

    public static boolean isLaunchResourcePath(String path) {
        return LaunchUtils.isLaunchBasedPath(path) && StringUtils.isNotEmpty((String)Text.getAbsoluteParent((String)path, (int)3));
    }

    public static String getLaunchPath(String path) {
        log.warn("LaunchUtils.getLaunchPath is deprecated and potentially broken. Please remove usages of this method.");
        if (LaunchUtils.isLaunchBasedPath(path)) {
            String launchPath = Text.getAbsoluteParent((String)path, (int)2);
            return StringUtils.isNotEmpty((String)launchPath) ? launchPath : null;
        }
        return null;
    }

    public static String getLaunchPathFromName(String launchName) {
        return Text.makeCanonicalPath((String)("/content/launches/" + launchName));
    }

    public static String getProductionResourcePath(String path) {
        return LaunchUtils.isLaunchResourcePath(path) ? StringUtils.substringAfter((String)path, (String)LaunchUtils.getLaunchPath(path)) : path;
    }

    public static String buildLaunchResourcePath(String path, String launchName) {
        log.warn("LaunchUtils#buildLaunchResourcePath is deprecated and potentially broken. Please remove usages of this method.");
        if (LaunchUtils.isLaunchResourcePath(path)) {
            return LaunchUtils.isLaunchBasedPath(path, LaunchUtils.getLaunchPathFromName(launchName)) ? path : null;
        }
        return !LaunchUtils.isLaunchBasedPath(path) ? Text.makeCanonicalPath((String)("/content/launches/" + launchName + "/" + path)) : null;
    }
}