Clover icon

Magnolia REST Content Delivery 2.0.1

  1. Project Clover database Thu Dec 21 2017 11:02:58 CET
  2. Package info.magnolia.rest.delivery.jcr.decorator

File ReferenceExpandWrapper.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
73% of files have more coverage

Code metrics

10
47
11
1
206
140
21
0.45
4.27
11
1.91
5.6% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ReferenceExpandWrapper 70 47 5.6% 21 68
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2017 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.decorator;
35   
36    import info.magnolia.jcr.decoration.ContentDecoratorNodeWrapper;
37    import info.magnolia.jcr.util.NodeUtil;
38    import info.magnolia.objectfactory.ComponentProvider;
39    import info.magnolia.objectfactory.Components;
40    import info.magnolia.rest.delivery.jcr.filter.FilteringContentDecoratorBuilder;
41    import info.magnolia.rest.reference.ReferenceDefinition;
42    import info.magnolia.rest.reference.ReferenceResolver;
43   
44    import java.util.ArrayList;
45    import java.util.Collection;
46    import java.util.HashMap;
47    import java.util.Iterator;
48    import java.util.List;
49    import java.util.Map;
50    import java.util.Optional;
51    import java.util.stream.Collectors;
52   
53    import javax.jcr.Item;
54    import javax.jcr.Node;
55    import javax.jcr.NodeIterator;
56    import javax.jcr.Property;
57    import javax.jcr.PropertyIterator;
58    import javax.jcr.RepositoryException;
59   
60    import org.apache.jackrabbit.commons.ItemNameMatcher;
61    import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
62    import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
63    import org.apache.jackrabbit.spi.commons.iterator.Iterators;
64   
65    import com.google.common.collect.Lists;
66   
67    /**
68    * A Node wrapper implementation which knows about expanding reference properties.
69    */
 
70    public class ReferenceExpandWrapper extends ContentDecoratorNodeWrapper {
71   
72    private final Map<String, Item> resolvedItems = new HashMap<>();
73    private final ComponentProvider componentProvider = Components.getComponentProvider();
74   
 
75  0 toggle public ReferenceExpandWrapper(Node wrapped, List<ReferenceDefinition> references, FilteringContentDecoratorBuilder expandDecorators) {
76  0 super(wrapped, new ReferenceExpandDecorator(references, expandDecorators));
77  0 try {
78  0 PropertyIterator properties = wrapped.getProperties();
79  0 while (properties.hasNext()) {
80  0 Property property = properties.nextProperty();
81   
82  0 Optional<Node> resolvedItem = resolveProperty(property, references);
83   
84  0 if (resolvedItem.isPresent()) {
85  0 resolvedItems.put(property.getName(), resolvedItem.get());
86    }
87    }
88    } catch (RepositoryException e) {
89  0 throw new RuntimeException("Can't wrap node [" + wrapped + "]", e);
90    }
91    }
92   
 
93  0 toggle private Optional<Node> resolveProperty(Property property, List<ReferenceDefinition> references) throws RepositoryException {
94  0 for (ReferenceDefinition definition : references) {
95  0 boolean matchNodeType = definition.getNodeType() == null || definition.getNodeType() != null && NodeUtil.isNodeType(wrapped, definition.getNodeType());
96  0 boolean matchItemName = definition.getPropertyName() == null || property.getName().matches(definition.getPropertyName());
97  0 boolean hasDefinition = definition.getReferenceResolver() != null && definition.getReferenceResolver().getImplementationClass() != null;
98  0 if (matchNodeType && matchItemName && hasDefinition) {
99  0 ReferenceResolver referenceResolver = componentProvider.newInstance(definition.getReferenceResolver().getImplementationClass(), definition.getReferenceResolver());
100  0 Optional itemOptional = referenceResolver.resolve(property);
101  0 if (itemOptional.isPresent() && itemOptional.get() instanceof Node) {
102  0 Node resolvedNode = (Node) itemOptional.get();
103  0 return Optional.of(new AdditionNodeWrapper(resolvedNode, property.getName()));
104    }
105    }
106    }
107   
108  0 return Optional.empty();
109    }
110   
 
111  0 toggle @Override
112    public boolean hasProperties() throws RepositoryException {
113  0 boolean hasProperties = resolvedItems.values().stream()
114    .anyMatch(item -> !item.isNode());
115  0 return hasProperties || super.hasProperties();
116    }
117   
 
118  0 toggle @Override
119    public boolean hasProperty(String relPath) throws RepositoryException {
120  0 boolean hasProperties = resolvedItems.entrySet().stream()
121    .anyMatch(entry -> !entry.getValue().isNode() && entry.getKey().equals(relPath));
122  0 return hasProperties || super.hasProperty(relPath);
123    }
124   
 
125    toggle @Override
126    public PropertyIterator getProperties() throws RepositoryException {
127    return getProperties("*");
128    }
129   
 
130  0 toggle @Override
131    public PropertyIterator getProperties(String namePattern) throws RepositoryException {
132  0 return getProperties(new String[] {namePattern});
133    }
134   
 
135  0 toggle @Override
136    public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
137  0 Iterator properties = Iterators.filterIterator(Iterators.properties(super.getProperties(nameGlobs)), property -> {
138  0 try {
139  0 return !resolvedItems.containsKey(property.getName());
140    } catch (RepositoryException e) {
141  0 return false;
142    }
143    });
144   
145  0 List<Property> merged = Lists.newArrayList(properties);
146  0 List<Property> resolvedProperties = resolvedItems.entrySet().stream()
147    .filter(entry -> !entry.getValue().isNode() && ItemNameMatcher.matches(entry.getKey(), nameGlobs))
148    .map(entry -> (Property) entry.getValue())
149    .collect(Collectors.toList());
150  0 merged.addAll(resolvedProperties);
151   
152  0 return new PropertyIteratorAdapter(merged);
153    }
154   
 
