AssetManagerImpl.java 4.3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.Asset
 *  com.adobe.granite.asset.api.AssetException
 *  com.adobe.granite.asset.api.AssetManager
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.jcr.resource.JcrResourceUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.asset.core.impl;

import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.core.impl.AssetUtils;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrResourceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AssetManagerImpl
implements AssetManager {
    private static final Logger log = LoggerFactory.getLogger(AssetManager.class);
    private final ResourceResolver resolver;
    private final Session session;

    public AssetManagerImpl(ResourceResolver resolver) {
        this.resolver = resolver;
        this.session = (Session)resolver.adaptTo(Session.class);
    }

    public Asset createAsset(String path) {
        if (this.assetExists(path)) {
            log.warn("Asset already existing at the given path {}. Aborting", (Object)path);
            throw new AssetException("Asset at path " + path + " exists.");
        }
        try {
            Node assetNode = JcrResourceUtil.createPath((String)path, (String)"sling:OrderedFolder", (String)"dam:Asset", (Session)this.session, (boolean)false);
            Node content = assetNode.addNode("jcr:content", "dam:AssetContent");
            content.addNode("metadata", "nt:unstructured");
            content.addNode("renditions", "nt:folder");
            content.addNode("related", "nt:unstructured");
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to create Asset at path [ " + path + " ]", (Throwable)e);
        }
        return (Asset)this.resolver.getResource(path).adaptTo(Asset.class);
    }

    public Asset getAsset(String assetPath) {
        Resource resource = this.resolver.getResource(assetPath);
        if (resource != null) {
            return (Asset)resource.adaptTo(Asset.class);
        }
        return null;
    }

    public Asset getAssetByIdentifier(String id) {
        try {
            Node node = this.session.getNodeByIdentifier(id);
            Resource resource = this.resolver.getResource(node.getPath());
            if (resource != null) {
                return (Asset)resource.adaptTo(Asset.class);
            }
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to get Asset with ID [ " + id + " ]", (Throwable)e);
        }
        return null;
    }

    public boolean assetExists(String assetPath) {
        return this.getAsset(assetPath) != null;
    }

    public void removeAsset(String assetPath) {
        try {
            this.session.removeItem(assetPath);
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to remove Asset [ " + assetPath + " ]", (Throwable)e);
        }
    }

    public void copyAsset(String assetPath, String destAbsPath) {
        try {
            String name = StringUtils.substringAfterLast((String)destAbsPath, (String)"/");
            String destAbsParent = StringUtils.substringBeforeLast((String)destAbsPath, (String)"/");
            Node nodeToCopy = this.session.getNode(assetPath);
            Node destinationParent = this.session.getNode(destAbsParent);
            AssetUtils.copy(nodeToCopy, destinationParent, name);
        }
        catch (RepositoryException e) {
            throw new AssetException((Throwable)e);
        }
    }

    public void moveAsset(String assetPath, String destAbsPath) {
        try {
            this.session.move(assetPath, destAbsPath);
        }
        catch (RepositoryException e) {
            throw new AssetException((Throwable)e);
        }
    }
}