AbstractPageableSirenConverter.java
3.78 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.reef.siren.Entity
* com.adobe.reef.siren.Link
* com.adobe.reef.siren.builder.BuilderException
* org.apache.sling.api.resource.Resource
* org.json.JSONException
* org.json.JSONObject
* org.slf4j.Logger
*/
package com.adobe.granite.rest.converter.siren;
import com.adobe.granite.rest.converter.ResourceConverterContext;
import com.adobe.granite.rest.converter.ResourceConverterException;
import com.adobe.granite.rest.converter.siren.AbstractSirenConverter;
import com.adobe.granite.rest.filter.Filter;
import com.adobe.granite.rest.filter.impl.FilteredIterator;
import com.adobe.granite.rest.utils.Resources;
import com.adobe.reef.siren.Entity;
import com.adobe.reef.siren.Link;
import com.adobe.reef.siren.builder.BuilderException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
public abstract class AbstractPageableSirenConverter
extends AbstractSirenConverter {
public AbstractPageableSirenConverter(Resource resource) {
super(resource);
}
@Override
protected List<Link> getLinks(ResourceConverterContext context) throws BuilderException, ResourceConverterException {
List<Link> links = super.getLinks(context);
Resource parent = this.resource.getParent();
if (parent != null) {
links.add(this.getLink("parent", this.buildURL(context, parent.getPath(), ".json"), null));
}
return links;
}
protected abstract Entity getEntity(ResourceConverterContext var1, Resource var2) throws ResourceConverterException;
@Override
protected List<Entity> getEntities(ResourceConverterContext context, Iterator<Resource> children) throws BuilderException {
LinkedList<Entity> entities = new LinkedList<Entity>();
int offset = context.getOffset();
int limit = context.getLimit();
this.log.debug("offset = " + offset + ", limit = " + limit);
int i = 0;
int n = 0;
Filter<Resource> filter = context.getFilter();
if (filter != null) {
children = new FilteredIterator<Resource>(children, filter);
}
Iterator<Resource> it = children;
while (it.hasNext()) {
Resource childResource = it.next();
if (i >= offset) {
if (n >= limit) break;
try {
Entity entity = this.getEntity(context, childResource);
if (entity != null) {
entities.add(entity);
++n;
}
}
catch (ResourceConverterException e) {
this.log.error("A conversion error occurred for resource " + (Object)childResource, (Throwable)e);
}
}
++i;
}
return entities;
}
@Override
protected Map<String, Object> getProperties(ResourceConverterContext context, boolean isChild) {
Map<String, Object> props = super.getProperties(context, isChild);
if (!isChild) {
JSONObject pagingInfo = new JSONObject();
try {
pagingInfo.put("limit", context.getLimit());
pagingInfo.put("offset", context.getOffset());
pagingInfo.put("total", Resources.getSize(this.listChildren().iterator(), context.getFilter()));
props.put("srn:paging", (Object)pagingInfo);
}
catch (JSONException e) {
this.log.error("Could not add paging info to properties", (Throwable)e);
}
}
return props;
}
}