ImagePropertiesHandler.java 13.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.scene7.is.catalog.util.ObjectTypeEnum
 *  com.scene7.is.catalog.util.localization.LocaleMap
 *  com.scene7.is.provider.EmbeddedProvider
 *  com.scene7.is.provider.ModifierList
 *  com.scene7.is.provider.ProviderException
 *  com.scene7.is.provider.RequestContext
 *  com.scene7.is.provider.ThumbTypeEnum
 *  com.scene7.is.provider.ThumbTypeEnumConverter
 *  com.scene7.is.provider.catalog.Catalog
 *  com.scene7.is.provider.catalog.CatalogException
 *  com.scene7.is.provider.catalog.ObjectRecord
 *  com.scene7.is.sleng.Anchor
 *  com.scene7.is.sleng.ColorProfileInfo
 *  com.scene7.is.sleng.ColorSpaceEnum
 *  com.scene7.is.sleng.IccProfile
 *  com.scene7.is.sleng.ImageAccessException
 *  com.scene7.is.sleng.ImageInfo
 *  com.scene7.is.sleng.ImageServer
 *  com.scene7.is.sleng.ipp.IppBadImageAnchorException
 *  com.scene7.is.util.Location
 *  com.scene7.is.util.callbacks.Option
 *  com.scene7.is.util.error.ApplicationException
 *  com.scene7.is.util.text.converters.BooleanConverter
 *  com.scene7.is.util.text.converters.DoubleConverter
 *  com.scene7.is.util.text.converters.IntegerConverter
 *  com.scene7.is.util.text.converters.LocationConverter
 *  com.scene7.is.util.text.converters.LongConverter
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.scene7.is.ps.provider;

