NodeServlet.java
4.16 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.security.AccessControlException
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.lang3.StringUtils
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.osgi.framework.BundleContext
* org.slf4j.Logger
*/
package com.day.crx.delite.impl.servlets;
import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.security.AccessControlException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
public class NodeServlet
extends AbstractServlet {
public NodeServlet(BundleContext bc) {
super(bc);
}
protected void doService(HttpServletRequest req, HttpServletResponse res, Session session) throws ServletException, IOException {
if (req.getMethod().equals("GET")) {
this.doGet(req, res, session);
return;
}
res.sendError(405);
}
private void doGet(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
RequestData req = new RequestData(request);
String id = req.getParameter("identifier");
if (StringUtils.isBlank((CharSequence)id)) {
res.sendError(404);
return;
}
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
PrintWriter pw = res.getWriter();
JSONWriter writer = new JSONWriter((Writer)pw);
try {
Node node = session.getNodeByIdentifier(id);
new Printer(writer).print(node);
}
catch (PathNotFoundException e) {
res.sendError(404);
}
catch (AccessControlException e) {
res.sendError(403);
}
catch (RepositoryException e) {
this.logger.error("Error occur getting node by id: " + id, (Throwable)e);
res.sendError(500);
res.getWriter().write(e.getMessage());
}
catch (JSONException e) {
this.logger.error("Error occur getting node by id: " + id, (Throwable)e);
res.sendError(500);
res.getWriter().write(e.getMessage());
}
}
private class Printer {
private JSONWriter writer;
public Printer(JSONWriter writer) {
this.writer = writer;
}
public void print(Node node) throws JSONException, RepositoryException {
this.writer.object();
this.writer.key(":path").value((Object)node.getPath());
this.writer.key(":identifier").value((Object)node.getIdentifier());
PropertyIterator it = node.getProperties();
while (it.hasNext()) {
Property p = it.nextProperty();
if (p.getType() == 2) continue;
if (p.isMultiple()) {
Value[] values = p.getValues();
this.writer.key(p.getName()).array();
for (Value v : values) {
this.writer.value((Object)v.getString());
}
this.writer.endArray();
continue;
}
this.writer.key(p.getName()).value((Object)p.getString());
}
this.writer.endObject();
}
}
}