AssetResourceConverter.java
9.72 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.rest.converter.ResourceConverter
* com.adobe.granite.rest.converter.ResourceConverterContext
* com.adobe.granite.rest.converter.ResourceConverterException
* com.adobe.granite.rest.converter.siren.AbstractPageableSirenConverter
* com.adobe.reef.siren.Action
* com.adobe.reef.siren.Action$Method
* com.adobe.reef.siren.Entity
* com.adobe.reef.siren.Field
* com.adobe.reef.siren.Field$FieldType
* com.adobe.reef.siren.Link
* com.adobe.reef.siren.builder.ActionBuilder
* com.adobe.reef.siren.builder.BuilderException
* com.adobe.reef.siren.builder.EntityBuilder
* com.adobe.reef.siren.builder.FieldBuilder
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
*/
package com.adobe.granite.rest.assets.impl;
import com.adobe.granite.rest.assets.impl.AbstractAssetResource;
import com.adobe.granite.rest.assets.impl.AssetResource;
import com.adobe.granite.rest.assets.impl.CommentResource;
import com.adobe.granite.rest.assets.impl.CommentsResource;
import com.adobe.granite.rest.assets.impl.RenditionResource;
import com.adobe.granite.rest.assets.impl.RenditionsResource;
import com.adobe.granite.rest.converter.ResourceConverter;
import com.adobe.granite.rest.converter.ResourceConverterContext;
import com.adobe.granite.rest.converter.ResourceConverterException;
import com.adobe.granite.rest.converter.siren.AbstractPageableSirenConverter;
import com.adobe.reef.siren.Action;
import com.adobe.reef.siren.Entity;
import com.adobe.reef.siren.Field;
import com.adobe.reef.siren.Link;
import com.adobe.reef.siren.builder.ActionBuilder;
import com.adobe.reef.siren.builder.BuilderException;
import com.adobe.reef.siren.builder.EntityBuilder;
import com.adobe.reef.siren.builder.FieldBuilder;
import java.security.AccessControlException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
public class AssetResourceConverter
extends AbstractPageableSirenConverter {
private static final String ASSET_SIZE = "asset:size";
private static final String REL_THUMBNAIL = "thumbnail";
public AssetResourceConverter(Resource resource) {
super(resource);
}
protected String[] getClazz() {
return new String[]{"assets/asset"};
}
protected Map<String, Object> getProperties(ResourceConverterContext context, boolean isChild) {
return this.addShowProperties(context, super.getProperties(context, isChild));
}
private Map<String, Object> addShowProperties(ResourceConverterContext context, Map<String, Object> props) {
if (context.getShowProperties() != null) {
AssetResource assetResource = (AssetResource)this.resource;
ValueMap valueMap = assetResource.adaptTo(ValueMap.class);
ValueMap resourceMap = assetResource.getResourceProperties();
for (String property : context.getShowProperties()) {
if ("asset:size".equals(property)) {
RenditionResource original = assetResource.getOriginal();
props.put("asset:size", original.getSize());
continue;
}
if ("jcr:created".equals(property)) {
Object created = resourceMap.get((Object)"jcr:created");
if (created instanceof Calendar) {
Calendar calendar = (Calendar)created;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
props.put("jcr:created", dateFormat.format(calendar.getTime()));
continue;
}
props.put("jcr:created", created);
continue;
}
if ("jcr:createdBy".equals(property)) {
props.put("jcr:createdBy", resourceMap.get((Object)"jcr:createdBy"));
continue;
}
if (!"asset:readonly".equals(property)) continue;
Session userSession = (Session)this.resource.getResourceResolver().adaptTo(Session.class);
boolean readOnly = true;
if (valueMap.containsKey((Object)"cq:drivelock") && !userSession.getUserID().equals(valueMap.get((Object)"cq:drivelock"))) {
readOnly = true;
} else {
readOnly = false;
try {
userSession.checkPermission(this.resource.getPath(), "add_node,set_property,remove");
}
catch (AccessControlException e) {
this.log.debug("access control exception: user doesn't have access to asset", (Throwable)e);
readOnly = true;
}
catch (RepositoryException e) {
this.log.error("repository exception while attempting to check user permissions", (Throwable)e);
readOnly = true;
}
}
props.put("asset:readonly", readOnly);
}
}
return props;
}
protected Map<String, Object> getProperties(ResourceConverterContext context) {
Map props = super.getProperties(context);
if (context.isShowAllProperties() && this.resource instanceof AssetResource) {
props.put("srn:comments", ((AssetResource)this.resource).getComments().size());
}
return this.addShowProperties(context, props);
}
protected List<Link> getLinks(ResourceConverterContext context) throws BuilderException, ResourceConverterException {
List<RenditionResource> renditions;
List links = super.getLinks(context);
AssetResource assetResource = (AssetResource)this.resource;
RenditionResource original = assetResource.getOriginal();
if (original != null) {
links.add(this.getLink("content", this.buildURL(context, original.getPath(), null), null));
}
if (!(renditions = assetResource.getRenditions()).isEmpty()) {
for (RenditionResource rendition : renditions) {
if (!"cq5dam.thumbnail.319.319.png".equals(rendition.getName())) continue;
links.add(this.getLink("thumbnail", this.buildURL(context, rendition.getPath(), null), null));
break;
}
}
return links;
}
protected List<Action> getActions(ResourceConverterContext context) throws BuilderException, ResourceConverterException {
List actions = super.getActions(context);
actions.add(new ActionBuilder().setName("update-metadata").setHref(this.buildURL(context, this.resource.getPath(), null)).setMethod(Action.Method.PUT).setTitle("Update Metadata").setType("application/vnd.siren+json").addField((Field)new FieldBuilder().setName("file").setType(Field.FieldType.FILE).build()).build());
actions.add(new ActionBuilder().setName("update-data").setHref(this.buildURL(context, this.resource.getPath(), null)).setMethod(Action.Method.PUT).setTitle("Update Data").setType("application/octet-stream").addField((Field)new FieldBuilder().setName("data").build()).build());
actions.add(new ActionBuilder().setName("delete").setHref(this.buildURL(context, this.resource.getPath(), null)).setMethod(Action.Method.DELETE).setTitle("Delete").build());
return actions;
}
public List<Entity> getEntities(ResourceConverterContext context, Iterator<Resource> children) throws BuilderException {
LinkedList<AbstractAssetResource> assetChildren = new LinkedList<AbstractAssetResource>();
AssetResource assetResource = (AssetResource)this.resource;
if (!assetResource.getRenditions().isEmpty()) {
assetChildren.add(assetResource.getRenditionsResource());
}
if (!assetResource.getComments().isEmpty()) {
assetChildren.add(assetResource.getCommentsResource());
}
return super.getEntities(context, assetChildren.iterator());
}
protected Entity getEntity(ResourceConverterContext context, Resource child) throws ResourceConverterException {
ResourceConverter converter = (ResourceConverter)child.adaptTo(ResourceConverter.class);
if (converter != null) {
return (Entity)converter.toSubEntity(context);
}
this.log.error("Could not adapt child {} to ResourceConverter", (Object)child);
return null;
}
public Entity toSubEntity(ResourceConverterContext context) throws ResourceConverterException {
try {
AssetResource assetResource = (AssetResource)this.resource;
LinkedList<Link> links = new LinkedList<Link>();
links.add(this.getLink("self", this.buildURL(context, this.resource.getPath(), ".json"), null));
RenditionResource original = assetResource.getOriginal();
if (original != null) {
links.add(this.getLink("content", this.buildURL(context, original.getPath(), null), null));
}
Entity entity = (Entity)new EntityBuilder().setClass(this.getClazz()).setLinks(links).setProperties(this.getProperties(context, true)).build();
return entity;
}
catch (Exception e) {
throw new ResourceConverterException((Throwable)e);
}
}
}