ImagePropertiesHandler.java
13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* 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";
}
}