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.cms.i18n.I18nContentSupport;
37  import info.magnolia.jcr.decoration.ContentDecoratorNodeIterator;
38  import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
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.delivery.jcr.i18n.I18nDeliveryContentDecorator;
44  import info.magnolia.rest.delivery.jcr.i18n.I18nDeliveryNodeWrapper;
45  import info.magnolia.rest.reference.ReferenceDefinition;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  import java.util.Optional;
50  
51  import javax.jcr.Node;
52  import javax.jcr.NodeIterator;
53  import javax.jcr.RepositoryException;
54  
55  /**
56   * A builder to centralize the decorators in one place.
57   */
58  public class FilteringContentDecoratorBuilder {
59      private List<String> childNodeTypes = new ArrayList<>();
60      private List<String> systemProperties = new ArrayList<>();
61      boolean strict;
62      private boolean includeSystemProperties;
63      private int depth;
64      private I18nContentSupport i18nContentSupport;
65      private List<ReferenceDefinition> references = new ArrayList<>();
66  
67      public FilteringContentDecoratorBuilder childNodeTypes(List<String> childrenNodeTypes) {
68          this.childNodeTypes.clear();
69          Optional.ofNullable(childrenNodeTypes).ifPresent(this.childNodeTypes::addAll);
70          return this;
71      }
72  
73      public FilteringContentDecoratorBuilder systemProperties(List<String> properties) {
74          this.systemProperties.clear();
75          Optional.ofNullable(properties).ifPresent(this.systemProperties::addAll);
76          return this;
77      }
78  
79      public FilteringContentDecoratorBuilder strict(boolean strict) {
80          this.strict = strict;
81          return this;
82      }
83  
84      public FilteringContentDecoratorBuilder includeSystemProperties(boolean includeSystemProperties) {
85          this.includeSystemProperties = includeSystemProperties;
86          return this;
87      }
88  
89      public FilteringContentDecoratorBuilder depth(int depth) {
90          this.depth = depth;
91          return this;
92      }
93  
94      public FilteringContentDecoratorBuilder supportI18n(I18nContentSupport i18nContentSupport) {
95          this.i18nContentSupport = i18nContentSupport;
96          return this;
97      }
98  
99      /**
100      * @deprecated since 2.1, reference resolver is now moved to {@link info.magnolia.rest.delivery.jcr.NodeWriter}.
101      */
102     @Deprecated
103     public FilteringContentDecoratorBuilder references(List<ReferenceDefinition> references) {
104         this.references.clear();
105         if (references != null) {
106             this.references.addAll(references);
107         }
108         return this;
109     }
110 
111     public Node wrapNode(Node node) {
112         if (!childNodeTypes.isEmpty()) {
113             node = new ContentDecoratorNodeWrapper<>(node, new ChildFilteringContentDecorator(new NodeTypesPredicate(childNodeTypes, !strict), true));
114         }
115 
116         if (!includeSystemProperties || !systemProperties.isEmpty()) {
117             node = new ContentDecoratorNodeWrapper<>(node, new PropertyFilteringContentDecorator(new SystemPropertyHidingPredicate(systemProperties)));
118         }
119 
120         if (depth >= 0) {
121             ChildFilteringContentDecorator depthDecorator = new ChildFilteringContentDecorator(new DepthFilteringPredicate(getDepth(node), getDepth(node) + depth), true);
122             node = new ContentDecoratorNodeWrapper<>(node, depthDecorator);
123         }
124 
125         if (i18nContentSupport != null) {
126             node = new I18nDeliveryNodeWrapper(node, i18nContentSupport);
127         }
128 
129         // TODO: will be removed, used by v1 for backward compatibility
130         if (!references.isEmpty()) {
131             FilteringContentDecoratorBuilderoratorBuilder.html#FilteringContentDecoratorBuilder">FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
132             expandDecorators
133                     .includeSystemProperties(includeSystemProperties)
134                     .depth(-1);
135 
136             node = new ReferenceExpandWrapper(node, references, expandDecorators);
137         }
138 
139         return node;
140     }
141 
142     public NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
143 
144         if (!childNodeTypes.isEmpty()) {
145             nodeIterator = new ChildNodeTypesFilteringNodeIterator(nodeIterator, childNodeTypes, !strict);
146         }
147 
148         if (!includeSystemProperties || !systemProperties.isEmpty()) {
149             nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new PropertyFilteringContentDecorator(new SystemPropertyHidingPredicate(systemProperties)));
150         }
151 
152         if (depth >= 0) {
153             nodeIterator = new DepthFilteringDecoratorNodeIterator(nodeIterator, depth);
154         }
155 
156         if (i18nContentSupport != null) {
157             nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new I18nDeliveryContentDecorator(i18nContentSupport));
158         }
159 
160         // TODO: will be removed, used by v1 for backward compatibility
161         if (!references.isEmpty()) {
162             FilteringContentDecoratorBuilderoratorBuilder.html#FilteringContentDecoratorBuilder">FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
163             expandDecorators
164                     .includeSystemProperties(includeSystemProperties)
165                     .depth(-1);
166 
167             nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new ReferenceExpandDecorator(references, expandDecorators));
168         }
169 
170         return nodeIterator;
171     }
172 
173     private int getDepth(Node node) {
174         try {
175             return node.getDepth();
176         } catch (RepositoryException e) {
177             return 0;
178         }
179     }
180 }