RequestMapper.java
5.77 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:
* javax.jcr.Item
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Workspace
* javax.jcr.observation.Event
* javax.jcr.observation.EventIterator
* javax.jcr.observation.EventListener
* javax.jcr.observation.ObservationManager
* javax.jcr.query.Query
* javax.jcr.query.QueryManager
* javax.jcr.query.QueryResult
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.explorer.impl.j2ee.mapping;
import com.day.crx.explorer.impl.j2ee.mapping.AbstractMapping;
import com.day.crx.explorer.impl.j2ee.mapping.Mapping;
import com.day.crx.explorer.impl.j2ee.mapping.NodeTypeMapping;
import com.day.crx.explorer.impl.j2ee.mapping.PathMapping;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RequestMapper
implements EventListener {
public static final String CRX_REQUEST_MAPPING = "crx:RequestMapping";
private static final Logger log = LoggerFactory.getLogger(RequestMapper.class);
private Map mappings = new HashMap();
private Session session;
public RequestMapper(Session session) {
this.session = session;
this.initMappings();
try {
ObservationManager obs = session.getWorkspace().getObservationManager();
obs.addEventListener((EventListener)this, 31, "/", true, null, new String[]{"crx:RequestMapping"}, false);
obs.addEventListener((EventListener)new RemoveListener(), 2, "/", true, null, null, false);
}
catch (RepositoryException e) {
log.error("Unable to get ObservationManager: {}", (Object)e.toString());
}
}
private void initMappings() {
try {
QueryManager qmgr = this.session.getWorkspace().getQueryManager();
Query query = qmgr.createQuery("select * from crx:RequestMapping", "sql");
QueryResult result = query.execute();
NodeIterator iter = result.getNodes();
while (iter.hasNext()) {
this.addMapping(iter.nextNode().getPath());
}
}
catch (RepositoryException e) {
log.error("Error during initialization of request mappings: {}", (Object)e.getMessage());
}
}
public Mapping getMapping(Item i) throws RepositoryException {
for (Mapping map : this.mappings.values()) {
if (!map.contains(i)) continue;
return map;
}
return null;
}
private void addMapping(String path) throws RepositoryException {
Node n = (Node)this.session.getItem(path);
AbstractMapping map = null;
if (n.isNodeType("crx:NodeTypeRequestMapping")) {
map = new NodeTypeMapping(n);
} else if (n.isNodeType("crx:PathRequestMapping")) {
map = new PathMapping(n);
} else {
log.info("Invalid mapping at " + path);
}
this.mappings.put(path, map);
log.info("Adding mapping: {}", (Object)map);
}
private void removeMapping(String path) {
Mapping map = (Mapping)this.mappings.remove(path);
if (map != null) {
log.info("Removed mapping: {}", (Object)map);
}
}
public void onEvent(EventIterator eventIterator) {
HashSet<String> modifiedNodes = new HashSet<String>();
HashSet<String> removedNodes = new HashSet<String>();
while (eventIterator.hasNext()) {
try {
Event evt = eventIterator.nextEvent();
String path2 = evt.getPath();
switch (evt.getType()) {
case 1: {
break;
}
case 2: {
removedNodes.add(path2);
break;
}
case 4:
case 8:
case 16: {
String parent = Text.getRelativeParent((String)path2, (int)1);
modifiedNodes.add(parent);
}
}
}
catch (RepositoryException e) {}
}
for (String path2 : removedNodes) {
this.removeMapping(path2);
}
for (String path2 : modifiedNodes) {
try {
this.removeMapping(path2);
this.addMapping(path2);
}
catch (RepositoryException e) {
log.error("Error during modification: {}", (Object)e.getMessage());
}
}
}
private class RemoveListener
implements EventListener {
private RemoveListener() {
}
public void onEvent(EventIterator eventIterator) {
while (eventIterator.hasNext()) {
try {
Event evt = eventIterator.nextEvent();
String path = evt.getPath();
switch (evt.getType()) {
case 2: {
RequestMapper.this.removeMapping(path);
}
}
}
catch (RepositoryException e) {}
}
}
}
}