RenditionResource.java
5.07 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.AssetManager
* com.adobe.granite.asset.api.Rendition
* com.adobe.granite.rest.utils.DeepModifiableValueMapDecorator
* org.apache.commons.io.IOUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceMetadata
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
*/
package com.adobe.granite.rest.assets.impl;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetManager;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.AssetResourceProvider;
import com.adobe.granite.rest.assets.impl.FileInputUtils;
import com.adobe.granite.rest.utils.DeepModifiableValueMapDecorator;
import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
public class RenditionResource
extends AbstractAssetResource {
public static final String RESOURCE_TYPE = "granite/rest/assets/rendition";
private final String mimeType;
private long size = 0;
private ValueMap valueMap;
public RenditionResource(Resource resource, String path) {
Resource content;
super(resource, path);
Rendition rendition = (Rendition)resource.adaptTo(Rendition.class);
if (rendition != null) {
this.size = rendition.getSize();
}
if ((content = resource.getChild("jcr:content")) == null) {
this.mimeType = "application/octet-stream";
} else {
if (content.getResourceMetadata() != null) {
this.resourceMetadata.setModificationTime(content.getResourceMetadata().getModificationTime());
}
ValueMap base = (ValueMap)content.adaptTo(ValueMap.class);
this.mimeType = (String)base.get("jcr:mimeType", String.class);
this.valueMap = new RenditionValueMapDecorator(this, base);
}
}
public String getResourceType() {
return "granite/rest/assets/rendition";
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
Resource content;
if (type == ValueMap.class || type == ModifiableValueMap.class) {
return (AdapterType)this.valueMap;
}
if (type == InputStream.class && (content = this.getResourceResolver().getResource(this.resource, "jcr:content")) != null) {
return (AdapterType)content.adaptTo(InputStream.class);
}
return (AdapterType)super.adaptTo(type);
}
public String getMimeType() {
return this.mimeType;
}
public long getSize() {
return this.size;
}
private class RenditionValueMapDecorator
extends DeepModifiableValueMapDecorator {
private AssetManager assetMgr;
private Asset asset;
private String dataParameter;
public RenditionValueMapDecorator(RenditionResource resource, ValueMap base) {
super((Resource)resource, (Map)base, new String[0]);
this.dataParameter = AssetResourceProvider.getDataPropertyKey(base);
String fileName = "";
if (this.dataParameter != null) {
Map fileProperties = (Map)base.get((Object)"file");
fileName = (String)base.get((Object)"name");
}
String name = (String)this.get("name", (Object)fileName);
String path = resource.getParent().getParent().getPath();
String resourcePath = AssetResourceProvider.getResourcePath(path, name);
this.assetMgr = (AssetManager)resource.getResourceResolver().adaptTo(AssetManager.class);
this.asset = this.assetMgr.getAsset(resourcePath);
}
public Object put(String key, Object value) {
if ("file".equals(key) || key.equals(this.dataParameter)) {
return this.addRendition(this.asset, RenditionResource.this.resource.getName(), FileInputUtils.getFileInputStream(value), FileInputUtils.getRenditionMap(value, "rendition.mime"));
}
return super.put(key, value);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Rendition addRendition(Asset asset, String renditionName, InputStream is, Map<String, Object> renditionProperties) {
if (asset != null) {
try {
Rendition rendition = asset.setRendition(renditionName, is, renditionProperties);
return rendition;
}
finally {
IOUtils.closeQuietly((InputStream)is);
}
}
return null;
}
}
}