ReferenceListServlet.java
4.77 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.day.cq.commons.LabeledResource
* com.day.cq.commons.TidyJSONWriter
* com.day.cq.commons.servlets.AbstractPredicateServlet
* com.day.text.Text
* javax.servlet.Servlet
* javax.servlet.ServletException
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.extwidget.servlets;
import com.day.cq.commons.LabeledResource;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.commons.servlets.AbstractPredicateServlet;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.selectors", value={"referencelist"}), @Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class ReferenceListServlet
extends AbstractPredicateServlet {
public static final String PARAM_NAME_REFERENCELIST = "referencelist";
public static final String PARAM_NAME_TIDY = "tidy";
public static final String PARAM_NAME_PROPERTYNAME = "propertyname";
private static final Logger log = LoggerFactory.getLogger(ReferenceListServlet.class);
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp, Predicate predicate) throws ServletException, IOException {
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
ResourceResolver resourceResolver = req.getResourceResolver();
TidyJSONWriter writer = new TidyJSONWriter((Writer)resp.getWriter());
writer.setTidy("true".equals(req.getParameter("tidy")));
try {
writer.array();
String[] referenceList = req.getParameterValues("referencelist");
if (req.getParameter("propertyname") != null) {
ValueMap properties = (ValueMap)req.getResource().adaptTo(ValueMap.class);
referenceList = (String[])properties.get(req.getParameter("propertyname"), String[].class);
}
if (referenceList != null) {
for (String path : referenceList) {
Resource resource = resourceResolver.getResource(path);
if (resource == null || predicate != null && !predicate.evaluate((Object)resource)) continue;
writer.object();
LabeledResource lr = (LabeledResource)resource.adaptTo(LabeledResource.class);
String name = Text.getName((String)resource.getPath());
writer.key("path").value((Object)resource.getPath());
writer.key("name").value((Object)name);
if (lr == null) {
writer.key("text").value((Object)name);
} else {
writer.key("text").value((Object)(lr.getTitle() == null ? name : lr.getTitle()));
if (lr.getDescription() != null) {
writer.key("description").value((Object)lr.getDescription());
}
}
writer.key("type").value((Object)resource.getResourceType());
writer.key("cls").value((Object)"file");
writer.key("leaf").value(true);
writer.endObject();
}
}
writer.endArray();
}
catch (JSONException e) {
log.error("error while assembling JSON: ", (Throwable)e);
}
}
}