AssetImpl.java
15.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.Asset
* com.adobe.granite.asset.api.AssetException
* com.adobe.granite.asset.api.AssetMetadata
* com.adobe.granite.asset.api.AssetRelation
* com.adobe.granite.asset.api.Rendition
* com.adobe.granite.asset.api.RenditionHandler
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFactory
* org.apache.commons.lang.ArrayUtils
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.PersistenceException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ResourceWrapper
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.resource.collection.ResourceCollection
* org.apache.sling.resource.collection.ResourceCollectionManager
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.asset.core.impl;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.AssetException;
import com.adobe.granite.asset.api.AssetMetadata;
import com.adobe.granite.asset.api.AssetRelation;
import com.adobe.granite.asset.api.Rendition;
import com.adobe.granite.asset.api.RenditionHandler;
import com.adobe.granite.asset.core.impl.AssetRelationImpl;
import com.adobe.granite.asset.core.impl.AssetWrapper;
import com.adobe.granite.asset.core.impl.RenditionUtils;
import com.adobe.granite.asset.core.impl.TransientJcrPropertyMap;
import com.adobe.granite.asset.core.impl.ValueMapCreatingResourceOnDemand;
import com.adobe.granite.asset.core.impl.metadata.AssetMetadataImpl;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ResourceWrapper;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.resource.collection.ResourceCollection;
import org.apache.sling.resource.collection.ResourceCollectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class AssetImpl
extends ResourceWrapper
implements Asset {
private static final Logger log = LoggerFactory.getLogger(AssetImpl.class);
private final ResourceResolver resolver;
private final AssetWrapper assetWrapper;
private final AssetMetadata assetMetadata;
private final Session session;
private final ModifiableValueMap valueMap;
public AssetImpl(final Resource resource) {
super(resource);
this.resolver = resource.getResourceResolver();
this.session = (Session)this.resolver.adaptTo(Session.class);
this.assetWrapper = new AssetWrapper(resource);
this.valueMap = new ValueMapCreatingResourceOnDemand(){
public Resource getResource() {
return resource.getResourceResolver().getResource(resource, "jcr:content");
}
public Resource createResource() throws PersistenceException {
return resource.getResourceResolver().create(resource, "jcr:content", null);
}
public ValueMap getValueMap(Resource resource2) {
return new TransientJcrPropertyMap(resource2);
}
};
this.assetMetadata = new AssetMetadataImpl(this.assetWrapper);
}
private ResourceCollectionManager getResourceCollectionManager() {
return (ResourceCollectionManager)this.resolver.adaptTo(ResourceCollectionManager.class);
}
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (ValueMap.class == type) {
return (AdapterType)this.valueMap;
}
if (type == AssetMetadata.class) {
return (AdapterType)this.assetMetadata;
}
return (AdapterType)super.adaptTo(type);
}
public Rendition setRendition(String name, InputStream is, Map<String, Object> map) {
String handlerId = RenditionUtils.getRenditionHandlerId(map, "jcr.default");
RenditionHandler rh = RenditionUtils.getRenditionHandler(handlerId);
try {
Resource renditionsResource = this.assetWrapper.getOrCreateRenditionsResource();
Resource rendition = this.setRenditionResource(name, renditionsResource, handlerId);
return rh.setRendition(rendition, is, map);
}
catch (RepositoryException e) {
throw new AssetException("Failed to set Rendition [ " + name + "] under " + this.getPath(), (Throwable)e);
}
}
public void removeRendition(String name) {
Resource renditionsResource;
try {
renditionsResource = this.assetWrapper.getOrCreateRenditionsResource();
}
catch (RepositoryException e) {
throw new AssetException("Cannot create renditions folder", (Throwable)e);
}
Resource rendition = this.resolver.getResource(renditionsResource, name);
if (rendition == null) {
throw new AssetException("Rendition with name [ " + name + " ] does not exist");
}
String handlerId = RenditionUtils.getRenditionHandlerId(rendition, "jcr.default");
RenditionHandler rh = RenditionUtils.getRenditionHandler(handlerId);
rh.deleteRendition(rendition);
}
public String getName() {
return ResourceUtil.getName((String)this.getPath());
}
public Rendition getRendition(String name) {
return this.assetWrapper.getRendition(name);
}
public Iterator<Rendition> listRenditions() {
return this.assetWrapper.listRenditions();
}
public Iterator<Asset> listRelated(String name) {
return this.assetWrapper.listRelated(name);
}
public String getIdentifier() {
try {
return ((Node)this.getResource().adaptTo(Node.class)).getIdentifier();
}
catch (RepositoryException e) {
log.warn("Failed to get Asset identifier", (Throwable)e);
return null;
}
}
public AssetRelation addRelation(String name, String assetPath, Map<String, Object> map) {
Asset asset = this.getAsset(assetPath);
if (asset == null) {
throw new AssetException("Failed to set related asset, Asset at path [ " + assetPath + " ] does not exist");
}
try {
ResourceCollection rc;
Resource related = this.assetWrapper.getOrCreateRelatedResource();
Node node = (Node)related.adaptTo(Node.class);
if (node.hasProperty(name)) {
Property p = node.getProperty(name);
Object[] values = p.getValues();
values = (Value[])ArrayUtils.add((Object[])values, (Object)asset.getPath());
p.setValue((Value[])values);
return new AssetRelationImpl(name, asset);
}
Resource relationRes = related.getChild(name);
ResourceCollectionManager rcm = this.getResourceCollectionManager();
if (relationRes == null) {
HashMap<String, String> props = new HashMap<String, String>();
props.put("jcr:primaryType", "nt:unstructured");
rc = rcm.createCollection(related, name, props);
} else {
rc = rcm.getCollection(relationRes);
}
if (!rc.add(this.resolver.getResource(assetPath), map)) {
String message = MessageFormat.format("Asset {0} already has relation to {1}", this.getPath(), assetPath);
throw new AssetException(message);
}
return new AssetRelationImpl(name, asset, rc.getProperties(this.resolver.getResource(assetPath)));
}
catch (RepositoryException e) {
throw new AssetException("Failed to set related asset", (Throwable)e);
}
catch (PersistenceException e) {
throw new AssetException("Failed to set related asset", (Throwable)e);
}
}
public void removeRelation(String name, String assetPath) {
Asset asset = this.getAsset(assetPath);
if (asset == null) {
throw new AssetException("Failed to remove related asset, Asset at path [ " + assetPath + " ] does not exist");
}
try {
Resource related = this.assetWrapper.getOrCreateRelatedResource();
Node node = (Node)related.adaptTo(Node.class);
Resource relationRes = related.getChild(name);
if (relationRes != null) {
ResourceCollection rc = this.assetWrapper.getCollection(relationRes);
rc.remove((Resource)asset);
} else if (node.hasProperty(name)) {
Property p = node.getProperty(name);
Object[] values = p.getValues();
Object[] newValues = (Value[])ArrayUtils.removeElement((Object[])values, (Object)this.session.getValueFactory().createValue(asset.getPath()));
if (newValues.length == values.length) {
newValues = (Value[])ArrayUtils.removeElement((Object[])newValues, (Object)this.session.getValueFactory().createValue(asset.getIdentifier()));
}
p.setValue((Value[])newValues);
}
}
catch (RepositoryException e) {
throw new AssetException("Failed to remove related asset", (Throwable)e);
}
catch (PersistenceException e) {
throw new AssetException("Failed to remove related asset", (Throwable)e);
}
}
public void removeRelation(String name) {
try {
Resource related = this.assetWrapper.getOrCreateRelatedResource();
Node node = (Node)related.adaptTo(Node.class);
if (node.hasNode(name)) {
ResourceCollectionManager rcm = this.getResourceCollectionManager();
rcm.deleteCollection(related.getChild(name));
} else {
node.getProperty(name).remove();
}
}
catch (RepositoryException e) {
throw new AssetException("Failed to remove relation", (Throwable)e);
}
catch (PersistenceException e) {
throw new AssetException("Failed to remove relation", (Throwable)e);
}
}
public AssetMetadata getAssetMetadata() {
return this.assetMetadata;
}
private Resource setRenditionResource(String name, Resource parent, String handlerId) throws RepositoryException {
Node content;
Node parentNode = (Node)parent.adaptTo(Node.class);
if (parentNode.hasNode(name)) {
content = parentNode.getNode(name + "/" + "jcr:content");
} else {
if (StringUtils.isEmpty((String)handlerId)) {
log.warn("Handler ID is null, using ID [{}]", (Object)"jcr.default");
handlerId = "jcr.default";
}
if (StringUtils.equals((String)handlerId, (String)"jcr.default")) {
Node file = parentNode.addNode(name, "nt:file");
content = file.addNode("jcr:content", "nt:resource");
} else {
Node folder = parentNode.addNode(name, "sling:OrderedFolder");
content = folder.addNode("jcr:content", "nt:unstructured");
content.setProperty("rendition.handler.id", handlerId);
}
}
return this.resolver.getResource(content.getParent().getPath());
}
private Asset getAsset(String assetPath) {
Resource resource = this.resolver.getResource(assetPath);
if (resource != null) {
return (Asset)resource.adaptTo(Asset.class);
}
return null;
}
@Deprecated
public void setRelation(String name, String assetPath) {
this.addRelation(name, assetPath);
}
public AssetRelation addRelation(String name, String assetPath) {
return this.addRelation(name, assetPath, null);
}
public void orderRelationBefore(String name, String srcAssetPath, String destAssetPath) {
if (srcAssetPath == null) {
throw new IllegalArgumentException("Source Asset Path can not be null");
}
try {
Resource related = this.assetWrapper.getOrCreateRelatedResource();
Resource relationRes = related.getChild(name);
if (relationRes != null) {
ResourceCollection rc = this.assetWrapper.getCollection(relationRes);
Resource destResource = null;
if (destAssetPath != null) {
destResource = this.resolver.getResource(destAssetPath);
}
rc.orderBefore(this.resolver.getResource(srcAssetPath), destResource);
} else {
ModifiableValueMap mvm = (ModifiableValueMap)related.adaptTo(ModifiableValueMap.class);
Object[] refs = (String[])mvm.get(name, (Object)new String[0]);
int srcIndex = ArrayUtils.indexOf((Object[])refs, (Object)srcAssetPath);
if (srcIndex < 0) {
log.warn("there is no related resource {} in asset {}", (Object)srcAssetPath, (Object)this.getPath());
return;
}
if (destAssetPath == null) {
refs = (String[])ArrayUtils.remove((Object[])refs, (int)srcIndex);
refs = (String[])ArrayUtils.add((Object[])refs, (Object)srcAssetPath);
} else {
if (destAssetPath.equals(srcAssetPath)) {
String message = MessageFormat.format("Relation ordering failed, as source {} and destination {} can not be same", srcAssetPath, destAssetPath);
log.error(message);
throw new IllegalArgumentException(message);
}
int destIndex = ArrayUtils.indexOf((Object[])refs, (Object)destAssetPath);
if (destIndex < 0) {
log.warn("Relation ordering failed, as there is no asset {} in relation {} for destAssetPath", (Object)destAssetPath, (Object)this.getPath());
return;
}
refs = (String[])ArrayUtils.remove((Object[])refs, (int)srcIndex);
if (srcIndex < destIndex) {
destIndex = ArrayUtils.indexOf((Object[])refs, (Object)destAssetPath);
}
refs = (String[])ArrayUtils.add((Object[])refs, (int)destIndex, (Object)srcAssetPath);
}
}
}
catch (RepositoryException e) {
throw new AssetException("Failed to reorder related asset", (Throwable)e);
}
}
public Iterator<AssetRelation> listRelations(String name) {
return this.assetWrapper.listRelations(name);
}
}