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.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
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
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
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
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 }