AssetResourceProvider.java 12.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.AssetManager
 *  com.adobe.granite.asset.api.Rendition
 *  com.adobe.granite.comments.Comment
 *  com.adobe.granite.comments.CommentCollection
 *  com.adobe.granite.comments.CommentManager
 *  com.adobe.granite.rest.ApiResourceProvider
 *  javax.jcr.ItemExistsException
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang.StringUtils
 *  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.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.assets.impl;

import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentManager;
import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.assets.impl.AssetResource;
import com.adobe.granite.rest.assets.impl.AssetResourceFactory;
import com.adobe.granite.rest.assets.impl.CommentsResource;
import com.adobe.granite.rest.assets.impl.FileInputUtils;
import com.adobe.granite.rest.assets.impl.FolderResource;
import com.adobe.granite.rest.assets.impl.RenditionsResource;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import javax.jcr.ItemExistsException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AssetResourceProvider
implements ApiResourceProvider {
    private static final Logger log = LoggerFactory.getLogger(AssetResourceProvider.class);
    public static final String TYPE = "assets";
    public static final String DAM_ROOT = "/content/dam";
    private static final String PARAM_NAME = "name";
    private AssetResourceFactory resourceFactory = new AssetResourceFactory();
    private String apiContextPath;
    private CommentManager commentManager;

    public AssetResourceProvider(String contextPath, CommentManager commentManager) {
        this.commentManager = commentManager;
        this.apiContextPath = contextPath + "/" + "assets";
    }

    public Resource getResource(ResourceResolver resolver, String path) {
        Resource asset;
        log.debug("Asset getResource for path [" + path + "]");
        Resource collectionRoot = resolver.getResource("/content/dam");
        if (collectionRoot == null) {
            log.error("Collection root {} not found.", (Object)"/content/dam");
            return null;
        }
        if (StringUtils.isEmpty((String)path)) {
            return this.resourceFactory.createResource(collectionRoot, this.apiContextPath);
        }
        String resourcePath = "/content/dam" + this.unmap(path);
        log.debug("Resolving " + resourcePath + " relative to " + collectionRoot.getPath());
        Resource requestedResource = resolver.getResource(resourcePath);
        if (requestedResource == null && resourcePath.endsWith("comments") && (asset = resolver.getResource(ResourceUtil.getParent((String)resourcePath, (int)2))) != null) {
            this.commentManager.getOrCreateCollection(asset, CommentCollection.class);
            requestedResource = resolver.getResource(resourcePath);
        }
        log.debug("Resolved to " + (Object)requestedResource);
        if (requestedResource != null) {
            return this.resourceFactory.createResource(requestedResource, this.apiContextPath + path);
        }
        return null;
    }

    public Iterator<Resource> listChildren(Resource parent) {
        log.debug("Asset listChildren of parent [" + parent.getPath() + "]");
        String parentPath = parent.getPath();
        if (!StringUtils.isEmpty((String)parentPath)) {
            ResourceResolver resolver = parent.getResourceResolver();
            Resource collectionRoot = resolver.getResource("/content/dam");
            if (collectionRoot == null) {
                log.error("Collection root {} not found.", (Object)"/content/dam");
                return null;
            }
            Resource parentResource = resolver.getResource(this.unmap(parentPath));
            if (parentResource == null) {
                return null;
            }
            LinkedList<Resource> children = new LinkedList<Resource>();
            Iterator it = parentResource.listChildren();
            while (it.hasNext()) {
                Resource child = (Resource)it.next();
                Resource childRes = this.resourceFactory.createResource(child, this.map(child.getPath()));
                if (childRes == null) continue;
                children.add(childRes);
            }
            return children.iterator();
        }
        return null;
    }

    public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
        log.debug("Asset create [" + path + "]");
        if (!StringUtils.isEmpty((String)path)) {
            Asset asset;
            Resource assetResource;
            Resource collectionRoot = resolver.getResource("/content/dam");
            if (collectionRoot == null) {
                throw new PersistenceException("Collection root not found for " + path);
            }
            Resource parent = resolver.getResource(ResourceUtil.getParent((String)(this.apiContextPath + path)));
            if (parent == null) {
                log.error("Could not find parent resource for {}.", (Object)path);
                throw new PersistenceException("Cound not find parent at " + path);
            }
            AssetManager assetManager = (AssetManager)resolver.adaptTo(AssetManager.class);
            Resource resource = resolver.getResource(this.apiContextPath + path);
            String dataParameter = AssetResourceProvider.getDataPropertyKey(properties);
            String name = properties.containsKey("name") ? (String)properties.get("name") : null;
            String resourcePath = AssetResourceProvider.getResourcePath(path, name == null ? "" : name);
            if (parent instanceof FolderResource) {
                if (dataParameter != null) {
                    if (assetManager.assetExists(resourcePath)) {
                        throw new PersistenceException("Asset already exists at " + path, (Throwable)new ItemExistsException("Resource already exists at " + path));
                    }
                    Asset asset2 = assetManager.createAsset(resourcePath);
                    if (asset2 != null) {
                        this.addRendition(asset2, "original", FileInputUtils.getFileInputStream(properties.get(dataParameter)), FileInputUtils.getRenditionMap(properties.get(dataParameter), "rendition.mime"));
                        return this.resourceFactory.createResource((Resource)asset2, this.map(asset2.getPath()));
                    }
                    log.error("Could not create Asset for {} at {}.", (Object)path, (Object)resourcePath);
                    throw new PersistenceException("Cound not create Asset at " + path);
                }
                Resource root = resolver.getResource(ResourceUtil.getParent((String)resourcePath));
                if (name == null) {
                    name = ResourceUtil.getName((String)resourcePath);
                }
                resource = resolver.create(root, name, properties);
                return this.resourceFactory.createResource(resource, this.map(resource.getPath()));
            }
            if (parent instanceof RenditionsResource) {
                String parentAssetPath = resourcePath.substring(0, resourcePath.indexOf("/rendition"));
                Asset asset3 = assetManager.getAsset(parentAssetPath);
                if (asset3 != null) {
                    if (name == null) {
                        name = resource != null ? resource.getName() : path.substring(path.lastIndexOf("/") + 1);
                    }
                    Rendition rendition = this.addRendition(asset3, name, FileInputUtils.getFileInputStream(properties.get(dataParameter)), FileInputUtils.getRenditionMap(properties.get(dataParameter), "rendition.mime"));
                    return this.resourceFactory.createResource((Resource)rendition, this.map(rendition.getPath()));
                }
                log.error("Could not find Asset for {} at {}.", (Object)path, (Object)parentAssetPath);
                throw new PersistenceException("Cound not find Asset at " + path);
            }
            if (parent instanceof CommentsResource && (asset = (Asset)resolver.getResource(((AssetResource)(assetResource = parent.getParent())).getWrappedResourcePath()).adaptTo(Asset.class)) != null) {
                String message = (String)properties.get("message");
                String author = resolver.getUserID();
                String annotationData = (String)properties.get("annotationData");
                CommentCollection commentCollection = this.commentManager.getOrCreateCollection((Resource)asset, CommentCollection.class);
                Comment comment = commentCollection.addComment(message, author, annotationData);
                if (comment != null) {
                    Resource commentResource = resolver.getResource(comment.getPath());
                    return this.resourceFactory.createResource(commentResource, this.map(commentResource.getPath()));
                }
            }
        }
        throw new PersistenceException("Unable to create resource at " + path);
    }

    public void delete(ResourceResolver resolver, String path) throws PersistenceException {
        log.debug("Asset delete [" + path + "]");
        if (!StringUtils.isEmpty((String)path)) {
            Resource collectionRoot = resolver.getResource("/content/dam");
            if (collectionRoot == null) {
                log.error("Collection root not found for {} at {}", (Object)path, (Object)"/content/dam");
                throw new PersistenceException("Collection root not found for " + path);
            }
            Resource res = resolver.getResource("/content/dam" + this.unmap(path));
            if (res != null) {
                resolver.delete(res);
                return;
            }
        }
        throw new PersistenceException("Unable to delete resource at " + path);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private Rendition addRendition(Asset asset, String renditionName, InputStream is, Map<String, Object> renditionProperties) {
        if (asset != null) {
            try {
                Rendition rendition = asset.setRendition(renditionName, is, renditionProperties);
                return rendition;
            }
            finally {
                IOUtils.closeQuietly((InputStream)is);
            }
        }
        return null;
    }

    static String getResourcePath(String resourcePath, String name) {
        String path;
        String string = path = resourcePath.endsWith("*") ? resourcePath.replace("*", name) : resourcePath;
        if (!path.startsWith("/content/dam/")) {
            return "/content/dam" + path;
        }
        return path;
    }

    String map(String resourcePath) {
        String path = resourcePath.replace("/content/dam", this.apiContextPath);
        if (path != null && path.matches(".*?/jcr:content/(renditions|comments|folderThumbnail).*")) {
            path = path.replaceFirst("jcr:content/(renditions|comments|folderThumbnail)", "$1");
        }
        return path;
    }

    String unmap(String apiPath) {
        String path = apiPath.replace(this.apiContextPath, "/content/dam");
        if (path != null && path.matches(".*?/(renditions|comments|folderThumbnail).*")) {
            return path.replaceFirst("(renditions|comments|folderThumbnail)", "jcr:content/$1");
        }
        return path;
    }

    static String getDataPropertyKey(Map<String, Object> properties) {
        for (Map.Entry<String, Object> e : properties.entrySet()) {
            if ("jcr:data".equals(e.getKey()) || !(e.getValue() instanceof Map)) continue;
            return e.getKey();
        }
        return null;
    }
}