Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.delivery.jcr.filter

File FilteringContentDecoratorBuilder.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
20% of files have more coverage

Code metrics

20
38
7
1
152
89
18
0.47
5.43
7
2.57

Classes

Class Line # Actions
FilteringContentDecoratorBuilder 55 38 0% 18 10
0.8461538684.6%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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  6 toggle public FilteringContentDecoratorBuilder childNodeTypes(List<String> childrenNodeTypes) {
62  6 this.childNodeTypes.clear();
63  6 if (childrenNodeTypes != null) {
64  4 this.childNodeTypes.addAll(childrenNodeTypes);
65    }
66  6 return this;
67    }
68   
 
69  7 toggle public FilteringContentDecoratorBuilder includeSystemProperties(boolean includeSystemProperties) {
70  7 this.includeSystemProperties = includeSystemProperties;
71  7 return this;
72    }
73   
 
74  7 toggle public FilteringContentDecoratorBuilder depth(int depth) {
75  7 this.depth = depth;
76  7 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  2 toggle @Deprecated
83    public FilteringContentDecoratorBuilder references(List<ReferenceDefinition> references) {
84  2 this.references.clear();
85  2 if (references != null) {
86  2 this.references.addAll(references);
87    }
88  2 return this;
89    }
90   
 
91  4 toggle public Node wrapNode(Node node) {
92  4 if (!childNodeTypes.isEmpty()) {
93  2 node = new ContentDecoratorNodeWrapper<>(node, new ChildFilteringContentDecorator(new NodeTypesPredicate(childNodeTypes), true));
94    }
95   
96  4 if (!includeSystemProperties) {
97  2 node = new ContentDecoratorNodeWrapper<>(node, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
98    }
99   
100  4 if (depth >= 0) {
101  2 ChildFilteringContentDecorator depthDecorator = new ChildFilteringContentDecorator(new DepthFilteringPredicate(getDepth(node), getDepth(node) + depth), true);
102  2 node = new ContentDecoratorNodeWrapper<>(node, depthDecorator);
103    }
104   
105    // TODO: will be removed, used by v1 for backward compatibility
106  4 if (!references.isEmpty()) {
107  0 FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
108  0 expandDecorators
109    .includeSystemProperties(includeSystemProperties)
110    .depth(-1);
111   
112  0 node = new ReferenceExpandWrapper(node, references, expandDecorators);
113    }
114   
115  4 return node;
116    }
117   
 
118  3 toggle public NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
119   
120  3 if (!childNodeTypes.isEmpty()) {
121  2 nodeIterator = new ChildNodeTypesFilteringNodeIterator(nodeIterator, childNodeTypes);
122    }
123   
124  3 if (!includeSystemProperties) {
125  1 nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new PropertyFilteringContentDecorator(new JCRMgnlPropertyHidingPredicate()));
126    }
127   
128  3 if (depth >= 0) {
129  2 nodeIterator = new DepthFilteringDecoratorNodeIterator(nodeIterator, depth);
130    }
131   
132    // TODO: will be removed, used by v1 for backward compatibility
133  3 if (!references.isEmpty()) {
134  0 FilteringContentDecoratorBuilder expandDecorators = new FilteringContentDecoratorBuilder();
135  0 expandDecorators
136    .includeSystemProperties(includeSystemProperties)
137    .depth(-1);
138   
139  0 nodeIterator = new ContentDecoratorNodeIterator(nodeIterator, new ReferenceExpandDecorator(references, expandDecorators));
140    }
141   
142  3 return nodeIterator;
143    }
144   
 
145  4 toggle private int getDepth(Node node) {
146  4 try {
147  4 return node.getDepth();
148    } catch (RepositoryException e) {
149  0 return 0;
150    }
151    }
152    }