FullNodeHitWriter.java
3.39 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Component
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.apache.sling.commons.json.jcr.JsonItemWriter
*/
package com.day.cq.search.writer;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.result.Hit;
import com.day.cq.search.writer.ResultHitWriter;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.commons.json.jcr.JsonItemWriter;
@Component(metatype=0, factory="com.day.cq.search.writer.ResultHitWriter/full")
public class FullNodeHitWriter
implements ResultHitWriter {
@Override
public void write(Hit hit, JSONWriter writer, Query query) throws RepositoryException, JSONException {
int maxRecursionLevels = Integer.parseInt(query.getPredicates().get("nodedepth", "0"));
boolean writeACLs = query.getPredicates().getBool("acls");
writer.key("jcr:path").value((Object)hit.getPath());
new JsonNodeDumper(writeACLs).dumpObject(hit.getNode(), writer, 0, maxRecursionLevels);
}
private static class JsonNodeDumper
extends JsonItemWriter {
private boolean writeACLs = false;
public JsonNodeDumper(boolean writeACLs) {
super(null);
this.writeACLs = writeACLs;
}
protected void dump(Node node, JSONWriter w, int currentRecursionLevel, int maxRecursionLevels) throws RepositoryException, JSONException {
w.object();
this.dumpObject(node, w, currentRecursionLevel, maxRecursionLevels);
w.endObject();
}
public void dumpObject(Node node, JSONWriter w, int currentRecursionLevel, int maxRecursionLevels) throws RepositoryException, JSONException {
PropertyIterator props = node.getProperties();
if (this.writeACLs) {
this.writeACL(node, w);
}
while (props.hasNext()) {
this.writeProperty(w, props.nextProperty());
}
if (this.recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
NodeIterator children = node.getNodes();
while (children.hasNext()) {
Node n = children.nextNode();
this.dumpSingleNode(n, w, currentRecursionLevel, maxRecursionLevels);
}
}
}
protected void writeACL(Node node, JSONWriter w) throws RepositoryException, JSONException {
Session session = node.getSession();
w.key("jcr:permissions").object();
w.key("create").value(session.hasPermission(node.getPath() + "/newnode", "add_node"));
w.key("modify").value(session.hasPermission(node.getPath() + "/newprop", "set_property"));
w.key("delete").value(session.hasPermission(node.getPath(), "remove"));
w.endObject();
}
}
}