DialogServlet.java
4.72 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.Property
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletResponse
* 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.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.servlets.HtmlResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.JSONTokener
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.extwidget.servlets;
import java.io.IOException;
import java.util.Iterator;
import javax.jcr.Node;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
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.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HtmlResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"cq/Dialog"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class DialogServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -5743114344802542122L;
private static final Logger log = LoggerFactory.getLogger(DialogServlet.class);
private static final String CQ_DIALOG = "cq:Dialog";
private static final String PARAM_JSON = "json";
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
RequestParameterMap params = request.getRequestParameterMap();
RequestParameter dialog = params.getValue("json");
HtmlResponse htmlResponse = new HtmlResponse();
if (dialog != null) {
try {
JSONObject json = new JSONObject(new JSONTokener(dialog.getString()));
Node node = (Node)request.getResource().adaptTo(Node.class);
Node parent = node.getParent();
String name = node.getName();
node.remove();
node = parent.addNode(name, this.getPrimaryType(json, "cq:Dialog"));
this.storeDialog(json, node);
parent.save();
}
catch (Exception e) {
log.error("Error storing dialog", (Throwable)e);
htmlResponse.setError((Throwable)e);
}
}
htmlResponse.setTitle("Dialog saved successfully");
htmlResponse.setStatus(200, "Dialog saved successfully");
htmlResponse.setPath(request.getRequestPathInfo().getResourcePath());
htmlResponse.setLocation(request.getRequestPathInfo().getResourcePath());
try {
htmlResponse.send((HttpServletResponse)response, true);
}
catch (IOException e) {
this.log("Error while writing response", (Throwable)e);
}
}
private void storeDialog(JSONObject json, Node node) throws Exception {
Iterator keys = json.keys();
while (keys.hasNext()) {
String key = (String)keys.next();
if (key.startsWith("jcr:")) continue;
if (json.optJSONObject(key) != null) {
this.storeDialog(json.getJSONObject(key), node.addNode(key, this.getPrimaryType(json.getJSONObject(key), "nt:unstructured")));
continue;
}
node.setProperty(key, json.getString(key));
}
}
private String getPrimaryType(JSONObject json, String defaultType) throws JSONException {
return json.has("jcr:primaryType") ? json.getString("jcr:primaryType") : defaultType;
}
}