ParagraphServlet.java
7.1 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
/*
* 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.WCMMode
* com.day.cq.wcm.api.components.Component
* com.day.cq.wcm.commons.WCMUtils
* javax.servlet.RequestDispatcher
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.ServletOutputStream
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* 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.servlets.SlingSafeMethodsServlet
* org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper
* org.apache.sling.commons.json.JSONException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.foundation;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMMode;
import com.day.cq.wcm.commons.WCMUtils;
import com.day.cq.wcm.foundation.Paragraph;
import com.day.cq.wcm.foundation.ParagraphSystem;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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.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.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.selectors", value={"paragraphs"}), @Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class ParagraphServlet
extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 2156140435583248698L;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Page page = (Page)request.getResource().adaptTo(Page.class);
if (page == null) {
response.sendError(404, "Page not found: " + request.getResource().getPath());
return;
}
Resource content = page.getContentResource();
if (content == null) {
response.sendError(404, "Content not found: " + page.getPath());
return;
}
try {
new ParagraphWriter(request, response).writeParagraphSystems(content);
}
catch (JSONException e) {
throw new ServletException("Failed to produce JSON output", (Throwable)e);
}
}
private static class ParagraphWriter {
private final Logger logger;
private final Writer writer;
private final TidyJSONWriter json;
private final SlingHttpServletRequest request;
private final SlingHttpServletResponse response;
private long count;
public ParagraphWriter(SlingHttpServletRequest request, SlingHttpServletResponse response) {
this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
this.count = 0;
this.writer = new StringWriter();
this.json = new TidyJSONWriter(this.writer);
this.request = request;
this.response = response;
}
public void writeParagraphSystems(Resource content) throws JSONException, IOException {
WCMMode.DISABLED.toRequest((ServletRequest)this.request);
this.json.object();
this.json.key("paragraphs");
this.json.array();
Iterator iterator = content.getResourceResolver().listChildren(content);
while (iterator.hasNext()) {
Resource resource = (Resource)iterator.next();
com.day.cq.wcm.api.components.Component component = WCMUtils.getComponent((Resource)resource);
if (component == null || !component.isContainer()) continue;
this.writeParagraphs(resource);
}
this.json.endArray();
this.json.key("count").value(this.count);
this.json.endObject();
this.response.setContentType("application/json; charset=UTF-8");
this.response.getWriter().write(this.writer.toString());
}
public void writeParagraphs(Resource container) throws JSONException {
ParagraphSystem system = new ParagraphSystem(container);
for (Paragraph paragraph : system.paragraphs()) {
if (paragraph.getType() != Paragraph.Type.NORMAL) continue;
this.json.object();
this.json.key("path").value((Object)paragraph.getPath());
this.json.key("html").value((Object)this.render(paragraph.getPath()));
this.json.endObject();
++this.count;
}
}
public String render(String path) {
try {
final StringWriter buffer = new StringWriter();
final ServletOutputStream stream = new ServletOutputStream(){
public void write(int b) throws IOException {
buffer.append((char)b);
}
};
SlingHttpServletResponseWrapper wrapper = new SlingHttpServletResponseWrapper(this.response){
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() throws IOException {
return new PrintWriter(buffer);
}
public SlingHttpServletResponse getSlingResponse() {
return super.getSlingResponse();
}
};
RequestDispatcher dispatcher = this.request.getRequestDispatcher(path + ".html");
dispatcher.include((ServletRequest)this.request, (ServletResponse)wrapper);
return buffer.toString();
}
catch (Exception e) {
this.logger.error("Exception occured: " + e.getMessage(), (Throwable)e);
return e.getMessage();
}
}
}
}