ConfWebConsolePlugin.java
9.97 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang.StringUtils
* 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.felix.webconsole.AbstractWebConsolePlugin
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
*/
package com.adobe.granite.confmgr.impl;
import com.adobe.granite.confmgr.Conf;
import com.adobe.granite.confmgr.ConfMgr;
import com.adobe.granite.confmgr.impl.ConfImpl;
import com.adobe.granite.confmgr.impl.ConfMgrImpl;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
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.felix.webconsole.AbstractWebConsolePlugin;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
@Component
@Service(value={Servlet.class})
@Properties(value={@Property(name="service.description", value={"Apache Sling Web Console Plugin for /conf Manager"}), @Property(name="felix.webconsole.label", value={"conf"}), @Property(name="felix.webconsole.title", value={"ConfMgr"})})
public class ConfWebConsolePlugin
extends AbstractWebConsolePlugin {
public static final String LABEL = "conf";
public static final String TITLE = "ConfMgr";
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private ConfMgr confMgr;
public String getLabel() {
return "conf";
}
public String getTitle() {
return "ConfMgr";
}
protected void renderContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
this.info(pw, "ConfMgr manages configurations in the JCR repository. Use this tool to test config resolutions.");
this.printConfiguration(pw);
pw.println("<br/>");
this.printResolutionTestTool(request, pw);
}
private void printConfiguration(PrintWriter pw) {
ConfMgrImpl confMgrImpl = (ConfMgrImpl)this.confMgr;
this.tableStart(pw, "Configuration", 2);
pw.println("<tr>");
pw.println("<td style='width:20%'>Allowed paths</td>");
pw.println("<td>" + StringUtils.join((Object[])confMgrImpl.getAllowedPaths(), (String)", ") + "</td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td style='width:20%'>Fallback paths</td>");
pw.println("<td>" + StringUtils.join((Object[])confMgrImpl.getFallbackPaths(), (String)", ") + "</td>");
pw.println("</tr>");
pw.println("<tr>");
pw.println("<td></td>");
pw.println("<td>");
pw.println("<form method='get' action='${appRoot}/configMgr/com.adobe.granite.confmgr.impl.ConfMgrImpl'>");
pw.println("<input type='submit' value='Configure'/>");
pw.println("</form>");
pw.println("</td>");
pw.println("</tr>");
this.tableEnd(pw);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void printResolutionTestTool(HttpServletRequest request, PrintWriter pw) {
String item;
String user;
String path = request.getParameter("path");
if (StringUtils.isEmpty((String)path)) {
path = "/content";
}
if (StringUtils.isEmpty((String)(item = request.getParameter("item")))) {
item = ".";
}
if (StringUtils.isEmpty((String)(user = request.getParameter("user")))) {
user = request.getRemoteUser();
}
boolean runTest = request.getParameter("path") != null;
ResourceResolver resolver = null;
try {
Resource content = null;
if (runTest) {
resolver = this.getResolver(user);
}
if (resolver != null) {
content = resolver.getResource(path);
}
pw.println("<form method='get'>");
this.tableStart(pw, "Test ConfMgr Resolution", 2);
pw.println("<td style='width:20%'>Content Path</td>");
pw.println("<td><input name='path' value='" + path + "' style='width:100%'/>");
if (resolver != null && content == null) {
pw.println("<div>");
pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
pw.println("<span style='float:left'>Path does not exist.</span>");
pw.println("</div>");
}
pw.println("</td>");
this.tableRows(pw);
pw.println("<td>Item</td>");
pw.println("<td><input name='item' value='" + item + "' style='width:100%'/></td>");
this.tableRows(pw);
pw.println("<td>User</td>");
pw.println("<td><input name='user' value='" + user + "' style='width:50%'/>");
if (runTest && resolver == null) {
pw.println("<div>");
pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
pw.println("<span style='float:left'>User does not exist.</span>");
pw.println("</div>");
}
pw.println("</td>");
this.tableRows(pw);
pw.println("<td></td>");
pw.println("<td><input type='submit' value='Resolve'/></td>");
this.tableEnd(pw);
pw.println("</form>");
pw.println("<br/>");
if (runTest && content != null) {
ConfImpl conf = (ConfImpl)this.confMgr.getConf(content);
Resource itemRes = conf.getItemResource(item);
String itemResPath = null;
if (itemRes != null) {
itemResPath = itemRes.getPath();
}
this.tableStart(pw, "Resolved", 2);
pw.println("<td style='width:20%'>Code</td>");
pw.println("<td>");
pw.println("<code>getResource(\"" + content.getPath() + "\").adaptTo(Conf.class).getItem(\"" + item + "\")</code>");
pw.println("<br/> ");
pw.println("</td>");
this.tableRows(pw);
pw.println("<td style='width:20%'>Item</td>");
if (itemResPath != null) {
pw.println("<td>" + itemResPath + "<br/> </td>");
} else {
pw.println("<td>");
pw.println("<div>");
pw.println("<span class='ui-icon ui-icon-alert' style='float:left'></span>");
pw.println("<span style='float:left'>No matching item found.</span>");
pw.println("</div>");
pw.println("<br/> </td>");
}
this.tableRows(pw);
pw.println("<td>Config paths</td>");
pw.println("<td>");
for (String p : conf.getPaths()) {
if (itemResPath != null && itemResPath.startsWith(p + "/" + "settings")) {
pw.println("<b>" + p + "</b>");
} else {
pw.println(p);
}
pw.println("<br/>");
}
pw.println("</td>");
this.tableEnd(pw);
}
}
finally {
if (resolver != null) {
resolver.close();
}
}
}
private void info(PrintWriter pw, String text) {
pw.println("<p class='statline ui-state-highlight'>" + text + "</p>");
}
private void tableStart(PrintWriter pw, String title, int colspan) {
pw.println("<table class='nicetable ui-widget'>");
pw.println("<thead class='ui-widget-header'>");
pw.println("<tr>");
pw.println("<th colspan=" + colspan + ">" + title + "</th>");
pw.println("</tr>");
pw.println("</thead>");
pw.println("<tbody class='ui-widget-content'>");
pw.println("<tr>");
}
private void tableEnd(PrintWriter pw) {
pw.println("</tr>");
pw.println("</tbody>");
pw.println("</table>");
}
private void tableRows(PrintWriter pw) {
pw.println("</tr>");
pw.println("<tr>");
}
private ResourceResolver getResolver(final String user) {
try {
return this.resolverFactory.getAdministrativeResourceResolver((Map)new HashMap<String, Object>(){});
}
catch (LoginException e) {
return null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindConfMgr(ConfMgr confMgr) {
this.confMgr = confMgr;
}
protected void unbindConfMgr(ConfMgr confMgr) {
if (this.confMgr == confMgr) {
this.confMgr = null;
}
}
}