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 package info.magnolia.resourceloader.jcr;
35
36 import static info.magnolia.cms.util.FilteredEventListener.JCR_SYSTEM_EXCLUDING_PREDICATE;
37 import static info.magnolia.resourceloader.ResourceOriginChange.Type.*;
38 import static info.magnolia.resourceloader.ResourceOriginChange.resourceChange;
39 import static java.util.Spliterators.spliteratorUnknownSize;
40 import static java.util.stream.Collectors.toList;
41
42 import info.magnolia.cms.util.FilteredEventListener;
43 import info.magnolia.context.SystemContext;
44 import info.magnolia.jcr.util.NodeTypes;
45 import info.magnolia.jcr.util.PropertyUtil;
46 import info.magnolia.observation.WorkspaceEventListenerRegistration;
47 import info.magnolia.resourceloader.AbstractResourceOrigin;
48 import info.magnolia.resourceloader.ResourceOriginChange;
49 import info.magnolia.resourceloader.ResourceOriginFactory;
50
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.io.Reader;
54 import java.io.StringReader;
55 import java.nio.charset.Charset;
56 import java.nio.charset.StandardCharsets;
57 import java.util.Calendar;
58 import java.util.LinkedHashMap;
59 import java.util.List;
60 import java.util.Map;
61 import java.util.Optional;
62 import java.util.Spliterator;
63 import java.util.regex.Matcher;
64 import java.util.regex.Pattern;
65 import java.util.stream.StreamSupport;
66
67 import javax.jcr.Binary;
68 import javax.jcr.Node;
69 import javax.jcr.RepositoryException;
70 import javax.jcr.Session;
71 import javax.jcr.observation.Event;
72 import javax.jcr.observation.EventIterator;
73 import javax.jcr.observation.EventListener;
74
75 import org.apache.commons.io.input.NullInputStream;
76 import org.apache.commons.lang3.StringUtils;
77 import org.apache.jackrabbit.JcrConstants;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80
81 import com.google.auto.factory.AutoFactory;
82 import com.google.auto.factory.Provided;
83
84 import lombok.SneakyThrows;
85
86
87
88
89 @AutoFactory(implementing = ResourceOriginFactory.class)
90 public class JcrResourceOrigin extends AbstractResourceOrigin<JcrResource> {
91 private final static Logger log = LoggerFactory.getLogger(JcrResourceOrigin.class);
92
93 public static final String RESOURCES_WORKSPACE = "resources";
94
95 public static final String BINARY_NODE_NAME = "binary";
96
97 public static final String TEXT_PROPERTY = "text";
98 public static final String BYPASS_PROPERTY = "bypass";
99
100 private final SystemContext systemContext;
101
102 JcrResourceOrigin(@Provided SystemContext systemContext, String name) {
103 super(name);
104 this.systemContext = systemContext;
105 }
106
107 @Override
108 protected void initializeResourceChangeMonitoring() {
109 try {
110 EventListener listener = new FilteredEventListener(new ResourcesObservationListener(), EVENT_LISTENER_FILTER);
111 WorkspaceEventListenerRegistration.observe(RESOURCES_WORKSPACE, "/", listener).withDelay(1000L, 1000L).register();
112 } catch (RepositoryException e) {
113 log.error("Failed to initialize JCR resource change monitoring: {}", e.getMessage(), e);
114 }
115 }
116
117 @Override
118 @SneakyThrows(RepositoryException.class)
119 public JcrResource getRoot() {
120 final Node rootNode = getJcrSession().getRootNode();
121 return newResource(rootNode);
122 }
123
124 @Override
125 @SneakyThrows(RepositoryException.class)
126 public JcrResource getByPath(String path) {
127 final Node node = getNode(path);
128 if (node == null || isBypassed(node)) {
129 throw new ResourceNotFoundException(this, path);
130 }
131 return newResource(node);
132 }
133
134 @Override
135 @SneakyThrows(RepositoryException.class)
136 public boolean hasPath(String path) {
137 final Node node = getNode(path);
138 return node != null && !isBypassed(node);
139 }
140
141 @Override
142 @SneakyThrows(RepositoryException.class)
143 protected boolean isFile(JcrResource resource) {
144
145 return !isDirectory(resource.getNode());
146 }
147
148 @Override
149 @SneakyThrows(RepositoryException.class)
150 protected boolean isDirectory(JcrResource resource) {
151
152 return isDirectory(resource.getNode());
153 }
154
155 @Override
156 protected boolean isEditable(JcrResource resource) {
157
158 return true;
159 }
160
161 @Override
162 @SneakyThrows(RepositoryException.class)
163 protected String getPath(JcrResource resource) {
164 return resource.getNode().getPath();
165 }
166
167 @Override
168 @SneakyThrows(RepositoryException.class)
169 protected String getName(JcrResource resource) {
170 return resource.getNode().getName();
171 }
172
173 @Override
174 @SneakyThrows(RepositoryException.class)
175 protected long getLastModified(JcrResource resource) {
176
177 final Calendar lastModified = NodeTypes.LastModified.getLastModified(resource.getNode());
178 if (lastModified == null) {
179
180 throw new RepositoryException("No lastModified or created date property on " + resource.getNode());
181 }
182 return lastModified.getTimeInMillis();
183 }
184
185 @Override
186 @SneakyThrows(RepositoryException.class)
187 protected List<JcrResource> listChildren(JcrResource resource) {
188 if (!resource.isDirectory()) {
189 throw new IllegalStateException(resource.getPath() + " is not a directory.");
190 }
191
192 Spliterator<Node> nodeIterator = spliteratorUnknownSize(resource.getNode().getNodes(), Spliterator.ORDERED);
193 return StreamSupport.stream(nodeIterator, false)
194 .filter(this::nodeFilter)
195 .map(this::newResource)
196 .collect(toList());
197 }
198
199 @Override
200 @SneakyThrows(RepositoryException.class)
201 protected JcrResource getParent(JcrResource resource) {
202 Node node = resource.getNode();
203 String childPath = node.getPath();
204
205 if (childPath.equals("/")) {
206 return null;
207 }
208
209 return newResource(node.getParent());
210 }
211
212 @Override
213 @SneakyThrows(RepositoryException.class)
214 protected Reader openReader(JcrResource resource) throws IOException {
215 Node node = resource.getNode();
216 if (isTextResource(node)) {
217 return new StringReader(node.getProperty(TEXT_PROPERTY).getString());
218 }
219 return super.openReader(resource);
220 }
221
222 @Override
223 @SneakyThrows(RepositoryException.class)
224 protected InputStream doOpenStream(JcrResource resource) {
225 Binary binary = getBinary(resource.getNode());
226 if (binary != null) {
227 return binary.getStream();
228 }
229
230 log.debug("JCR resource {} has no content", resource);
231 return new NullInputStream(0);
232 }
233
234 @Override
235 @SneakyThrows(RepositoryException.class)
236 protected Charset getCharsetFor(JcrResource resource) {
237 Node node = resource.getNode();
238 if (isBinaryResource(node)) {
239 Node binary = node.getNode(BINARY_NODE_NAME);
240 if (binary.hasProperty(JcrConstants.JCR_ENCODING)) {
241 return Charset.forName(node.getProperty(JcrConstants.JCR_ENCODING).getString());
242 }
243 }
244
245 return StandardCharsets.UTF_8;
246 }
247
248 protected JcrResource newResource(Node node) {
249 return new JcrResource(this, node);
250 }
251
252
253
254
255 private Node getNode(String resource) throws RepositoryException {
256 final Session jcrSession = getJcrSession();
257 final boolean exists = jcrSession.nodeExists(resource);
258
259 if (!exists) {
260 return null;
261 }
262
263 return jcrSession.getNode(resource);
264 }
265
266 protected Session getJcrSession() throws RepositoryException {
267 return systemContext.getJCRSession(RESOURCES_WORKSPACE);
268 }
269
270 protected boolean isResource(Node node) throws RepositoryException {
271 return isBinaryResource(node) || isTextResource(node);
272 }
273
274 protected Binary getBinary(Node resourceNode) throws RepositoryException {
275 if (isTextResource(resourceNode)) {
276 return resourceNode.getProperty(TEXT_PROPERTY).getBinary();
277 } else if (isBinaryResource(resourceNode)) {
278 final Node binary = resourceNode.getNode(BINARY_NODE_NAME);
279 return binary.getProperty(JcrConstants.JCR_DATA).getBinary();
280 }
281 return null;
282 }
283
284 protected boolean isTextResource(Node resourceNode) throws RepositoryException {
285 return resourceNode.hasProperty(TEXT_PROPERTY);
286 }
287
288 protected boolean isBinaryResource(Node node) throws RepositoryException {
289 return node.hasNode(BINARY_NODE_NAME);
290 }
291
292 protected boolean isFile(Node node) throws RepositoryException {
293
294 return node.isNodeType(NodeTypes.Content.NAME);
295 }
296
297 protected boolean isDirectory(Node node) throws RepositoryException {
298 return node.isNodeType(NodeTypes.Folder.NAME) || node.getDepth() == 0;
299 }
300
301
302 protected boolean isBypassed(Node node) {
303 return PropertyUtil.getBoolean(node, BYPASS_PROPERTY, false);
304 }
305
306 @SneakyThrows(RepositoryException.class)
307 private boolean nodeFilter(Node node) {
308 return node != null && !isBypassed(node) && (isDirectory(node) || isFile(node));
309 }
310
311
312
313
314
315 private class ResourcesObservationListener implements EventListener {
316
317 @Override
318 public void onEvent(EventIterator events) {
319
320 final Map<String, ResourceOriginChange> changes = new LinkedHashMap<>();
321
322 final ResourceOriginChange.Builder builder = resourceChange().inOrigin(JcrResourceOrigin.this);
323 while (events.hasNext()) {
324 final Event event = events.nextEvent();
325 int eventType = event.getType();
326
327 try {
328
329 String path = event.getPath();
330 builder.at(path);
331 if (eventType == Event.NODE_ADDED) {
332 builder.ofType(ResourceOriginChange.Type.ADDED);
333 changes.put(path, builder.build());
334 } else if (eventType == Event.NODE_REMOVED) {
335 builder.ofType(REMOVED);
336 changes.put(path, builder.build());
337 }
338
339
340 final Matcher itemPathMatcher = Pattern.compile("^(?<relatedNodePath>.+)?/(?<name>.+)?$").matcher(event.getPath());
341
342 if (itemPathMatcher.matches()) {
343 final String relatedNode = StringUtils.defaultIfBlank(itemPathMatcher.group("relatedNodePath"), "/");
344 final String name = StringUtils.defaultIfBlank(itemPathMatcher.group("name"), "");
345
346
347
348
349
350
351
352
353
354 if (eventType == Event.PROPERTY_ADDED || eventType == Event.PROPERTY_CHANGED || eventType == Event.PROPERTY_REMOVED) {
355 Optional<ResourceOriginChange> bypassStateChange = tryDetectBypassPropertyChange(relatedNode, name, eventType);
356 if (bypassStateChange.isPresent()) {
357 changes.put(bypassStateChange.get().getRelatedResourcePath(), bypassStateChange.get());
358 continue;
359 }
360 }
361
362
363 changes.put(relatedNode, builder.at(relatedNode).ofType(MODIFIED).build());
364 }
365 } catch (RepositoryException e) {
366 log.error("Failed to process event [{}] due to: {}", event, e.getMessage(), e);
367 }
368 }
369
370 for (final ResourceOriginChange change : changes.values()) {
371 if (change.getType() == MODIFIED || change.getType() == ADDED) {
372 try {
373 final Node relatedNode = getNode(change.getRelatedResourcePath());
374 if (isBypassed(relatedNode)) {
375 continue;
376 }
377 } catch (RepositoryException e) {
378 log.warn("Failed to retrieve supposedly present node [{}] during resource change dispatching: {}", change.getRelatedResourcePath(), e.getMessage());
379 continue;
380 }
381 }
382
383 dispatchResourceChange(change);
384 }
385 }
386
387 private Optional<ResourceOriginChange> tryDetectBypassPropertyChange(String relatedNodePath, String name, int eventType) throws RepositoryException {
388 if (BYPASS_PROPERTY.equals(name)) {
389 final Node resourceNode = getNode(relatedNodePath);
390 boolean isBypassed = PropertyUtil.getBoolean(resourceNode, BYPASS_PROPERTY, false);
391
392 if (isBypassed) {
393 return Optional.of(resourceChange().at(relatedNodePath).inOrigin(JcrResourceOrigin.this).ofType(REMOVED).build());
394 } else if (eventType != Event.PROPERTY_ADDED) {
395 return Optional.of(resourceChange().at(relatedNodePath).inOrigin(JcrResourceOrigin.this).ofType(ADDED).build());
396 }
397 }
398 return Optional.empty();
399 }
400 }
401
402
403
404
405
406 private final static org.apache.jackrabbit.commons.predicate.Predicate EVENT_LISTENER_FILTER = new org.apache.jackrabbit.commons.predicate.Predicate() {
407 @Override
408 public boolean evaluate(Object eventObject) {
409 if (!JCR_SYSTEM_EXCLUDING_PREDICATE.evaluate(eventObject)) {
410 return false;
411 }
412
413 final Event event = (Event) eventObject;
414 try {
415 final String relatedPath = event.getPath();
416 return !relatedPath.endsWith("/" + NodeTypes.LastModified.LAST_MODIFIED) && !relatedPath.endsWith("/" + NodeTypes.LastModified.LAST_MODIFIED_BY);
417 } catch (RepositoryException e) {
418 return false;
419 }
420 }
421 };
422 }