DynamicImageHandler.java 8.52 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.dam.api.Asset
 *  com.day.cq.dam.api.s7dam.set.MediaSet
 *  com.day.cq.dam.api.s7dam.set.SpinSet
 *  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.s7dam.set.MediaSet;
import com.day.cq.dam.api.s7dam.set.SpinSet;
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.Iterator;
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 Dynamic Images", description="Manages product assets of type: dynamic images.")
@Service(value={ProductAssetHandler.class})
@Property(name="service.description", value={"Manages product assets of type: dynamic images"})
public class DynamicImageHandler
implements ProductAssetHandler {
    private static final Logger log = LoggerFactory.getLogger(DynamicImageHandler.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 = "dynamic-image";
    @Property(label="%Handler Name", description="%Name of the handler", value={"dynamic-image"})
    private static final String CONFIG_PROPERTY_HANDLER_NAME = "cq.commerce.asset.handler.name";
    private static final String SPIN_SET_MIME_TYPE = "Multipart/Related; type=application/x-SpinSet";
    private static final String IMAGE_SET_MIME_TYPE = "Multipart/Related; type=application/x-ImageSet";
    private static final String MEDIA_SET_MIME_TYPE = "Multipart/Related; type=application/x-MixedMediaSet";
    private static final String[] MIME_TYPES = new String[]{"Multipart/Related; type=application/x-SpinSet", "Multipart/Related; type=application/x-ImageSet", "Multipart/Related; type=application/x-MixedMediaSet"};
    private static final List<String> MIME_TYPES_LIST = Arrays.asList(MIME_TYPES);
    private static final String RES_TYPE = "dam/components/scene7/dynamicmediaimage";
    private static final String PN_ASSET_REFERENCE = "fileReference";
    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)"dynamic-image");
    }

    @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("fileReference", 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 dynamic image is not defined.");
            return false;
        }
        String dcFormat = asset.getMetadataValue("dc:format");
        if (StringUtils.isEmpty((String)dcFormat)) {
            log.debug("The dc:format of the dynamic image is not defined.");
            return false;
        }
        return MIME_TYPES_LIST.contains(dcFormat);
    }

    @Override
    public String getThumbnailUrl(Resource productAssetResource, String selectorString) {
        if (productAssetResource == null) {
            log.debug("The product asset is not defined.");
            return null;
        }
        Asset asset = this.getAsset(productAssetResource);
        if (asset == null) {
            log.debug("The dynamic image is not defined.");
            return null;
        }
        Resource assetRes = productAssetResource.getResourceResolver().resolve(asset.getPath());
        String dcFormat = asset.getMetadataValue("dc:format");
        if ("Multipart/Related; type=application/x-SpinSet".equals(dcFormat)) {
            SpinSet spinSet = (SpinSet)assetRes.adaptTo(SpinSet.class);
            Asset firstImage = spinSet.getAsset(0, 0);
            return firstImage.getPath();
        }
        if ("Multipart/Related; type=application/x-ImageSet".equals(dcFormat) || "Multipart/Related; type=application/x-MixedMediaSet".equals(dcFormat)) {
            MediaSet mediaSet = (MediaSet)assetRes.adaptTo(MediaSet.class);
            Iterator setImages = mediaSet.getMembers();
            Asset firstImage = (Asset)setImages.next();
            return firstImage.getPath();
        }
        return null;
    }

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

    private Asset getAsset(ResourceResolver resolver, String assetReference) {
        if (StringUtils.isEmpty((String)assetReference)) {
            log.debug("The dynamic image property {} is not defined", (Object)"fileReference");
            return null;
        }
        try {
            assetReference = new URLCodec().decode(assetReference);
        }
        catch (DecoderException e) {
            log.error("Error while decoding fileReference: {}", (Object)assetReference);
        }
        assetReference = assetReference.startsWith("/is/image") ? assetReference.substring("/is/image".length()) : 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", "dam/components/scene7/dynamicmediaimage");
        tmp.put("height", "300");
        tmp.put("width", "300");
        tmp.put("jpegQuality", "85");
        tmp.put("unsharpMaskAmount", "0");
        tmp.put("unsharpMaskRadius", "0");
        tmp.put("unsharpMaskThreshold", "0");
        PRODUCT_ASSET_RENDERING_PROPERTIES = Collections.unmodifiableMap(tmp);
    }
}