CFMUtils.java 9.76 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.dam.cfm.VersionDef
 *  com.adobe.cq.dam.cfm.VersionedContent
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.api.Revision
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.version.Version
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.io.input.ReaderInputStream
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.resource.collection.ResourceCollection
 *  org.apache.sling.resource.collection.ResourceCollectionManager
 */
package com.adobe.cq.dam.cfm.impl;

import com.adobe.cq.dam.cfm.VersionDef;
import com.adobe.cq.dam.cfm.VersionedContent;
import com.adobe.cq.dam.cfm.impl.FragmentWriteException;
import com.adobe.cq.dam.cfm.impl.VersionDefImpl;
import com.adobe.cq.dam.cfm.impl.VersionedContentImpl;
import com.adobe.cq.dam.cfm.impl.VersioningException;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.Revision;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.version.Version;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
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.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;

public class CFMUtils {
    private CFMUtils() {
    }

    public static String getContentAsString(Rendition rendition) {
        return CFMUtils.getContentAsString(rendition.getStream());
    }

    public static String getContentAsString(Resource renditionContent) {
        ValueMap map = (ValueMap)renditionContent.adaptTo(ValueMap.class);
        InputStream is = (InputStream)map.get("jcr:data", InputStream.class);
        return is != null ? CFMUtils.getContentAsString(is) : null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    private static String getContentAsString(InputStream is) {
        int curChar;
        String content = null;
        Reader reader = null;
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder contentBuf = new StringBuilder();
        do {
            if ((curChar = reader.read()) == -1) continue;
            contentBuf.append((char)curChar);
        } while (curChar != -1);
        content = contentBuf.toString();
        try {
            if (reader == null) return content;
            reader.close();
            return content;
        }
        catch (IOException ioe) {
            return content;
        }
        catch (IOException ioe) {
            try {
                if (reader == null) return content;
                reader.close();
                return content;
            }
            catch (IOException ioe) {
                return content;
            }
            catch (Throwable throwable) {
                try {
                    if (reader == null) throw throwable;
                    reader.close();
                    throw throwable;
                }
                catch (IOException ioe) {
                    // empty catch block
                }
                throw throwable;
            }
        }
    }

    public static String getContentType(Rendition rendition) {
        return rendition.getMimeType();
    }

    public static String getContentType(Resource renditionContent) {
        ValueMap map = (ValueMap)renditionContent.adaptTo(ValueMap.class);
        return (String)map.get("jcr:mimeType", String.class);
    }

    public static String removeExtension(String name) {
        if (name == null) {
            return null;
        }
        int extPos = name.lastIndexOf(".");
        if (extPos > 0) {
            name = name.substring(0, extPos);
        }
        return name;
    }

    public static Calendar getRenditionLastModified(Rendition rendition) {
        ValueMap props = rendition.getProperties();
        Calendar lastModified = (Calendar)props.get("jcr:lastModified", Calendar.class);
        if (lastModified == null) {
            lastModified = (Calendar)props.get("jcr:created", Calendar.class);
        }
        return lastModified;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static Rendition setContent(Rendition rendition, String content, String contentType) throws FragmentWriteException {
        Asset asset = rendition.getAsset();
        boolean oldBatchMode = asset.isBatchMode();
        asset.setBatchMode(true);
        String renditionName = rendition.getName();
        ReaderInputStream is = new ReaderInputStream((Reader)new StringReader(content), "UTF-8");
        try {
            Rendition newRendition = asset.addRendition(renditionName, (InputStream)is, contentType);
            if (newRendition == null) {
                throw new FragmentWriteException("Could not change content of rendition '" + renditionName);
            }
            Rendition rendition2 = newRendition;
            return rendition2;
        }
        finally {
            IOUtils.closeQuietly((InputStream)is);
            asset.setBatchMode(oldBatchMode);
        }
    }

    public static String getRevPath(Revision rev, String rendition, String subPath) throws RepositoryException {
        return rev.getVersion().getFrozenNode().getPath() + (subPath != null ? new StringBuilder().append("/").append(subPath).toString() : "") + "/jcr:content/renditions/" + rendition + "/jcr:content";
    }

    public static long getRevTc(ResourceResolver resolver, Revision rev, String rendition, String subPath) throws RepositoryException {
        String versionedPath = CFMUtils.getRevPath(rev, rendition, subPath);
        Resource versioned = resolver.getResource(versionedPath);
        ValueMap revProps = (ValueMap)versioned.adaptTo(ValueMap.class);
        Calendar revTime = revProps.containsKey((Object)"jcr:lastModified") ? (Calendar)revProps.get("jcr:lastModified", Calendar.class) : (Calendar)revProps.get("jcr:created", Calendar.class);
        return revTime != null ? revTime.getTimeInMillis() : -1;
    }

    public static Iterator<VersionDef> listVersions(Asset mainAsset, long lastModified, String renditionName, String subPath) throws VersioningException {
        ArrayList<VersionDefImpl> versions = new ArrayList<VersionDefImpl>();
        try {
            Collection revisions = mainAsset.getRevisions(null);
            ResourceResolver resolver = ((Resource)mainAsset.adaptTo(Resource.class)).getResourceResolver();
            long changeTc = lastModified;
            for (Revision rev : revisions) {
                long revTc = CFMUtils.getRevTc(resolver, rev, renditionName, subPath);
                if (revTc == changeTc) continue;
                changeTc = revTc;
                versions.add(new VersionDefImpl(rev));
            }
        }
        catch (Exception e) {
            throw new VersioningException("Could not retrieve list of revisions.", e);
        }
        return versions.iterator();
    }

    public static VersionedContent getVersionedContent(VersionDef version, Asset mainAsset, String renditionName, String subPath) throws VersioningException {
        String versionName = null;
        String content = null;
        String contentType = null;
        try {
            String identifier = version.getIdentifier();
            Collection revisions = mainAsset.getRevisions(null);
            ResourceResolver resolver = ((Resource)mainAsset.adaptTo(Resource.class)).getResourceResolver();
            for (Revision rev : revisions) {
                if (!rev.getVersion().getIdentifier().equals(identifier)) continue;
                String path = CFMUtils.getRevPath(rev, renditionName, subPath);
                Resource versionedRendition = resolver.getResource(path);
                if (versionedRendition == null) {
                    throw new VersioningException("No versioned rendition found at '" + path + "'.");
                }
                content = CFMUtils.getContentAsString(versionedRendition);
                contentType = CFMUtils.getContentType(versionedRendition);
            }
        }
        catch (Exception e) {
            if (!(e instanceof VersioningException)) {
                if (versionName != null) {
                    throw new VersioningException("Could get content of revision '" + versionName + "'.", e);
                }
                throw new VersioningException("Could not access version definition.", e);
            }
            throw (VersioningException)((Object)e);
        }
        return new VersionedContentImpl(content, contentType);
    }

    public static ResourceCollection createEmptyAssocContent(Resource assetResource, ResourceCollectionManager collectionManager) throws PersistenceException {
        HashMap<String, String> collProps = new HashMap<String, String>();
        collProps.put("jcr:primaryType", "nt:unstructured");
        return collectionManager.createCollection(assetResource, "associated", collProps);
    }
}