AbstractSirenConverter.java
11.7 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.reef.siren.Action
* com.adobe.reef.siren.Entity
* com.adobe.reef.siren.Link
* com.adobe.reef.siren.builder.BuilderException
* com.adobe.reef.siren.builder.EntityBuilder
* com.adobe.reef.siren.builder.LinkBuilder
* org.apache.commons.lang.ArrayUtils
* org.apache.jackrabbit.util.ISO8601
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.rest.converter.siren;
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.filter.Filter;
import com.adobe.granite.rest.utils.Resources;
import com.adobe.granite.rest.utils.URIUtils;
import com.adobe.reef.siren.Action;
import com.adobe.reef.siren.Entity;
import com.adobe.reef.siren.Link;
import com.adobe.reef.siren.builder.BuilderException;
import com.adobe.reef.siren.builder.EntityBuilder;
import com.adobe.reef.siren.builder.LinkBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.ArrayUtils;
import org.apache.jackrabbit.util.ISO8601;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractSirenConverter
implements ResourceConverter<Entity> {
protected static final String[] PREFIX_ALLOWED_SHORT = new String[]{"dc"};
protected static final String[] PREFIX_ALLOWED = new String[]{"dc", "cq", "crs", "xmp"};
public static final String PREFIX_SRN = "srn:";
protected Logger log;
protected Resource resource;
private Collection<Resource> children;
public static final String REL_SELF = "self";
public static final String REL_CHILD = "child";
public static final String REL_CONTENT = "content";
public static final String REL_NEXT = "next";
public static final String REL_PREV = "prev";
public static final String REL_PARENT = "parent";
public AbstractSirenConverter(Resource resource) {
this.log = LoggerFactory.getLogger(this.getClass());
this.resource = resource;
}
protected Collection<Resource> listChildren() {
if (this.children == null) {
this.children = new LinkedList<Resource>();
Iterator it = this.resource.listChildren();
while (it.hasNext()) {
Resource resource = (Resource)it.next();
this.children.add(resource);
}
}
return this.children;
}
protected boolean isAllowedPrefix(String key, String[] allowedPrefixes) {
return this.isAllowedPrefix(key, null, allowedPrefixes);
}
protected boolean isAllowedPrefix(String key, ResourceConverterContext context, String[] allowedPrefixes) {
boolean allowed;
if (key.charAt(0) == '_') {
return false;
}
if (context != null && context.getShowProperties() != null && context.getShowProperties().length > 0 && (allowed = ArrayUtils.contains((Object[])context.getShowProperties(), (Object)key))) {
return allowed;
}
String prefix = "";
int pos = key.indexOf(58);
if (pos < 0) {
return true;
}
prefix = key.substring(0, pos);
for (String p : allowedPrefixes) {
if (!p.equals(prefix)) continue;
return true;
}
return false;
}
protected abstract String[] getClazz();
protected Map<String, Object> getProperties(ResourceConverterContext context) {
return this.getProperties(context, false);
}
protected Map<String, Object> getProperties(ResourceConverterContext context, boolean isChild) {
ValueMap valueMap = (ValueMap)this.resource.adaptTo(ValueMap.class);
Map<String, Object> props = this.getProperties((Map<String, Object>)valueMap, context, isChild);
props.put("name", this.resource.getName());
return props;
}
private Map<String, Object> getProperties(Map<String, Object> properties, ResourceConverterContext context, boolean isChild) {
HashMap<String, Object> props = new HashMap<String, Object>();
if (properties != null) {
for (String key : properties.keySet()) {
boolean isAllowed = this.isAllowedPrefix(key, context, PREFIX_ALLOWED);
if (isChild && !context.isShowAllProperties()) {
isAllowed = this.isAllowedPrefix(key, context, PREFIX_ALLOWED_SHORT);
}
if (!isAllowed) continue;
Map<String, Object> value = properties.get(key);
if (value instanceof Calendar) {
value = ISO8601.format((Calendar)((Calendar)((Object)value)));
}
if (value instanceof ValueMap) {
value = this.getProperties((Map<String, Object>)((ValueMap)value), context, isChild);
}
props.put(key, value);
}
}
return props;
}
protected List<Entity> getEntities(ResourceConverterContext context, Iterator<Resource> children) throws BuilderException {
return new LinkedList<Entity>();
}
protected List<Entity> getEntities(ResourceConverterContext context) throws BuilderException {
return this.getEntities(context, this.listChildren().iterator());
}
protected Entity getEntity(String[] clazz, String title, List<Action> actions, List<Entity> entities, List<Link> links, Map<String, Object> properties) throws BuilderException {
Entity entity = (Entity)new EntityBuilder().setClass(clazz).setTitle(title).setActions(actions).setEntities(entities).setLinks(links).setProperties(properties).build();
return entity;
}
protected List<Link> getLinks(ResourceConverterContext context) throws BuilderException, ResourceConverterException {
HashMap<String, String[]> pagingParameters = new HashMap<String, String[]>();
pagingParameters.putAll(context.getParameters());
LinkedList<Link> links = new LinkedList<Link>();
links.add(this.getLink("self", this.buildURL(context, this.resource.getPath(), ".json", pagingParameters), null));
return links;
}
protected Link getLink(String[] rel, String href, String type) throws BuilderException {
Link link = (Link)new LinkBuilder().setRel(rel).setHref(href).setType(type).build();
return link;
}
protected Link getLink(String rel, String href, String type) throws BuilderException {
return this.getLink(new String[]{rel}, href, type);
}
protected List<Action> getActions(ResourceConverterContext context) throws BuilderException, ResourceConverterException {
return new LinkedList<Action>();
}
@Override
public Entity toEntity(ResourceConverterContext context) throws ResourceConverterException {
try {
ResourceConverterContext ctx = context;
Entity entity = (Entity)new EntityBuilder().setClass(this.getClazz()).setProperties(this.getProperties(ctx)).setEntities(this.getEntities(ctx)).setLinks(this.getLinks(ctx)).setActions(this.getActions(ctx)).build();
return entity;
}
catch (BuilderException e) {
throw new ResourceConverterException((Throwable)e);
}
}
@Override
public Entity toSubEntity(ResourceConverterContext context) throws ResourceConverterException {
try {
LinkedList<Link> links = new LinkedList<Link>();
links.add(this.getLink("self", this.buildURL(context, this.resource.getPath(), ".json"), null));
Entity entity = (Entity)new EntityBuilder().setClass(this.getClazz()).setRel(new String[]{"child"}).setProperties(this.getProperties(context, true)).setLinks(links).build();
return entity;
}
catch (BuilderException e) {
throw new ResourceConverterException((Throwable)e);
}
}
protected String buildURL(ResourceConverterContext context, String resourcePath, String extension) throws ResourceConverterException {
return this.buildURL(context, resourcePath, extension, null);
}
protected String getNextPageURL(ResourceConverterContext context) throws ResourceConverterException {
int offset = context.getOffset();
int limit = context.getLimit();
Filter<Resource> filter = context.getFilter();
if (offset + limit >= Resources.getSize(this.listChildren().iterator(), filter)) {
return null;
}
return this.buildPagingURL(context, offset += limit);
}
protected String getPrevPageURL(ResourceConverterContext context) throws ResourceConverterException {
int offset = context.getOffset();
if (offset == 0) {
return null;
}
int limit = context.getLimit();
if ((offset -= limit) < 0) {
offset = 0;
}
return this.buildPagingURL(context, offset);
}
private String buildPagingURL(ResourceConverterContext context, int offset) throws ResourceConverterException {
HashMap<String, String[]> pagingParameters = new HashMap<String, String[]>();
pagingParameters.putAll(context.getParameters());
pagingParameters.put("offset", new String[]{Integer.toString(offset)});
pagingParameters.put("limit", new String[]{Integer.toString(context.getLimit())});
return this.buildURL(context, this.resource.getPath(), ".json", pagingParameters);
}
private String buildURL(ResourceConverterContext context, String resourcePath, String extension, Map<String, String[]> additionalParameters) throws ResourceConverterException {
StringBuilder url = new StringBuilder();
if (context.isAbsolutURI()) {
url.append(context.getScheme());
url.append("://");
url.append(context.getServerName());
if ("http".equals(context.getScheme()) && context.getServerPort() != 80 || "https".equals(context.getScheme()) && context.getServerPort() != 443) {
url.append(":");
url.append(context.getServerPort());
}
} else {
resourcePath = URIUtils.relativize(context.getRequestPathInfo(), resourcePath);
}
url.append(URIUtils.urlEncodePath(resourcePath));
if (extension != null) {
url.append(extension);
}
try {
HashMap<String, String[]> parameters = new HashMap<String, String[]>();
if (additionalParameters != null) {
parameters.putAll(additionalParameters);
}
boolean first = true;
for (String param : parameters.keySet()) {
for (String value : (String[])parameters.get(param)) {
if (!first) {
url.append('&');
} else {
url.append('?');
first = false;
}
url.append(URLEncoder.encode(param, "UTF-8"));
url.append('=');
url.append(URLEncoder.encode(value, "UTF-8"));
}
}
}
catch (UnsupportedEncodingException ignored) {
this.log.error("URL encoding failed", (Throwable)ignored);
}
return url.toString();
}
}