View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * A {@link info.magnolia.resourceloader.ResourceOrigin} which loads resources from JCR.
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         // Make sure resource is always either file or directory
145         return !isDirectory(resource.getNode());
146     }
147 
148     @Override
149     @SneakyThrows(RepositoryException.class)
150     protected boolean isDirectory(JcrResource resource) {
151         // TODO seems incomplete/unsafe ? what about other node types ?
152         return isDirectory(resource.getNode());
153     }
154 
155     @Override
156     protected boolean isEditable(JcrResource resource) {
157         // For now we naively consider that any resource in the JcrResourceOrigin will be editable
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         // TODO Compare: return resource.getNode().getProperty(JcrConstants.JCR_LASTMODIFIED)
177         final Calendar lastModified = NodeTypes.LastModified.getLastModified(resource.getNode());
178         if (lastModified == null) {
179             // TODO: shouldn't this happen in info.magnolia.jcr.util.NodeTypes.LastModified.getLastModified() ?
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         // Node exists, but has no content yet (i.e empty file)
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         // assume UTF-8
245         return StandardCharsets.UTF_8;
246     }
247 
248     protected JcrResource newResource(Node node) {
249         return new JcrResource(this, node);
250     }
251 
252     /**
253      * Only returns the node if it exists.
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         // Resource are mgnl:content, which is mutually exclusive with mgnl:folder (a resource should never be both file and directory)
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     // TODO avoid !bypass double negatives --> isEnabled.
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      * The ResourcesObservationListener collects paths for received change events, then invokes the given visitor
313      * with proper corresponding {@link info.magnolia.resourceloader.Resource Resource} objects.
314      */
315     private class ResourcesObservationListener implements EventListener {
316 
317         @Override
318         public void onEvent(EventIterator events) {
319             // Collect changed paths from EventIterator
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                          * Handle bypass property change first. If the state of {@value #BYPASS_PROPERTY} has changed, then the following changes might be communicated:
348                          * <ul>
349                          * <li>{@value #BYPASS_PROPERTY} turned to true => resource is not a part of this origin any longer => it is deleted</li>
350                          * <li>{@value #BYPASS_PROPERTY} turned to false => unless the property was added with a false value (no effect then) the resource can be treated as a re-appeared,
351                          * new resource, hence a corresponding change emission/</li>
352                          * </ul>
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                         // If a node the changes is not about an added or removed node - signal the event for the parent
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      * Besides what a standard {@link FilteredEventListener#JCR_SYSTEM_EXCLUDING_PREDICATE} does, filters out {@value NodeTypes.LastModified#LAST_MODIFIED} and {@value NodeTypes.LastModified#LAST_MODIFIED_BY} -
404      * those only add noise to the event listening, if they're modified - means that something else is modified and the actual change (e.g. {@value BYPASS_PROPERTY}) can be mangled by either of them.
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 }