BlueprintServlet.java
11.7 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
258
259
260
261
262
263
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.ResourceStatusProvider
* com.day.cq.commons.TidyJSONWriter
* com.day.cq.commons.servlets.AbstractPredicateServlet
* com.day.cq.commons.servlets.HtmlStatusResponseHelper
* com.day.cq.wcm.api.WCMException
* com.day.cq.wcm.msm.api.BlueprintManagerFactory
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.cq.wcm.msm.api.RolloutConfig
* com.day.cq.wcm.msm.api.RolloutManager
* com.day.cq.wcm.msm.api.RolloutManager$Trigger
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RangeIterator
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletResponse
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.msm.impl.servlets;
import com.day.cq.commons.ResourceStatusProvider;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.commons.servlets.AbstractPredicateServlet;
import com.day.cq.commons.servlets.HtmlStatusResponseHelper;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.BlueprintManagerFactory;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.cq.wcm.msm.impl.BlueprintImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerFactoryImpl;
import com.day.cq.wcm.msm.impl.BlueprintManagerImpl;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RangeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service(value={ResourceStatusProvider.class, Servlet.class})
@SlingServlet(generateService=0, resourceTypes={"sling/servlet/default"}, selectors={"blueprint"}, extensions={"conf", "json"}, methods={"POST", "GET"})
public class BlueprintServlet
extends AbstractPredicateServlet
implements ResourceStatusProvider {
private static final long serialVersionUID = 1739398700022982003L;
private static final Logger log = LoggerFactory.getLogger(BlueprintServlet.class);
private static final String TIDY_PARAM = "tidy";
private static final String RETURN_RELATIONSHIPS_PARAM = "returnRelationships";
private static final String DISPLAY_LIMIT = "maxSize";
private static final String START = "start";
private static final String TRIGGER = "msm:trigger";
@org.apache.felix.scr.annotations.Property(propertyPrivate=1)
private static final long DEFAULT_LIST_LIMIT = 2500;
@Reference
private LiveRelationshipManager relationshipManager = null;
@Reference(referenceInterface=BlueprintManagerFactory.class, target="(service.pid=com.day.cq.wcm.msm.impl.BlueprintManagerFactoryImpl)")
private BlueprintManagerFactoryImpl blueprintManagerFactory = null;
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp, Predicate predicate) throws ServletException, IOException {
StringWriter buf = new StringWriter();
TidyJSONWriter writer = new TidyJSONWriter((Writer)buf);
writer.setTidy("true".equals(req.getParameter("tidy")));
boolean writeRelations = "true".equals(req.getParameter("returnRelationships"));
long skip = BlueprintServlet.readIntParameter(req, "start", 0);
long limit = BlueprintServlet.readIntParameter(req, "maxSize", 2500);
RolloutManager.Trigger triggerFilter = null;
if (req.getRequestParameter("msm:trigger") != null) {
try {
triggerFilter = RolloutManager.Trigger.fromName((String)req.getRequestParameter("msm:trigger").getString());
}
catch (IllegalArgumentException ignore) {
// empty catch block
}
}
try {
Resource resource = req.getResource();
RangeIterator relations = this.relationshipManager.getLiveRelationships(resource, null, triggerFilter);
Node sourceNode = Utils.getNode(req.getResourceResolver(), resource.getPath());
BlueprintServlet.writeBlueprintInfo((JSONWriter)writer, relations, sourceNode, writeRelations, skip, limit);
}
catch (Exception e) {
throw new ServletException((Throwable)e);
}
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print(buf.getBuffer().toString());
}
public void writeStatus(SlingHttpServletRequest request, JSONWriter writer, Resource resource) throws JSONException {
writer.key("blueprint");
try {
RangeIterator relations = this.relationshipManager.getLiveRelationships(resource, null, null);
Node sourceNode = Utils.getNode(resource.getResourceResolver(), resource.getPath());
BlueprintServlet.writeBlueprintInfo(writer, relations, sourceNode, false, 0, 2500);
}
catch (RepositoryException e) {
log.error("Error during retrieval of blueprint info.", (Throwable)e);
}
catch (WCMException e) {
log.error("Error during retrieval of blueprint info.", (Throwable)e);
}
}
private static void writeBlueprintInfo(JSONWriter writer, RangeIterator relations, Node sourceNode, boolean writeRelations, long skip, long limit) throws JSONException, RepositoryException {
writer.object();
writer.key("msm:targets");
writer.array();
limit += skip;
long total = 0;
try {
while (relations.hasNext()) {
long pos = relations.getPosition();
LiveRelationship relation = (LiveRelationship)relations.next();
if (pos >= skip) {
if (writeRelations) {
writer.object();
writer.key("msm:targetPath").value((Object)relation.getTargetPath());
writer.key("msm:liveRelationship");
relation.write(writer);
writer.endObject();
} else {
writer.value((Object)Text.getRelativeParent((String)relation.getTargetPath(), (int)1));
}
}
if (pos + 1 != limit) continue;
break;
}
if ((total = relations.getSize()) < 0) {
total = relations.getPosition();
if (relations.hasNext()) {
++total;
}
}
}
catch (Exception e) {
log.error("Error computing relationship", (Throwable)e);
}
writer.endArray();
writer.key("total").value(total);
writer.key("cq:rolloutConfigs");
writer.array();
Node configNode = null;
if (sourceNode != null && sourceNode.hasNode("cq:BlueprintSyncConfig")) {
configNode = sourceNode.getNode("cq:BlueprintSyncConfig");
}
if (configNode != null) {
Value[] values;
for (Value v : values = configNode.getProperty("cq:rolloutConfigs").getValues()) {
writer.value((Object)v.getString());
}
}
writer.endArray();
writer.endObject();
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
HtmlResponse htmlResponse;
try {
Resource resource = request.getResource();
String[] rolloutConfigs = request.getParameterValues("cq:rolloutConfigs");
if (rolloutConfigs != null && rolloutConfigs.length == 1 && "".equals(rolloutConfigs[0])) {
rolloutConfigs = null;
}
Resource configResource = resource.getName().equals("jcr:content") ? resource.getParent() : resource;
ResourceResolver resolver = request.getResourceResolver();
BlueprintManagerImpl bpm = this.blueprintManagerFactory.getBlueprintManager(resolver);
BlueprintImpl blueprint = bpm.getContainingBlueprint(configResource.getPath());
if (blueprint != null) {
String relPath = "";
if (configResource.getPath().length() > blueprint.getSitePath().length()) {
relPath = relPath + configResource.getPath().substring(blueprint.getSitePath().length() + 1);
}
blueprint.setRolloutConfigs(relPath, rolloutConfigs);
resolver.commit();
}
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)true, (String)"Blueprint config updated");
}
catch (Exception e) {
log.error("Error while updating blueprint config", (Throwable)e);
htmlResponse = HtmlStatusResponseHelper.createStatusResponse((boolean)false, (String)("Error while updating blueprint config: " + e.getMessage()));
}
htmlResponse.send((HttpServletResponse)response, true);
}
public void bindBlueprintManagerFactory(BlueprintManagerFactory factory) {
this.blueprintManagerFactory = (BlueprintManagerFactoryImpl)factory;
}
public void unbindBlueprintManagerFactory(BlueprintManagerFactory factory) {
this.blueprintManagerFactory = null;
}
private static long readIntParameter(SlingHttpServletRequest req, String parameterName, long defaultValue) {
if (req.getRequestParameterMap().containsKey((Object)parameterName)) {
try {
return Long.valueOf(req.getRequestParameter(parameterName).getString());
}
catch (NumberFormatException ignore) {
// empty catch block
}
}
return defaultValue;
}
protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.relationshipManager = liveRelationshipManager;
}
protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.relationshipManager == liveRelationshipManager) {
this.relationshipManager = null;
}
}
}