StaticImageHandler.java 7.81 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.ImageResource
 *  com.day.cq.dam.api.Asset
 *  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.commons.ImageResource;
import com.day.cq.dam.api.Asset;
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 Static Images", description="Manages product assets of type: static images.")
@Service(value={ProductAssetHandler.class})
@Property(name="service.description", value={"Manages product assets of type: static images"})
public class StaticImageHandler
implements ProductAssetHandler {
    private static final Logger log = LoggerFactory.getLogger(StaticImageHandler.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 = "static-image";
    @Property(label="%Handler Name", description="%Name of the handler", value={"static-image"})
    private static final String CONFIG_PROPERTY_HANDLER_NAME = "cq.commerce.asset.handler.name";
    private static final String[] MIME_TYPES = new String[]{"image/png", "image/jpeg", "image/tiff", "image/png", "image/bmp", "image/gif", "image/pipeg", "image/x-portable-anymap", "image/x-portable-bitmap", "image/x-portable-graymap", "image/x-portable-pixmap", "image/x-rgb", "image/x-xbitmap", "image/x-xpixmap", "image/x-icon", "image/photoshop", "image/x-photoshop", "image/psd", "application/photoshop", "application/psd", "image/vnd.adobe.photoshop"};
    private static final List<String> MIME_TYPES_LIST = Arrays.asList(MIME_TYPES);
    private static final String RES_TYPE = "commerce/components/product/image";
    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)"static-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 static image is not defined.");
            return false;
        }
        String dcFormat = asset.getMetadataValue("dc:format");
        if (StringUtils.isEmpty((String)dcFormat)) {
            log.debug("The dc:format of the static image is not defined.");
            return false;
        }
        return MIME_TYPES_LIST.contains(dcFormat);
    }

    @Override
    public String getThumbnailUrl(Resource productAssetResource, String selectorString) {
        String[] arrstring;
        if (selectorString != null) {
            arrstring = selectorString.split("\\.");
        } else {
            String[] arrstring2 = new String[1];
            arrstring = arrstring2;
            arrstring2[0] = "";
        }
        String[] selectors = arrstring;
        try {
            ImageResource thumbnail = new ImageResource(productAssetResource);
            if (thumbnail != null) {
                String thumbnailSelector = "thumbnail";
                for (String s : selectors) {
                    if (s.isEmpty()) continue;
                    thumbnailSelector = thumbnailSelector + "." + s;
                }
                thumbnail.setSelector(thumbnailSelector);
                return thumbnail.getHref();
            }
        }
        catch (IllegalArgumentException e) {
            log.error("Cannot get a thumbnail url for product asset resource at: {}", (Object)productAssetResource.getPath(), (Object)e);
        }
        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 static 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);
        }
        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", "commerce/components/product/image");
        PRODUCT_ASSET_RENDERING_PROPERTIES = Collections.unmodifiableMap(tmp);
    }
}