View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.rest.delivery.jcr.filter;
35  
36  import info.magnolia.jcr.decoration.ContentDecoratorNodeIterator;
37  import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
38  import info.magnolia.jcr.predicate.JCRMgnlPropertyHidingPredicate;
39  import info.magnolia.jcr.wrapper.ChildFilteringContentDecorator;
40  import info.magnolia.jcr.wrapper.PropertyFilteringContentDecorator;
41  import info.magnolia.rest.delivery.jcr.decorator.ReferenceExpandDecorator;
42  import info.magnolia.rest.delivery.jcr.decorator.ReferenceExpandWrapper;
43  import info.magnolia.rest.reference.ReferenceDefinition;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.RepositoryException;
51  
52  /**
53   * A builder to centralize the decorators in one place.
54   */
55  public class FilteringContentDecoratorBuilder {
56      private List<String> childNodeTypes = new ArrayList<>();
57      private boolean includeSystemProperties;
58      private int depth;
59      private List<ReferenceDefinition> references = new ArrayList<>();
60  
61      public FilteringContentDecoratorBuilder childNodeTypes(List<String> childrenNodeTypes) {
62          this.childNodeTypes.clear();
63          if (childrenNodeTypes != null) {
64              this.childNodeTypes.addAll(childrenNodeTypes);
65          }
66          return this;
67      }
68  
69      public FilteringContentDecoratorBuilder includeSystemProperties(boolean includeSystemProperties) {
70          this.includeSystemProperties = includeSystemProperties;
71          return this;
72      }
73  
74      public FilteringContentDecoratorBuilder depth(int depth) {
75          this.depth = depth;
76          return this;
77      }
78  
79      /**
80       * @deprecated since 2.1, reference resolver is now moved to {@link info.magnolia.rest.delivery.jcr.NodeWriter}.
81       */
82      @Deprecated
83      public FilteringContentDecoratorBuilder references(List<ReferenceDefinition> references) {
84          this.references.clear();
85          if (references != null) {
86              this.references.addAll(references);
87          }
88          return this;
89      }
90  
91      public Node wrapNode(Node node) {
92          if (!childNodeTypes.isEmpty()) {
93              node = new ContentDecoratorNodeWrapper<>(node, new ChildFilteringContentDecorator(new NodeTypesPredicate(childNodeTypes), true));
94          }
95  
96          if (!includeSystemProperties) {
97              node = new ContentDecoratorNodeWrapper<>(node, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
98          }
99  
100         if (depth >= 0) {
101             ChildFilteringContentDecorator depthDecorator = new ChildFilteringContentDecorator(new DepthFilteringPredicate(getDepth(node), getDepth(node) + depth), true);
102             node = new ContentDecoratorNodeWrapper<>(node, depthDecorator);
103         }
104 
105         // TODO: will be removed, used by v1 for backward compatibility
106         if (!references.isEmpty()) {
107             FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
108             expandDecorators
109                     .includeSystemProperties(includeSystemProperties)
110                     .depth(-1);
111 
112             node = new ReferenceExpandWrapper(node, references, expandDecorators);
113         }
114 
115         return node;
116     }
117 
118     public NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
119 
120         if (!childNodeTypes.isEmpty()) {
121             nodeIterator = new ChildNodeTypesFilteringNodeIterator(nodeIterator, childNodeTypes);
122         }
123 
124         if (!includeSystemProperties) {
125             nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
126         }
127 
128         if (depth >= 0) {
129             nodeIterator = new DepthFilteringDecoratorNodeIterator(nodeIterator, depth);
130         }
131 
132         // TODO: will be removed, used by v1 for backward compatibility
133         if (!references.isEmpty()) {
134             FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
135             expandDecorators
136                     .includeSystemProperties(includeSystemProperties)
137                     .depth(-1);
138 
139             nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new ReferenceExpandDecorator(references, expandDecorators));
140         }
141 
142         return nodeIterator;
143     }
144 
145     private int getDepth(Node node) {
146         try {
147             return node.getDepth();
148         } catch (RepositoryException e) {
149             return 0;
150         }
151     }
152 }