AssetResourceProvider.java
12.4 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
/*
* 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.comments.Comment
* com.adobe.granite.comments.CommentCollection
* com.adobe.granite.comments.CommentManager
* com.adobe.granite.rest.ApiResourceProvider
* javax.jcr.ItemExistsException
* org.apache.commons.io.IOUtils
* org.apache.commons.lang.StringUtils
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
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.comments.Comment;
import com.adobe.granite.comments.CommentCollection;
import com.adobe.granite.comments.CommentManager;
import com.adobe.granite.rest.ApiResourceProvider;
import com.adobe.granite.rest.assets.impl.AssetResource;
import com.adobe.granite.rest.assets.impl.AssetResourceFactory;
import com.adobe.granite.rest.assets.impl.CommentsResource;
import com.adobe.granite.rest.assets.impl.FileInputUtils;
import com.adobe.granite.rest.assets.impl.FolderResource;
import com.adobe.granite.rest.assets.impl.RenditionsResource;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import javax.jcr.ItemExistsException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AssetResourceProvider
implements ApiResourceProvider {
private static final Logger log = LoggerFactory.getLogger(AssetResourceProvider.class);
public static final String TYPE = "assets";
public static final String DAM_ROOT = "/content/dam";
private static final String PARAM_NAME = "name";
private AssetResourceFactory resourceFactory = new AssetResourceFactory();
private String apiContextPath;
private CommentManager commentManager;
public AssetResourceProvider(String contextPath, CommentManager commentManager) {
this.commentManager = commentManager;
this.apiContextPath = contextPath + "/" + "assets";
}
public Resource getResource(ResourceResolver resolver, String path) {
Resource asset;
log.debug("Asset getResource for path [" + path + "]");
Resource collectionRoot = resolver.getResource("/content/dam");
if (collectionRoot == null) {
log.error("Collection root {} not found.", (Object)"/content/dam");
return null;
}
if (StringUtils.isEmpty((String)path)) {
return this.resourceFactory.createResource(collectionRoot, this.apiContextPath);
}
String resourcePath = "/content/dam" + this.unmap(path);
log.debug("Resolving " + resourcePath + " relative to " + collectionRoot.getPath());
Resource requestedResource = resolver.getResource(resourcePath);
if (requestedResource == null && resourcePath.endsWith("comments") && (asset = resolver.getResource(ResourceUtil.getParent((String)resourcePath, (int)2))) != null) {
this.commentManager.getOrCreateCollection(asset, CommentCollection.class);
requestedResource = resolver.getResource(resourcePath);
}
log.debug("Resolved to " + (Object)requestedResource);
if (requestedResource != null) {
return this.resourceFactory.createResource(requestedResource, this.apiContextPath + path);
}
return null;
}
public Iterator<Resource> listChildren(Resource parent) {
log.debug("Asset listChildren of parent [" + parent.getPath() + "]");
String parentPath = parent.getPath();
if (!StringUtils.isEmpty((String)parentPath)) {
ResourceResolver resolver = parent.getResourceResolver();
Resource collectionRoot = resolver.getResource("/content/dam");
if (collectionRoot == null) {
log.error("Collection root {} not found.", (Object)"/content/dam");
return null;
}
Resource parentResource = resolver.getResource(this.unmap(parentPath));
if (parentResource == null) {
return null;
}
LinkedList<Resource> children = new LinkedList<Resource>();
Iterator it = parentResource.listChildren();
while (it.hasNext()) {
Resource child = (Resource)it.next();
Resource childRes = this.resourceFactory.createResource(child, this.map(child.getPath()));
if (childRes == null) continue;
children.add(childRes);
}
return children.iterator();
}
return null;
}
public Resource create(ResourceResolver resolver, String path, Map<String, Object> properties) throws PersistenceException {
log.debug("Asset create [" + path + "]");
if (!StringUtils.isEmpty((String)path)) {
Asset asset;
Resource assetResource;
Resource collectionRoot = resolver.getResource("/content/dam");
if (collectionRoot == null) {
throw new PersistenceException("Collection root not found for " + path);
}
Resource parent = resolver.getResource(ResourceUtil.getParent((String)(this.apiContextPath + path)));
if (parent == null) {
log.error("Could not find parent resource for {}.", (Object)path);
throw new PersistenceException("Cound not find parent at " + path);
}
AssetManager assetManager = (AssetManager)resolver.adaptTo(AssetManager.class);
Resource resource = resolver.getResource(this.apiContextPath + path);
String dataParameter = AssetResourceProvider.getDataPropertyKey(properties);
String name = properties.containsKey("name") ? (String)properties.get("name") : null;
String resourcePath = AssetResourceProvider.getResourcePath(path, name == null ? "" : name);
if (parent instanceof FolderResource) {
if (dataParameter != null) {
if (assetManager.assetExists(resourcePath)) {
throw new PersistenceException("Asset already exists at " + path, (Throwable)new ItemExistsException("Resource already exists at " + path));
}
Asset asset2 = assetManager.createAsset(resourcePath);
if (asset2 != null) {
this.addRendition(asset2, "original", FileInputUtils.getFileInputStream(properties.get(dataParameter)), FileInputUtils.getRenditionMap(properties.get(dataParameter), "rendition.mime"));
return this.resourceFactory.createResource((Resource)asset2, this.map(asset2.getPath()));
}
log.error("Could not create Asset for {} at {}.", (Object)path, (Object)resourcePath);
throw new PersistenceException("Cound not create Asset at " + path);
}
Resource root = resolver.getResource(ResourceUtil.getParent((String)resourcePath));
if (name == null) {
name = ResourceUtil.getName((String)resourcePath);
}
resource = resolver.create(root, name, properties);
return this.resourceFactory.createResource(resource, this.map(resource.getPath()));
}
if (parent instanceof RenditionsResource) {
String parentAssetPath = resourcePath.substring(0, resourcePath.indexOf("/rendition"));
Asset asset3 = assetManager.getAsset(parentAssetPath);
if (asset3 != null) {
if (name == null) {
name = resource != null ? resource.getName() : path.substring(path.lastIndexOf("/") + 1);
}
Rendition rendition = this.addRendition(asset3, name, FileInputUtils.getFileInputStream(properties.get(dataParameter)), FileInputUtils.getRenditionMap(properties.get(dataParameter), "rendition.mime"));
return this.resourceFactory.createResource((Resource)rendition, this.map(rendition.getPath()));
}
log.error("Could not find Asset for {} at {}.", (Object)path, (Object)parentAssetPath);
throw new PersistenceException("Cound not find Asset at " + path);
}
if (parent instanceof CommentsResource && (asset = (Asset)resolver.getResource(((AssetResource)(assetResource = parent.getParent())).getWrappedResourcePath()).adaptTo(Asset.class)) != null) {
String message = (String)properties.get("message");
String author = resolver.getUserID();
String annotationData = (String)properties.get("annotationData");
CommentCollection commentCollection = this.commentManager.getOrCreateCollection((Resource)asset, CommentCollection.class);
Comment comment = commentCollection.addComment(message, author, annotationData);
if (comment != null) {
Resource commentResource = resolver.getResource(comment.getPath());
return this.resourceFactory.createResource(commentResource, this.map(commentResource.getPath()));
}
}
}
throw new PersistenceException("Unable to create resource at " + path);
}
public void delete(ResourceResolver resolver, String path) throws PersistenceException {
log.debug("Asset delete [" + path + "]");
if (!StringUtils.isEmpty((String)path)) {
Resource collectionRoot = resolver.getResource("/content/dam");
if (collectionRoot == null) {
log.error("Collection root not found for {} at {}", (Object)path, (Object)"/content/dam");
throw new PersistenceException("Collection root not found for " + path);
}
Resource res = resolver.getResource("/content/dam" + this.unmap(path));
if (res != null) {
resolver.delete(res);
return;
}
}
throw new PersistenceException("Unable to delete resource at " + path);
}
/*
* 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;
}
static String getResourcePath(String resourcePath, String name) {
String path;
String string = path = resourcePath.endsWith("*") ? resourcePath.replace("*", name) : resourcePath;
if (!path.startsWith("/content/dam/")) {
return "/content/dam" + path;
}
return path;
}
String map(String resourcePath) {
String path = resourcePath.replace("/content/dam", this.apiContextPath);
if (path != null && path.matches(".*?/jcr:content/(renditions|comments|folderThumbnail).*")) {
path = path.replaceFirst("jcr:content/(renditions|comments|folderThumbnail)", "$1");
}
return path;
}
String unmap(String apiPath) {
String path = apiPath.replace(this.apiContextPath, "/content/dam");
if (path != null && path.matches(".*?/(renditions|comments|folderThumbnail).*")) {
return path.replaceFirst("(renditions|comments|folderThumbnail)", "jcr:content/$1");
}
return path;
}
static String getDataPropertyKey(Map<String, Object> properties) {
for (Map.Entry<String, Object> e : properties.entrySet()) {
if ("jcr:data".equals(e.getKey()) || !(e.getValue() instanceof Map)) continue;
return e.getKey();
}
return null;
}
}