FolderListServlet.java
4.89 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.servlet.ServletException
* 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.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl.servlets;
import com.day.cq.analytics.testandtarget.Folder;
import com.day.cq.analytics.testandtarget.TestandtargetService;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
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.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/cq/analytics/testandtarget/folderlist"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class FolderListServlet
extends SlingAllMethodsServlet {
private final Logger log = LoggerFactory.getLogger(FolderListServlet.class);
private static final String PARAM_PATH = "cfgpath";
@Reference
private TestandtargetService service;
@Reference
private ConfigurationManagerFactory cfgMgrFactory;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
try {
Configuration config = this.cfgMgrFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(request.getParameter("cfgpath"));
if (config != null) {
Folder folder = this.service.listFolders(config);
JSONArray result = new JSONArray();
JSONObject folders = this.getFolderJSON(folder);
result.put((Object)folders);
out.write(result.toString());
}
}
catch (Exception e) {
this.log.error(e.getMessage(), (Throwable)e);
throw new ServletException((Throwable)e);
}
finally {
out.flush();
}
}
private JSONObject getFolderJSON(Folder folder) throws JSONException {
JSONObject json = new JSONObject();
json.put("text", (Object)folder.getName());
json.put("id", (Object)folder.getId());
json.put("leaf", !folder.getChildren().hasNext());
JSONArray children = new JSONArray();
Iterator<Folder> it = folder.getChildren();
while (it.hasNext()) {
Folder subFolder = it.next();
children.put((Object)this.getFolderJSON(subFolder));
}
if (children.length() > 0) {
json.put("children", (Object)children);
}
return json;
}
protected void bindService(TestandtargetService testandtargetService) {
this.service = testandtargetService;
}
protected void unbindService(TestandtargetService testandtargetService) {
if (this.service == testandtargetService) {
this.service = null;
}
}
protected void bindCfgMgrFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.cfgMgrFactory = configurationManagerFactory;
}
protected void unbindCfgMgrFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.cfgMgrFactory == configurationManagerFactory) {
this.cfgMgrFactory = null;
}
}
}