AbstractAssetHandler.java
15.2 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.AssetRelation
* com.day.cq.dam.api.Asset
* com.day.cq.dam.api.AssetHandlerException
* com.day.cq.dam.api.AssetManager
* com.day.cq.dam.api.ProcessorException
* com.day.cq.dam.api.Rendition
* com.day.cq.dam.api.cache.BufferedImageCache
* com.day.cq.dam.api.cache.BufferedImageCache$Entry
* com.day.cq.dam.api.handler.AssetHandler
* com.day.cq.dam.api.metadata.ExtractedMetadata
* com.day.cq.dam.api.thumbnail.ThumbnailConfig
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.commons.handler;
import com.adobe.granite.asset.api.AssetRelation;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetHandlerException;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.api.ProcessorException;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.cache.BufferedImageCache;
import com.day.cq.dam.api.handler.AssetHandler;
import com.day.cq.dam.api.metadata.ExtractedMetadata;
import com.day.cq.dam.api.thumbnail.ThumbnailConfig;
import com.day.cq.dam.commons.handler.XMPProcessor;
import com.day.cq.dam.commons.thumbnail.ThumbnailGenerator;
import com.day.cq.dam.commons.util.DamUtil;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(componentAbstract=1, metatype=0)
public abstract class AbstractAssetHandler
implements AssetHandler {
private static final Logger log = LoggerFactory.getLogger(AbstractAssetHandler.class);
public static String PROPERTY_DEACTIVATED_MIME_TYPES = "dam.assethandler.deactivateMimeTypes";
@Reference(policy=ReferencePolicy.STATIC)
private JcrResourceResolverFactory jcrResolverFactory;
@Reference(policy=ReferencePolicy.STATIC)
private BufferedImageCache imageCache;
public void createThumbnails(Node asset, Node renditionFolder, Session session, List<Integer[]> dimensions) {
Resource assetResource = this.getResourceResolver(session).getResource(this.safeGetPath(asset));
if (null != assetResource) {
Asset a = DamUtil.resolveToAsset(assetResource);
if (null != a) {
try {
this.createThumbnails(a, ThumbnailGenerator.getConfigs(dimensions));
}
catch (IOException e) {
log.error("createThumbnails: I/O error while creating thumbnails for [{}]: ", (Object)a.getPath(), (Object)e);
}
} else {
log.error("createThumbnails: could not find asset [{}] to create thumbnails for.", (Object)assetResource.getPath());
}
} else {
log.error("createThumbnails: could not find resource [{}] to create thumbnails for.", (Object)this.safeGetPath(asset));
}
}
public void createThumbnailsExt(Node asset, Node renditionFolder, Session session, List<Map<String, Object>> thumbnailConfigs) throws IOException {
Resource assetResource = this.getResourceResolver(session).getResource(this.safeGetPath(asset));
if (null != assetResource) {
Asset a = DamUtil.resolveToAsset(assetResource);
if (null != a) {
try {
HashSet<ThumbnailConfig> configs = new HashSet<ThumbnailConfig>();
for (Map<String, Object> entry : thumbnailConfigs) {
String width = (String)entry.get("width");
String height = (String)entry.get("height");
String doCenter = (String)entry.get("doCenter");
String configStr = width + ":" + height + ":" + doCenter;
ThumbnailConfig config = ThumbnailGenerator.parseConfig(configStr);
if (null != config) {
configs.add(config);
continue;
}
log.error("createThumbnailsExt: cannot add invalid thumbnail config [{}] for asset [{}]", (Object)configStr, (Object)this.safeGetPath(asset));
}
this.createThumbnails(a, configs);
}
catch (IOException e) {
log.error("createThumbnails: I/O error while creating thumbnails for [{}]: ", (Object)a.getPath(), (Object)e);
}
} else {
log.error("createThumbnails: could not find asset [{}] to create thumbnails for.", (Object)assetResource.getPath());
}
} else {
log.error("createThumbnails: could not find resource [{}] to create thumbnails for.", (Object)this.safeGetPath(asset));
}
}
public void createThumbnails(Asset asset, Collection<ThumbnailConfig> configs) throws IOException {
this.createThumbnails(asset, asset.getOriginal(), configs);
}
public void createThumbnails(Asset asset) throws IOException {
this.createThumbnails(asset, asset.getOriginal(), this.getThumbnailConfigs(asset));
}
public void createThumbnails(Asset asset, Rendition rendition, Collection<ThumbnailConfig> configs) throws IOException {
block8 : {
if (rendition == null) {
throw new IOException("cannot create thumbnail for for asset [" + asset.getPath() + "]: " + "rendition doesn't exist.");
}
BufferedImageCache.Entry image = this.imageCache.getImage(rendition, (AssetHandler)this);
try {
if (image != null && image.getImage() != null) {
ThumbnailGenerator generator = new ThumbnailGenerator(asset, image.getImage());
generator.generate(configs);
break block8;
}
log.error("createThumbnails: cannot create thumbnails for asset [{}], failed loading graphical representation for rendition [{}].", (Object)asset.getPath(), (Object)rendition.getPath());
}
catch (RepositoryException re) {
throw new IOException("createThumbnails: error while storing thumbnail(s) for asset [" + asset.getPath() + "]: ");
}
finally {
if (image != null) {
image.release();
}
}
}
}
public boolean canHandleSubAssets() {
return false;
}
public List<String> processSubAssets(Node asset, Session session) {
log.debug("processSubAssets: no subassets to process for asset [{}].", (Object)this.safeGetPath(asset));
return new ArrayList<String>();
}
public List<String> processSubAssets(Asset asset) {
log.debug("processSubAssets: no subassets to process for asset [{}].", (Object)asset.getPath());
return new ArrayList<String>();
}
public Iterator<? extends AssetRelation> processRelated(Asset asset) {
log.debug("processReferences: no references to process for asset [{}].", (Object)asset.getPath());
return Collections.emptyList().iterator();
}
public BufferedImage getImage(Rendition rendition) throws IOException {
return null;
}
public BufferedImage getImage(Rendition rendition, Dimension maxDimension) throws IOException {
return this.getImage(rendition);
}
public void exportAsset(Node asset, OutputStream stream) throws AssetHandlerException {
try {
Resource assetResource = this.getResourceResolver(asset.getSession()).getResource(this.safeGetPath(asset));
if (null != assetResource) {
Asset a = DamUtil.resolveToAsset(assetResource);
if (null != a) {
this.exportAsset(a, stream);
} else {
log.error("exportAsset: cannot export, asset [{}] doesn't exist.", (Object)this.safeGetPath(asset));
}
}
}
catch (Exception e) {
throw new AssetHandlerException("Cannot export asset: " + e.getMessage(), (Throwable)e);
}
}
public void exportAsset(Asset asset, OutputStream stream) throws AssetHandlerException {
try {
InputStream is = asset.getOriginal().getStream();
IOUtils.copy((InputStream)is, (OutputStream)stream);
is.close();
}
catch (Exception e) {
throw new AssetHandlerException("Cannot export asset: " + e.getMessage(), (Throwable)e);
}
}
public ExtractedMetadata extractMetadata(Node asset) {
try {
Asset a;
Resource assetResource = this.getResourceResolver(asset.getSession()).getResource(this.safeGetPath(asset));
if (null != assetResource && null != (a = (Asset)assetResource.adaptTo(Asset.class))) {
return this.extractMetadata(a);
}
}
catch (RepositoryException e) {
log.error("extractMetadata: error while extracting metadata for asset [{}]: ", (Object)this.safeGetPath(asset), (Object)e);
}
return null;
}
public BufferedImage getImage(Node file) throws IOException {
return null;
}
protected BufferedImage getThumbnailImage(Node asset) {
try {
Resource resource = this.getResourceResolver(asset.getSession()).getResource(this.safeGetPath(asset));
if (null != resource) {
Rendition rendition = null;
if (DamUtil.isAsset(resource)) {
rendition = ((Asset)resource.adaptTo(Asset.class)).getOriginal();
} else if (DamUtil.isRendition(resource)) {
rendition = (Rendition)resource.adaptTo(Rendition.class);
}
if (null != rendition) {
return this.getImage(rendition);
}
log.error("getThumbnailImage: could not get image, resource [{}] doesn't exist.", (Object)this.safeGetPath(asset));
}
}
catch (RepositoryException e) {
log.error("getThumbnailImage: error while access repository: ", (Throwable)e);
}
catch (IOException e) {
log.error("getThumbnailImage: I/O error: ", (Throwable)e);
}
return null;
}
protected ResourceResolver getResourceResolver(Session session) {
return this.jcrResolverFactory.getResourceResolver(session);
}
protected AssetManager getAssetManager(Session session) {
return (AssetManager)this.getResourceResolver(session).adaptTo(AssetManager.class);
}
protected InputStream getInputStream(Node file) {
try {
String dataPath;
String string = dataPath = file.isNodeType("dam:Asset") ? "jcr:content/renditions/original/jcr:content/jcr:data" : "jcr:content/jcr:data";
if (file.hasProperty(dataPath)) {
return file.getProperty(dataPath).getBinary().getStream();
}
}
catch (RepositoryException e) {
log.warn("getInputStream: repository error while getting stream for file [{}]: ", (Object)this.safeGetPath(file), (Object)e);
}
return new ByteArrayInputStream(new byte[0]);
}
protected void setMimetype(ExtractedMetadata metadata, Asset asset) {
Rendition original = asset.getOriginal();
if (original != null) {
metadata.setMetaDataProperty("dc:format", (Object)original.getMimeType());
}
}
protected String safeGetPath(Node node) {
try {
return node.getPath();
}
catch (RepositoryException e) {
log.warn("safeGetPath: error while getting path from node: ", (Throwable)e);
return "(unknown)";
}
}
protected Collection<ThumbnailConfig> getThumbnailConfigs(Asset asset) {
HashSet<ThumbnailConfig> set = new HashSet<ThumbnailConfig>();
List renditions = asset.getRenditions();
for (Rendition rendition : renditions) {
String name = rendition.getName();
if (!name.startsWith("cq5dam.thumbnail.")) continue;
String[] fragments = name.split("\\.");
String width = fragments[2];
String height = fragments[3];
String configStr = width + ":" + height + ":true";
ThumbnailConfig config = ThumbnailGenerator.parseConfig(configStr);
if (null != config) {
set.add(config);
continue;
}
log.error("getThumbnailConfigs: cannot add invalid config [{}] for asset [{}].", (Object)configStr, (Object)asset.getPath());
}
return set;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void execGenericProcessor(InputStream is, ExtractedMetadata metadata) {
try {
InputStream xmp = XMPProcessor.process(is);
if (xmp != null) {
metadata.setXmp(xmp);
}
}
catch (IOException e) {
log.error("I/O error while getting metadata", (Throwable)e);
}
catch (ProcessorException e) {
log.error("Failed to extract metadata : {}", (Object)e.getMessage());
if (log.isDebugEnabled()) {
log.debug("Stack trace.", (Throwable)e);
}
}
finally {
IOUtils.closeQuietly((InputStream)is);
}
}
protected void bindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.jcrResolverFactory = jcrResourceResolverFactory;
}
protected void unbindJcrResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.jcrResolverFactory == jcrResourceResolverFactory) {
this.jcrResolverFactory = null;
}
}
protected void bindImageCache(BufferedImageCache bufferedImageCache) {
this.imageCache = bufferedImageCache;
}
protected void unbindImageCache(BufferedImageCache bufferedImageCache) {
if (this.imageCache == bufferedImageCache) {
this.imageCache = null;
}
}
}