SitecatalystComponentServlet.java
9.36 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.api.components.ComponentEditConfig
* com.day.cq.wcm.api.components.VirtualComponent
* com.day.text.Text
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* 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.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.impl.servlets;
import com.day.cq.analytics.impl.ComponentFilter;
import com.day.cq.analytics.impl.ComponentService;
import com.day.cq.wcm.api.components.ComponentEditConfig;
import com.day.cq.wcm.api.components.VirtualComponent;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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.Reference;
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.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.paths", value={"/libs/cq/analytics/sitecatalystcomponent"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class SitecatalystComponentServlet
extends SlingSafeMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(SitecatalystComponentServlet.class);
private static final String ICON = "icon";
private static final String THUMBNAIL = "thumbnail";
private static final String DESCRIPTION = "description";
private static final String RESOURCE_TYPE = "resourceType";
private static final String DIV_ATTRIBUTES = "divAttributes";
private static final String TITLE = "title";
private static final String GROUP = "group";
private static final String PATH = "path";
private static final String VIRTUAL = "virtual";
private static final String DIALOG = "dialog";
private static final String CONFIG = "config";
private static final String TEMPLATE_PATH = "templatePath";
private static final String CELL_NAMES = "cellNames";
private static final String IS_CONTAINER = "isContainer";
private static final String CQ_COMPONENT_GROUP = "cq:componentGroup";
private static final String CQ_COMPONENT_NAME = "cq:componentName";
@Reference
private ComponentService componentService;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
ResourceResolver resolver = request.getResourceResolver();
String prefix = request.getContextPath();
if (prefix == null) {
prefix = "";
}
ComponentFilter componentFilter = ComponentFilter.fromRequest((HttpServletRequest)request);
String json = this.collectComponents(resolver, this.componentService.getComponents(resolver, componentFilter), prefix);
response.getWriter().write(json);
log.debug("Provided components for mode {} ", (Object)componentFilter);
}
catch (JSONException e) {
log.error("Error while generating component json", (Throwable)e);
throw new ServletException(e.getMessage(), (Throwable)e);
}
}
private String collectComponents(ResourceResolver resolver, Collection<com.day.cq.wcm.api.components.Component> components, String prefix) throws JSONException {
StringWriter ret = new StringWriter();
JSONWriter w = new JSONWriter((Writer)ret);
w.object();
for (com.day.cq.wcm.api.components.Component c : components) {
String dialogPath;
ComponentEditConfig ed;
String templatePath;
String key = c.getResourceType();
if (c instanceof VirtualComponent) {
String p = c.getPath();
key = p.substring(p.indexOf("/", 1) + 1);
}
w.key(key).object();
w.key("path").value((Object)c.getPath());
String title = null;
String group = null;
Resource analyticsResource = resolver.getResource(c.getPath() + "/analytics");
if (analyticsResource != null) {
ValueMap analyticsProperties = (ValueMap)analyticsResource.adaptTo(ValueMap.class);
group = (String)analyticsProperties.get("cq:componentGroup", null);
title = (String)analyticsProperties.get("cq:componentName", null);
}
if (group == null) {
group = c.getComponentGroup();
}
if (group != null) {
w.key("group").value((Object)group);
}
if (title == null || title.trim().length() == 0) {
title = c.getTitle();
}
if (title == null) {
title = Text.getName((String)c.getPath());
}
if (c instanceof VirtualComponent) {
w.key("virtual").value(true);
}
w.key("title").value((Object)title);
w.key("resourceType").value((Object)c.getResourceType());
this.writeDivAttributes(w, c);
if (c.getDescription() != null) {
w.key("description").value((Object)c.getDescription());
}
if (c.getThumbnailPath() != null) {
w.key("thumbnail").value((Object)(prefix + c.getThumbnailPath()));
}
if (c.getIconPath() != null) {
w.key("icon").value((Object)(prefix + c.getIconPath()));
}
if ((ed = c.getEditConfig()) != null && !ed.isDefault()) {
w.key("config");
ed.write(w);
}
if ((dialogPath = c.getDialogPath()) != null) {
if (!dialogPath.endsWith(".json")) {
dialogPath = dialogPath + ".infinity.json";
}
w.key("dialog").value((Object)dialogPath);
}
if ((templatePath = c.getTemplatePath()) != null) {
w.key("templatePath").value((Object)templatePath);
}
w.key("cellNames");
w.array();
w.value((Object)c.getCellName());
for (com.day.cq.wcm.api.components.Component component = c.getSuperComponent(); component != null; component = component.getSuperComponent()) {
w.value((Object)component.getName());
}
w.endArray();
if (c.isContainer()) {
w.key("isContainer").value(c.isContainer());
}
w.endObject();
}
w.endObject();
return ret.toString();
}
private void writeDivAttributes(JSONWriter w, com.day.cq.wcm.api.components.Component c) throws JSONException {
HashMap<String, String> attrs = new HashMap<String, String>(c.getHtmlTagAttributes());
if (!attrs.containsKey("class")) {
StringBuffer clazz = new StringBuffer();
String delim = "";
for (com.day.cq.wcm.api.components.Component cc = c; cc != null; cc = cc.getSuperComponent()) {
String name = cc.getCellName();
if (name.length() <= 0) continue;
clazz.append(delim);
clazz.append(name);
delim = " ";
}
if (clazz.length() > 0) {
attrs.put("class", clazz.toString());
}
}
if (!attrs.isEmpty()) {
w.key("divAttributes").object();
for (Map.Entry e : attrs.entrySet()) {
w.key((String)e.getKey()).value(e.getValue());
}
w.endObject();
}
}
protected void bindComponentService(ComponentService componentService) {
this.componentService = componentService;
}
protected void unbindComponentService(ComponentService componentService) {
if (this.componentService == componentService) {
this.componentService = null;
}
}
}