MissingPagesServlet.java
4.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.TidyJSONWriter
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.msm.api.LiveRelationship
* com.day.cq.wcm.msm.api.LiveRelationshipManager
* com.day.text.Text
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* 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.servlets.SlingSafeMethodsServlet
*/
package com.day.cq.wcm.msm.impl.servlets;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.text.Text;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Reference;
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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
@SlingServlet(resourceTypes={"sling/servlet/default"}, extensions={"json"}, selectors={"missingpages"}, methods={"GET"})
public class MissingPagesServlet
extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1814651477812099101L;
private static final String TIDY_PARAM = "tidy";
@Reference
private LiveRelationshipManager relationshipManager;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resolver = request.getResourceResolver();
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
Resource resource = request.getResource();
StringWriter buf = new StringWriter();
TidyJSONWriter w = new TidyJSONWriter((Writer)buf);
w.setTidy("true".equals(request.getParameter("tidy")));
try {
Page source;
LiveRelationship lr;
w.array();
Page currentPage = (Page)resource.adaptTo(Page.class);
if (currentPage != null && (lr = this.relationshipManager.getLiveRelationship(currentPage.getContentResource(), true)) != null && (source = pageManager.getContainingPage(lr.getSourcePath())) != null) {
Iterator iter = source.listChildren();
while (iter.hasNext()) {
Page child = (Page)iter.next();
if (currentPage.hasChild(child.getName())) continue;
String path = currentPage.getPath() + "/" + child.getName();
w.object();
w.key("path").value((Object)path);
w.key("name").value((Object)Text.getName((String)path));
String title = child.getTitle();
if (title == null || "".equals(title)) {
title = child.getName();
}
w.key("title").value((Object)title);
w.endObject();
}
}
w.endArray();
}
catch (Exception e) {
throw new ServletException("Error while computing skipped pages", (Throwable)e);
}
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(buf.getBuffer().toString());
}
protected void bindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
this.relationshipManager = liveRelationshipManager;
}
protected void unbindRelationshipManager(LiveRelationshipManager liveRelationshipManager) {
if (this.relationshipManager == liveRelationshipManager) {
this.relationshipManager = null;
}
}
}