AssetImpl.java 15.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.AssetMetadata
 *  com.adobe.granite.asset.api.AssetRelation
 *  com.adobe.granite.asset.api.Rendition
 *  com.adobe.granite.asset.api.RenditionHandler
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 *  org.apache.commons.lang.ArrayUtils
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  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.apache.sling.api.resource.ResourceWrapper
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 *  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.AssetMetadata;
import com.adobe.granite.asset.api.AssetRelation;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.asset.api.RenditionHandler;
import com.adobe.granite.asset.core.impl.AssetRelationImpl;
import com.adobe.granite.asset.core.impl.AssetWrapper;
import com.adobe.granite.asset.core.impl.RenditionUtils;
import com.adobe.granite.asset.core.impl.TransientJcrPropertyMap;
import com.adobe.granite.asset.core.impl.ValueMapCreatingResourceOnDemand;
import com.adobe.granite.asset.core.impl.metadata.AssetMetadataImpl;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
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.apache.sling.api.resource.ResourceWrapper;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class AssetImpl
extends ResourceWrapper
implements Asset {
    private static final Logger log = LoggerFactory.getLogger(AssetImpl.class);
    private final ResourceResolver resolver;
    private final AssetWrapper assetWrapper;
    private final AssetMetadata assetMetadata;
    private final Session session;
    private final ModifiableValueMap valueMap;

    public AssetImpl(final Resource resource) {
        super(resource);
        this.resolver = resource.getResourceResolver();
        this.session = (Session)this.resolver.adaptTo(Session.class);
        this.assetWrapper = new AssetWrapper(resource);
        this.valueMap = new ValueMapCreatingResourceOnDemand(){

            public Resource getResource() {
                return resource.getResourceResolver().getResource(resource, "jcr:content");
            }

            public Resource createResource() throws PersistenceException {
                return resource.getResourceResolver().create(resource, "jcr:content", null);
            }

            public ValueMap getValueMap(Resource resource2) {
                return new TransientJcrPropertyMap(resource2);
            }
        };
        this.assetMetadata = new AssetMetadataImpl(this.assetWrapper);
    }

    private ResourceCollectionManager getResourceCollectionManager() {
        return (ResourceCollectionManager)this.resolver.adaptTo(ResourceCollectionManager.class);
    }

    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
        if (ValueMap.class == type) {
            return (AdapterType)this.valueMap;
        }
        if (type == AssetMetadata.class) {
            return (AdapterType)this.assetMetadata;
        }
        return (AdapterType)super.adaptTo(type);
    }

    public Rendition setRendition(String name, InputStream is, Map<String, Object> map) {
        String handlerId = RenditionUtils.getRenditionHandlerId(map, "jcr.default");
        RenditionHandler rh = RenditionUtils.getRenditionHandler(handlerId);
        try {
            Resource renditionsResource = this.assetWrapper.getOrCreateRenditionsResource();
            Resource rendition = this.setRenditionResource(name, renditionsResource, handlerId);
            return rh.setRendition(rendition, is, map);
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to set Rendition [ " + name + "] under " + this.getPath(), (Throwable)e);
        }
    }

    public void removeRendition(String name) {
        Resource renditionsResource;
        try {
            renditionsResource = this.assetWrapper.getOrCreateRenditionsResource();
        }
        catch (RepositoryException e) {
            throw new AssetException("Cannot create renditions folder", (Throwable)e);
        }
        Resource rendition = this.resolver.getResource(renditionsResource, name);
        if (rendition == null) {
            throw new AssetException("Rendition with name [ " + name + " ] does not exist");
        }
        String handlerId = RenditionUtils.getRenditionHandlerId(rendition, "jcr.default");
        RenditionHandler rh = RenditionUtils.getRenditionHandler(handlerId);
        rh.deleteRendition(rendition);
    }

    public String getName() {
        return ResourceUtil.getName((String)this.getPath());
    }

    public Rendition getRendition(String name) {
        return this.assetWrapper.getRendition(name);
    }

    public Iterator<Rendition> listRenditions() {
        return this.assetWrapper.listRenditions();
    }

    public Iterator<Asset> listRelated(String name) {
        return this.assetWrapper.listRelated(name);
    }

    public String getIdentifier() {
        try {
            return ((Node)this.getResource().adaptTo(Node.class)).getIdentifier();
        }
        catch (RepositoryException e) {
            log.warn("Failed to get Asset identifier", (Throwable)e);
            return null;
        }
    }

    public AssetRelation addRelation(String name, String assetPath, Map<String, Object> map) {
        Asset asset = this.getAsset(assetPath);
        if (asset == null) {
            throw new AssetException("Failed to set related asset, Asset at path [ " + assetPath + " ] does not exist");
        }
        try {
            ResourceCollection rc;
            Resource related = this.assetWrapper.getOrCreateRelatedResource();
            Node node = (Node)related.adaptTo(Node.class);
            if (node.hasProperty(name)) {
                Property p = node.getProperty(name);
                Object[] values = p.getValues();
                values = (Value[])ArrayUtils.add((Object[])values, (Object)asset.getPath());
                p.setValue((Value[])values);
                return new AssetRelationImpl(name, asset);
            }
            Resource relationRes = related.getChild(name);
            ResourceCollectionManager rcm = this.getResourceCollectionManager();
            if (relationRes == null) {
                HashMap<String, String> props = new HashMap<String, String>();
                props.put("jcr:primaryType", "nt:unstructured");
                rc = rcm.createCollection(related, name, props);
            } else {
                rc = rcm.getCollection(relationRes);
            }
            if (!rc.add(this.resolver.getResource(assetPath), map)) {
                String message = MessageFormat.format("Asset {0} already has relation to {1}", this.getPath(), assetPath);
                throw new AssetException(message);
            }
            return new AssetRelationImpl(name, asset, rc.getProperties(this.resolver.getResource(assetPath)));
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to set related asset", (Throwable)e);
        }
        catch (PersistenceException e) {
            throw new AssetException("Failed to set related asset", (Throwable)e);
        }
    }

    public void removeRelation(String name, String assetPath) {
        Asset asset = this.getAsset(assetPath);
        if (asset == null) {
            throw new AssetException("Failed to remove related asset, Asset at path [ " + assetPath + " ] does not exist");
        }
        try {
            Resource related = this.assetWrapper.getOrCreateRelatedResource();
            Node node = (Node)related.adaptTo(Node.class);
            Resource relationRes = related.getChild(name);
            if (relationRes != null) {
                ResourceCollection rc = this.assetWrapper.getCollection(relationRes);
                rc.remove((Resource)asset);
            } else if (node.hasProperty(name)) {
                Property p = node.getProperty(name);
                Object[] values = p.getValues();
                Object[] newValues = (Value[])ArrayUtils.removeElement((Object[])values, (Object)this.session.getValueFactory().createValue(asset.getPath()));
                if (newValues.length == values.length) {
                    newValues = (Value[])ArrayUtils.removeElement((Object[])newValues, (Object)this.session.getValueFactory().createValue(asset.getIdentifier()));
                }
                p.setValue((Value[])newValues);
            }
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to remove related asset", (Throwable)e);
        }
        catch (PersistenceException e) {
            throw new AssetException("Failed to remove related asset", (Throwable)e);
        }
    }

    public void removeRelation(String name) {
        try {
            Resource related = this.assetWrapper.getOrCreateRelatedResource();
            Node node = (Node)related.adaptTo(Node.class);
            if (node.hasNode(name)) {
                ResourceCollectionManager rcm = this.getResourceCollectionManager();
                rcm.deleteCollection(related.getChild(name));
            } else {
                node.getProperty(name).remove();
            }
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to remove relation", (Throwable)e);
        }
        catch (PersistenceException e) {
            throw new AssetException("Failed to remove relation", (Throwable)e);
        }
    }

    public AssetMetadata getAssetMetadata() {
        return this.assetMetadata;
    }

    private Resource setRenditionResource(String name, Resource parent, String handlerId) throws RepositoryException {
        Node content;
        Node parentNode = (Node)parent.adaptTo(Node.class);
        if (parentNode.hasNode(name)) {
            content = parentNode.getNode(name + "/" + "jcr:content");
        } else {
            if (StringUtils.isEmpty((String)handlerId)) {
                log.warn("Handler ID is null, using ID [{}]", (Object)"jcr.default");
                handlerId = "jcr.default";
            }
            if (StringUtils.equals((String)handlerId, (String)"jcr.default")) {
                Node file = parentNode.addNode(name, "nt:file");
                content = file.addNode("jcr:content", "nt:resource");
            } else {
                Node folder = parentNode.addNode(name, "sling:OrderedFolder");
                content = folder.addNode("jcr:content", "nt:unstructured");
                content.setProperty("rendition.handler.id", handlerId);
            }
        }
        return this.resolver.getResource(content.getParent().getPath());
    }

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

    @Deprecated
    public void setRelation(String name, String assetPath) {
        this.addRelation(name, assetPath);
    }

    public AssetRelation addRelation(String name, String assetPath) {
        return this.addRelation(name, assetPath, null);
    }

    public void orderRelationBefore(String name, String srcAssetPath, String destAssetPath) {
        if (srcAssetPath == null) {
            throw new IllegalArgumentException("Source Asset Path can not be null");
        }
        try {
            Resource related = this.assetWrapper.getOrCreateRelatedResource();
            Resource relationRes = related.getChild(name);
            if (relationRes != null) {
                ResourceCollection rc = this.assetWrapper.getCollection(relationRes);
                Resource destResource = null;
                if (destAssetPath != null) {
                    destResource = this.resolver.getResource(destAssetPath);
                }
                rc.orderBefore(this.resolver.getResource(srcAssetPath), destResource);
            } else {
                ModifiableValueMap mvm = (ModifiableValueMap)related.adaptTo(ModifiableValueMap.class);
                Object[] refs = (String[])mvm.get(name, (Object)new String[0]);
                int srcIndex = ArrayUtils.indexOf((Object[])refs, (Object)srcAssetPath);
                if (srcIndex < 0) {
                    log.warn("there is no related resource {} in asset {}", (Object)srcAssetPath, (Object)this.getPath());
                    return;
                }
                if (destAssetPath == null) {
                    refs = (String[])ArrayUtils.remove((Object[])refs, (int)srcIndex);
                    refs = (String[])ArrayUtils.add((Object[])refs, (Object)srcAssetPath);
                } else {
                    if (destAssetPath.equals(srcAssetPath)) {
                        String message = MessageFormat.format("Relation ordering failed, as source {} and destination {} can not be same", srcAssetPath, destAssetPath);
                        log.error(message);
                        throw new IllegalArgumentException(message);
                    }
                    int destIndex = ArrayUtils.indexOf((Object[])refs, (Object)destAssetPath);
                    if (destIndex < 0) {
                        log.warn("Relation ordering failed, as there is no asset {} in relation {} for destAssetPath", (Object)destAssetPath, (Object)this.getPath());
                        return;
                    }
                    refs = (String[])ArrayUtils.remove((Object[])refs, (int)srcIndex);
                    if (srcIndex < destIndex) {
                        destIndex = ArrayUtils.indexOf((Object[])refs, (Object)destAssetPath);
                    }
                    refs = (String[])ArrayUtils.add((Object[])refs, (int)destIndex, (Object)srcAssetPath);
                }
            }
        }
        catch (RepositoryException e) {
            throw new AssetException("Failed to reorder related asset", (Throwable)e);
        }
    }

    public Iterator<AssetRelation> listRelations(String name) {
        return this.assetWrapper.listRelations(name);
    }

}