VideoHandler.java 8.06 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.Rendition
 *  com.day.cq.dam.commons.util.DamUtil
 *  org.apache.commons.codec.DecoderException
 *  org.apache.commons.codec.net.URLCodec
 *  org.apache.commons.lang.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.commerce.impl.asset;

import com.adobe.cq.commerce.api.asset.ProductAssetHandler;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.commons.util.DamUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
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.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, metatype=1, label="Adobe CQ Commerce Product Asset Handler for Videos", description="Manages product assets of type: videos.")
@Service(value={ProductAssetHandler.class})
@Property(name="service.description", value={"Manages product assets of type: static videos"})
public class VideoHandler
implements ProductAssetHandler {
    private static final Logger log = LoggerFactory.getLogger(VideoHandler.class);
    static final boolean DEFAULT_ACTIVE = true;
    @Property(label="%Active", description="%Make this handler active.", boolValue={1})
    protected static final String CONFIG_PROPERTY_ACTIVE = "cq.commerce.asset.handler.active";
    static final String DEFAULT_HANDLER_NAME = "video";
    @Property(label="%Handler Name", description="%Name of the handler", value={"video"})
    private static final String CONFIG_PROPERTY_HANDLER_NAME = "cq.commerce.asset.handler.name";
    private static final String[] VID_MIME_TYPES = new String[]{"video/m4v", "video/flv", "video/avi", "video/mov", "video/3gpp", "application/x-troff-msvideo", "video/vnd", "model/vnd.mts", "video/ts", "video/vnd", "video/dvd", "video/x-ms-wmv", "video/msvideo", "video/x-msvideo", "video/x-flv", "video/mpeg", "video/x-mpeg", "video/x-m4v", "video/mpg", "video/x-mpg", "video/mpeg2", "video/x-mpeg2a", "video/mts", "video/x-ms-asf", "video/3gpp2", "video/x-f4v", "video/f4v", "video/m2p", "video/mp2t", "video/avchd-stream", "video/m2ts", "video/mp2t", "video/vnd.dlna.mpeg-tts", "video/m2v", "video/quicktime", "video/x-quicktime", "video/mp4", "video/ogg", "video/x-mxf", "application/mxf", "video/x-matroska", "video/mj2", "video/vnd.rn-realvideo", "application/vnd.rn-realmedia", "video/webm"};
    private static final List<String> MIME_TYPES_LIST = Arrays.asList(VID_MIME_TYPES);
    private static final String RES_TYPE = "foundation/components/video";
    private static final String PN_ASSET_REFERENCE = "asset";
    private static final int DEFAULT_THUMBNAIL_WIDTH = 319;
    private static final Map<String, Object> PRODUCT_ASSET_RENDERING_PROPERTIES;
    private boolean isActive;
    private String handlerName;

    @Activate
    protected void activate(ComponentContext context) throws Exception {
        this.isActive = OsgiUtil.toBoolean(context.getProperties().get("cq.commerce.asset.handler.active"), (boolean)true);
        this.handlerName = OsgiUtil.toString(context.getProperties().get("cq.commerce.asset.handler.name"), (String)"video");
    }

    @Override
    public boolean isActive() {
        return this.isActive;
    }

    @Override
    public String getHandlerName() {
        return this.handlerName;
    }

    @Override
    public Map<String, Object> getAssetProperties(ResourceResolver resolver, String assetReference) {
        HashMap<String, Object> props = new HashMap<String, Object>();
        props.put("asset", assetReference);
        props.putAll(PRODUCT_ASSET_RENDERING_PROPERTIES);
        return props;
    }

    @Override
    public String getReferencedAsset(Resource productAssetResource) {
        Asset asset = this.getAsset(productAssetResource);
        if (asset != null) {
            return asset.getPath();
        }
        return null;
    }

    @Override
    public boolean isSupportedReferencedAsset(ResourceResolver resolver, String assetReference) {
        Asset asset = this.getAsset(resolver, assetReference);
        return this.supports(asset);
    }

    @Override
    public boolean isSupportedAsset(Resource productAssetResource) {
        Asset asset = this.getAsset(productAssetResource);
        return this.supports(asset);
    }

    private boolean supports(Asset asset) {
        if (asset == null) {
            log.debug("The video asset is not defined.");
            return false;
        }
        String dcFormat = asset.getMetadataValue("dc:format");
        if (StringUtils.isEmpty((String)dcFormat)) {
            log.debug("The dc:format of the video asset is not defined.");
            return false;
        }
        return MIME_TYPES_LIST.contains(dcFormat);
    }

    @Override
    public String getThumbnailUrl(Resource productAssetResource, String selectorString) {
        String[] arrstring;
        Rendition bestFitRendition;
        List renditions;
        Asset asset = this.getAsset(productAssetResource);
        if (asset == null) {
            return null;
        }
        if (selectorString != null) {
            arrstring = selectorString.split("\\.");
        } else {
            String[] arrstring2 = new String[1];
            arrstring = arrstring2;
            arrstring2[0] = "";
        }
        String[] selectors = arrstring;
        int width = 319;
        for (String selector : selectors) {
            try {
                width = Integer.parseInt(selector);
                break;
            }
            catch (NumberFormatException nfe) {
                continue;
            }
        }
        if ((bestFitRendition = DamUtil.getBestFitRendition((int)width, (List)(renditions = asset.getRenditions()))) == null) {
            return null;
        }
        return bestFitRendition.getPath();
    }

    private Asset getAsset(Resource productAssetResource) {
        if (productAssetResource == null) {
            return null;
        }
        ValueMap props = (ValueMap)productAssetResource.adaptTo(ValueMap.class);
        String assetReference = (String)props.get("asset", (Object)"");
        return this.getAsset(productAssetResource.getResourceResolver(), assetReference);
    }

    private Asset getAsset(ResourceResolver resolver, String assetReference) {
        if (StringUtils.isEmpty((String)assetReference)) {
            log.debug("The video property {} is not defined", (Object)"asset");
            return null;
        }
        try {
            assetReference = new URLCodec().decode(assetReference);
        }
        catch (DecoderException e) {
            log.error("Error while decoding fileReference: {}", (Object)assetReference);
        }
        Resource assetRes = resolver.getResource(assetReference);
        if (assetRes == null) {
            return null;
        }
        return DamUtil.resolveToAsset((Resource)assetRes);
    }

    static {
        HashMap<String, String> tmp = new HashMap<String, String>();
        tmp.put("sling:resourceType", "foundation/components/video");
        PRODUCT_ASSET_RENDERING_PROPERTIES = Collections.unmodifiableMap(tmp);
    }
}