155  0 toggle @Override
156    public boolean hasNodes() throws RepositoryException {
157  0 boolean hasNodes = resolvedItems.values().stream()
158    .anyMatch(Item::isNode);
159  0 return hasNodes || super.hasNodes();
160    }
161   
 
162  0 toggle @Override
163    public boolean hasNode(String relPath) throws RepositoryException {
164  0 boolean hasNodes = resolvedItems.entrySet().stream()
165    .anyMatch(entry ->entry.getValue().isNode() && entry.getKey().equals(relPath) );
166  0 return hasNodes || super.hasNode(relPath);
167    }
168   
 
169  0 toggle @Override
170    public Node getNode(String relPath) throws RepositoryException {
171  0 Optional<Item> itemOptional = resolvedItems.entrySet().stream()
172    .filter(entry -> entry.getValue().isNode() && entry.getKey().equals(relPath))
173    .findFirst()
174    .map(Map.Entry::getValue);
175   
176  0 if (itemOptional.isPresent()) {
177  0 return (Node) itemOptional.get();
178    }
179   
180  0 return super.getNode(relPath);
181    }
182   
 
183    toggle @Override
184    public NodeIterator getNodes() throws RepositoryException {
185    return getNodes("*");
186    }
187   
 
188  0 toggle @Override
189    public NodeIterator getNodes(String namePattern) throws RepositoryException {
190  0 return getNodes(new String[] {namePattern});
191    }
192   
 
193  0 toggle @Override
194    public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
195  0 Collection<Node> children = NodeUtil.getCollectionFromNodeIterator(getWrappedNode().getNodes(nameGlobs));
196   
197  0 List<Node> merged = new ArrayList<>(children);
198  0 List<Node> resolvedNodes = resolvedItems.entrySet().stream()
199    .filter(entry -> ItemNameMatcher.matches(entry.getKey(), nameGlobs) && entry.getValue().isNode())
200    .map(entry -> (Node) entry.getValue())
201    .collect(Collectors.toList());
202  0 merged.addAll(resolvedNodes);
203   
204  0 return wrapNodeIterator(new NodeIteratorAdapter(merged));
205    }
206    }