import com.scene7.is.catalog.util.ObjectTypeEnum;
import com.scene7.is.catalog.util.localization.LocaleMap;
import com.scene7.is.provider.EmbeddedProvider;
import com.scene7.is.provider.ModifierList;
import com.scene7.is.provider.ProviderException;
import com.scene7.is.provider.RequestContext;
import com.scene7.is.provider.ThumbTypeEnum;
import com.scene7.is.provider.ThumbTypeEnumConverter;
import com.scene7.is.provider.catalog.Catalog;
import com.scene7.is.provider.catalog.CatalogException;
import com.scene7.is.provider.catalog.ObjectRecord;
import com.scene7.is.ps.provider.ModifierSet;
import com.scene7.is.ps.provider.PropertiesHandler;
import com.scene7.is.ps.provider.Request;
import com.scene7.is.ps.provider.Util;
import com.scene7.is.ps.provider.defs.ModifierEnum;
import com.scene7.is.ps.provider.parsers.query.PSModifierListConverter;
import com.scene7.is.sleng.Anchor;
import com.scene7.is.sleng.ColorProfileInfo;
import com.scene7.is.sleng.ColorSpaceEnum;
import com.scene7.is.sleng.IccProfile;
import com.scene7.is.sleng.ImageAccessException;
import com.scene7.is.sleng.ImageInfo;
import com.scene7.is.sleng.ImageServer;
import com.scene7.is.sleng.ipp.IppBadImageAnchorException;
import com.scene7.is.util.Location;
import com.scene7.is.util.callbacks.Option;
import com.scene7.is.util.error.ApplicationException;
import com.scene7.is.util.text.converters.BooleanConverter;
import com.scene7.is.util.text.converters.DoubleConverter;
import com.scene7.is.util.text.converters.IntegerConverter;
import com.scene7.is.util.text.converters.LocationConverter;
import com.scene7.is.util.text.converters.LongConverter;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class ImagePropertiesHandler
extends PropertiesHandler {
    private final ImageServer server;

    public ImagePropertiesHandler(ImageServer server) {
        this.server = server;
    }

    protected abstract RequestContext getRequestContext();

    protected abstract EmbeddedProvider getSvgProvider();

    @Override
    protected Map<String, Object> getProperties(Request request) throws ImageAccessException {
        HashMap<String, Object> props = new HashMap<String, Object>();
        ObjectRecord descriptor = request.getGlobalAttributes().getOrDie(ModifierEnum.I_DESCRIPTOR);
        try {
            this.setImageProperties(descriptor, props);
        }
        catch (ProviderException e) {
            throw new ImageAccessException(8, "Failed to get image properties", (Throwable)e);
        }
        catch (ApplicationException e) {
            String errorMessage = this.buildErrorMessage((Throwable)e);
            String finalError = "Failed to get image properties";
            if (errorMessage.length() > 0) {
                finalError = finalError + " - " + errorMessage;
            }
            throw new ImageAccessException(8, finalError, (Throwable)e);
        }
        return props;
    }

    private void setImageProperties(ObjectRecord record, Map<String, Object> dst) throws ApplicationException {
        ImagePropertiesHandler.setProperty("image.expiration", ImagePropertiesHandler.expirationToString(record.getExpiration()), dst);
        ImagePropertiesHandler.setProperty("image.modifier", PSModifierListConverter.psModifierListConverter().revert(record.getModifier()), dst);
        ImagePropertiesHandler.setProperty("image.postModifier", PSModifierListConverter.psModifierListConverter().revert(record.getPostModifier()), dst);
        ImagePropertiesHandler.setProperty("image.printRes", DoubleConverter.doubleConverter().revert((Object)record.getPrintResolution()), dst);
        ImagePropertiesHandler.setProperty("image.resolution", DoubleConverter.doubleConverter().revert((Object)record.getResolution()), dst);
        ImagePropertiesHandler.setProperty("image.thumbRes", DoubleConverter.doubleConverter().revert((Object)record.getThumbResolution()), dst);
        ImagePropertiesHandler.setProperty("image.thumbType", ThumbTypeEnumConverter.thumbTypeEnumConverter().revert((Object)record.getThumbType()), dst);
        if (record.getMaskPath() != null) {
            ImagePropertiesHandler.setProperty("image.mask", "3", dst);
        }
        if (!record.getCatalog().getLocaleMap().isEmpty()) {
            ImagePropertiesHandler.setProperty("image.translatedId", record.getName(), dst);
        }
        Location anchor = record.getAnchor();
        String imagePath = record.getImagePath();
        if (imagePath != null) {
            ImageInfo imageProps;
            ObjectTypeEnum type = record.getType();
            if (type == ObjectTypeEnum.SVG) {
                RequestContext requestContext = this.getRequestContext();
                EmbeddedProvider svgProvider = this.getSvgProvider();
                ObjectRecord catalogRecord = requestContext.getRecord();
                imageProps = svgProvider.getObjectProps(requestContext, catalogRecord);
            } else {
                imageProps = this.server.getImageProps(Anchor.createImageAnchor((String)imagePath));
            }
            if (!record.getCatalog().getCatalogBasedValidation()) {
                ImagePropertiesHandler.setProperty("image.timeStamp", LongConverter.longConverter().revert((Object)imageProps.getFileLastModified()), dst);
            } else {
                ImagePropertiesHandler.setProperty("image.timeStamp", LongConverter.longConverter().revert((Object)record.getLastModified()), dst);
            }
            Dimension imageSize = imageProps.getSize();
            int width = imageSize.width;
            int height = imageSize.height;
            if (anchor == null) {
                anchor = Location.location((double)((double)width / 2.0), (double)((double)height / 2.0));
            }
            ImagePropertiesHandler.setProperty("image.width", IntegerConverter.integerConverter().revert((Object)width), dst);
            ImagePropertiesHandler.setProperty("image.height", IntegerConverter.integerConverter().revert((Object)height), dst);
            ColorProfileInfo profileInfo = imageProps.getProfileInfo();
            ImagePropertiesHandler.setProperty("image.pixTyp", profileInfo.getColorSpace().pixelType, dst);
            if (!dst.containsKey("image.mask")) {
                String value = imageProps.hasAlpha() ? "1" : "0";
                ImagePropertiesHandler.setProperty("image.mask", value, dst);
            }
            ImagePropertiesHandler.setProperty("image.embeddedIccProfile", BooleanConverter.booleanConverter().revert((Object)imageProps.hasEmbeddedProfile()), dst);
            this.handleProfile(dst, imageProps, record);
            ImagePropertiesHandler.setProperty("image.embeddedPhotoshopPaths", BooleanConverter.booleanConverter().revert((Object)imageProps.hasEmbeddedPhotoshopPaths()), dst);
            ImagePropertiesHandler.setProperty("image.embeddedXmpData", BooleanConverter.booleanConverter().revert((Object)imageProps.hasEmbeddedXmpData()), dst);
            ImagePropertiesHandler.setProperty("image.photoshopPathnames", imageProps.getPhotoshopPathnames(), dst);
        } else {
            ImagePropertiesHandler.setProperty("warning", "no image file", dst);
        }
        if (anchor != null) {
            ImagePropertiesHandler.setProperty("image.anchor", LocationConverter.locationConverter().revert((Object)anchor), dst);
        }
    }

    private static void setProperty(@NotNull String name, @Nullable Object value, @NotNull Map<String, Object> dst) {
        if (value != null) {
            if (value instanceof String) {
                String tmp = (String)value;
                if (!tmp.isEmpty()) {
                    dst.put(name, value);
                }
            } else {
                dst.put(name, value);
            }
        }
    }

    @NotNull
    private static String expirationToString(@Nullable Object value) {
        if (value != null) {
            long expiration = (Long)value;
            if (expiration == Long.MAX_VALUE) {
                return "-1.0";
            }
            if (expiration >= 0) {
                return String.valueOf((double)expiration / 3600000.0);
            }
            throw new AssertionError(value);
        }
        return "";
    }

    private void handleProfile(Map<String, Object> props, ImageInfo imageInfo, ObjectRecord record) throws ImageAccessException, CatalogException {
        ColorProfileInfo profile = imageInfo.getProfileInfo();
        ColorProfileInfo defaultProfile = this.getCatalogProfile(record, profile.getColorSpace());
        try {
            profile = this.server.resolveProfile(imageInfo, defaultProfile);
            if (profile != null) {
                props.put("image.iccProfile", profile.getDescription());
            }
        }
        catch (IllegalArgumentException e) {
            ImagePropertiesHandler.setProperty("warning", e.getMessage(), props);
        }
    }

    @Nullable
    private ColorProfileInfo getCatalogProfile(ObjectRecord attributes, ColorSpaceEnum colorSpace) throws ImageAccessException, CatalogException {
        Option profile = attributes.getDefaultSourceProfile(colorSpace);
        if (profile.isEmpty()) {
            return null;
        }
        return this.server.getProfileInfo(((IccProfile)profile.get()).path());
    }

    private String buildErrorMessage(Throwable e) {
        RequestContext context = this.getRequestContext();
        if (context != null) {
            String failingImageId = Util.findFailingImageId(Option.some((Object)e), context);
            boolean isInCatalog = Util.isInPSCatalog(failingImageId, context);
            if (isInCatalog) {
                String message = "Unable to find image file";
                if (e instanceof IppBadImageAnchorException) {
                    String originalPath = ((IppBadImageAnchorException)e).getOriginalPath();
                    String resolvedPath = ((IppBadImageAnchorException)e).getResolvedPath();
                    message = message + " '" + originalPath + "', resolved as '" + resolvedPath + "'.";
                }
                return message;
            }
            return "Unable to find " + failingImageId;
        }
        return "";
    }

    private static interface ImageProps {
        public static final String ANCHOR = "image.anchor";
        public static final String EXPIRATION = "image.expiration";
        public static final String MODIFIER = "image.modifier";
        public static final String POST_MODIFIER = "image.postModifier";
        public static final String PRINT_RESOLUTION = "image.printRes";
        public static final String RESOLUTION = "image.resolution";
        public static final String THUMB_RESOLUTION = "image.thumbRes";
        public static final String THUMB_TYPE = "image.thumbType";
        public static final String MASK_TYPE = "image.mask";
        public static final String WIDTH = "image.width";
        public static final String HEIGHT = "image.height";
        public static final String PIXEL_TYPE = "image.pixTyp";
        public static final String HAS_EMBEDDED_PROFILE = "image.embeddedIccProfile";
        public static final String ICC_PROFILE = "image.iccProfile";
        public static final String HAS_EMBEDDED_PHOTOSHOP_PATHS = "image.embeddedPhotoshopPaths";
        public static final String HAS_EMBEDDED_XMP_DATA = "image.embeddedXmpData";
        public static final String WARNING = "warning";
        public static final String TIME_STAMP = "image.timeStamp";
        public static final String PHOTOSHOP_PATHNAMES = "image.photoshopPathnames";
        public static final String TRANSLATED_ID = "image.translatedId";
    }

    private static interface MaskType {
        public static final String NONE = "0";
        public static final String PREMULTIPLIED = "1";
        public static final String EXTERNAL = "3";
    }

}