OriginalPageEditorServlet.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.Servlet
* 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.ReferencePolicy
* 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.commons.html.HtmlParser
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.siteimporter.internal.servlet;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.siteimporter.ProgressTracker;
import com.day.cq.wcm.siteimporter.internal.servlet.SiteImporterServletBase;
import com.day.cq.wcm.siteimporter.internal.util.Utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
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.commons.html.HtmlParser;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@Component(metatype=0, label="Original Page Editor", description="Edits original page")
@Service(value={Servlet.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="sling.servlet.paths", propertyPrivate=1, value={"/libs/wcm/bin/siteimporter/editorigpage"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", propertyPrivate=1, value={"POST"}), @org.apache.felix.scr.annotations.Property(name="service.description", value={"Original Page Editor"})})
public class OriginalPageEditorServlet
extends SiteImporterServletBase {
private static final long serialVersionUID = 9112167990766136591L;
private static final Logger log = LoggerFactory.getLogger(OriginalPageEditorServlet.class);
@Reference(policy=ReferencePolicy.STATIC)
private HtmlParser parser;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
ResourceResolver resolver = request.getResourceResolver();
String path = request.getParameter("path");
String id = request.getParameter("id");
String newid = request.getParameter("newid");
String content = request.getParameter("content");
String operation = request.getParameter("operation");
if ("insert".equalsIgnoreCase(operation) && id != null && newid != null && content != null) {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new ServletException("path " + path + " not found");
}
this.makeBackup(resource);
this.insertElementAfterID(resolver, path, id, newid, content);
} else if ("remove".equalsIgnoreCase(operation) && id != null) {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new ServletException("path " + path + " not found");
}
this.makeBackup(resource);
this.removeElementID(resolver, path, id);
} else if ("restore".equalsIgnoreCase(operation)) {
Resource resource = resolver.getResource(path + ".bk");
if (resource != null) {
this.restoreBackup(resource);
}
} else {
throw new ServletException("invalid parameters");
}
}
protected void makeBackup(Resource res) throws ServletException {
if (res.getResourceResolver().getResource(res.getPath() + ".bk") != null) {
return;
}
Node node = (Node)res.adaptTo(Node.class);
try {
JcrUtil.copy((Node)node, (Node)node.getParent(), (String)(node.getName() + ".bk"));
node.getParent().getSession().save();
}
catch (Exception ex) {
log.error("error while making backup of " + (Object)res, (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
protected void restoreBackup(Resource res) throws ServletException {
Node node = (Node)res.adaptTo(Node.class);
try {
JcrUtil.copy((Node)node, (Node)node.getParent(), (String)node.getName().replaceAll("\\.bk$", ""));
node.getParent().getSession().save();
}
catch (Exception ex) {
log.error("error while restoring backup of " + (Object)res, (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
protected void insertElementAfterID(ResourceResolver resolver, String path, String id, String newid, String content) throws ServletException {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new ServletException("path " + path + " not found");
}
Resource res = resolver.getResource(path + "/jcr:content");
if (res == null) {
throw new ServletException("path " + path + "/jcr:content not found");
}
Node data = (Node)res.adaptTo(Node.class);
String html = null;
try {
html = data.getProperty("jcr:data").getString();
}
catch (Exception ex) {
log.error("unable to get jcr:data from " + path);
return;
}
String charset = this.getCharSet(resource);
ProgressTracker out = this.getOutput();
Document doc = Utils.parse(html, charset, this.parser, out);
XPath xpath = XPathFactory.newInstance().newXPath();
Element el = null;
try {
el = (Element)xpath.evaluate("//*[@componentinsertid='" + id + "']", doc, XPathConstants.NODE);
}
catch (XPathExpressionException e) {
out.error("Couldn't get element: " + e.getMessage(), e);
}
if (el == null) {
out.error("element " + id + " not found");
return;
}
Element newElement = doc.createElement("div");
newElement.setAttribute("componentinsertid", newid);
newElement.setTextContent(content);
el.getParentNode().insertBefore(newElement, el.getNextSibling());
String newhtml = this.makeHTML(doc, charset);
try {
data.getProperty("jcr:data").setValue(newhtml);
data.getProperty("jcr:lastModified").setValue((Calendar)new GregorianCalendar());
data.getSession().save();
}
catch (Exception ex) {
out.error("unable to set jcr:data to " + path);
}
}
protected String getCharSet(Resource resource) throws ServletException {
try {
Node node = (Node)resource.adaptTo(Node.class);
Node parent = node.getParent();
return parent.getProperty("cqProjectCharset").getString();
}
catch (Exception ex) {
log.error("unable to extract charset " + (Object)resource, (Throwable)ex);
return "UTF-8";
}
}
protected String makeHTML(Document doc, String charset) {
ProgressTracker out = this.getOutput();
DOMSource source = new DOMSource(doc);
ByteArrayOutputStream transContent = new ByteArrayOutputStream();
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer xformer = tf.newTransformer();
if (charset != null) {
xformer.setOutputProperty("encoding", charset);
}
xformer.setOutputProperty("omit-xml-declaration", "yes");
xformer.transform(source, new StreamResult(transContent));
}
catch (Exception e) {
out.error("Couldn't parse original HTML: " + e.getMessage(), e);
return null;
}
if (charset != null) {
try {
return new String(transContent.toByteArray(), charset);
}
catch (UnsupportedEncodingException e) {
return new String(transContent.toByteArray());
}
}
return new String(transContent.toByteArray());
}
protected void removeElementID(ResourceResolver resolver, String path, String id) throws ServletException {
Resource resource = resolver.getResource(path);
if (resource == null) {
throw new ServletException("path " + path + " not found");
}
Resource res = resolver.getResource(path + "/jcr:content");
if (res == null) {
throw new ServletException("path " + path + "/jcr:content not found");
}
Node data = (Node)res.adaptTo(Node.class);
String html = null;
try {
html = data.getProperty("jcr:data").getString();
}
catch (Exception ex) {
log.error("unable to get jcr:data from " + path);
return;
}
String charset = this.getCharSet(resource);
ProgressTracker out = this.getOutput();
Document doc = Utils.parse(html, charset, this.parser, out);
XPath xpath = XPathFactory.newInstance().newXPath();
Element el = null;
try {
el = (Element)xpath.evaluate("//*[@componentinsertid='" + id + "']", doc, XPathConstants.NODE);
}
catch (XPathExpressionException e) {
out.error("Couldn't get element: " + e.getMessage(), e);
}
if (el == null) {
out.error("element " + id + " not found");
return;
}
el.getParentNode().removeChild(el);
String newhtml = this.makeHTML(doc, charset);
try {
data.getProperty("jcr:data").setValue(newhtml);
data.getProperty("jcr:lastModified").setValue((Calendar)new GregorianCalendar());
data.getSession().save();
}
catch (Exception ex) {
out.error("unable to set jcr:data to " + path);
}
}
protected void activate(ComponentContext context) throws RepositoryException {
}
protected void bindParser(HtmlParser htmlParser) {
this.parser = htmlParser;
}
protected void unbindParser(HtmlParser htmlParser) {
if (this.parser == htmlParser) {
this.parser = null;
}
}
}