DefaultRenditionHandler.java 3 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.asset.api.AssetException
 *  com.adobe.granite.asset.api.Rendition
 *  com.adobe.granite.asset.api.RenditionHandler
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.ValueFactory
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 */
package com.adobe.granite.asset.core.impl;

import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.asset.api.RenditionHandler;
import com.adobe.granite.asset.core.impl.RenditionImpl;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=0, immediate=1)
@Properties(value={@Property(name="service.description", value={"Default JCR RenditionHandler"}), @Property(name="rendition.handler.id", value={"jcr.default"})})
@Service
public class DefaultRenditionHandler
implements RenditionHandler {
    public static final String ID = "jcr.default";

    public Rendition getRendition(Resource resource) {
        return new RenditionImpl(resource);
    }

    public Rendition setRendition(Resource resource, InputStream is, Map<String, Object> map) {
        if (is == null) {
            throw new AssetException("DefaultRenditionHandler cannot create rendition without binary stream");
        }
        String mimeType = (String)map.get("rendition.mime");
        try {
            Node content = ((Node)resource.adaptTo(Node.class)).getNode("jcr:content");
            this.setRenditionContent(content, is, mimeType);
            return this.getRendition(resource);
        }
        catch (RepositoryException e) {
            throw new AssetException((Throwable)e);
        }
    }

    public void deleteRendition(Resource resource) {
        try {
            ((Node)resource.adaptTo(Node.class)).remove();
        }
        catch (RepositoryException e) {
            throw new AssetException((Throwable)e);
        }
    }

    private void setRenditionContent(Node content, InputStream is, String mimeType) throws RepositoryException {
        content.setProperty("jcr:mimeType", mimeType);
        content.setProperty("jcr:data", content.getSession().getValueFactory().createBinary(is));
        content.setProperty("jcr:lastModified", Calendar.getInstance());
    }
}