View Javadoc
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.util.NodeUtil;
37  
38  import java.math.BigDecimal;
39  import java.util.List;
40  import java.util.stream.Collectors;
41  
42  import javax.jcr.Binary;
43  import javax.jcr.Item;
44  import javax.jcr.ItemVisitor;
45  import javax.jcr.Node;
46  import javax.jcr.NodeIterator;
47  import javax.jcr.PathNotFoundException;
48  import javax.jcr.Property;
49  import javax.jcr.PropertyIterator;
50  import javax.jcr.RepositoryException;
51  import javax.jcr.Session;
52  import javax.jcr.Value;
53  import javax.jcr.lock.Lock;
54  import javax.jcr.nodetype.NodeDefinition;
55  import javax.jcr.nodetype.NodeType;
56  import javax.jcr.version.Version;
57  
58  import org.apache.jackrabbit.commons.AbstractNode;
59  import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
60  import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
61  import org.apache.jackrabbit.util.ChildrenCollectorFilter;
62  
63  /**
64   * Read only {@link Node} implementation.
65   */
66  public class ReadOnlyNode extends AbstractNode {
67      private final String name;
68      private final Node parent;
69      private final List<Property> properties;
70      private final List<Node> children;
71  
72      public ReadOnlyNode(String name, Node parent, List<Node> children, List<Property> properties) {
73          this.name = name;
74          this.parent = parent;
75          this.children = children;
76          this.properties = properties;
77      }
78  
79      public ReadOnlyNode(String name, Node parent, List<Item> items) {
80          this.name = name;
81          this.parent = parent;
82          this.children = items.stream()
83                  .filter(item -> item.isNode())
84                  .map(item -> (Node) item)
85                  .collect(Collectors.toList());
86          this.properties = items.stream()
87                  .filter(item -> !item.isNode())
88                  .map(item -> (Property) item)
89                  .collect(Collectors.toList());
90      }
91  
92      @Override
93      public String getIdentifier() throws RepositoryException {
94          //TODO will be fixed with ref improvement feature.
95          return "";
96      }
97  
98      @Override
99      public String getPath() throws RepositoryException {
100         StringBuffer buffer = new StringBuffer(getParent().getPath());
101         if (buffer.length() > 1) {
102             buffer.append('/');
103         }
104         buffer.append("@" + this.name);
105 
106         return buffer.toString();
107     }
108 
109     @Override
110     public String getName() throws RepositoryException {
111         return this.name;
112     }
113 
114     @Override
115     public int getIndex() throws RepositoryException {
116         return 0;
117     }
118 
119     @Override
120     public boolean isNew() {
121         return false;
122     }
123 
124     @Override
125     public boolean isModified() {
126         return false;
127     }
128 
129     @Override
130     public boolean isSame(Item item) throws RepositoryException {
131         return false;
132     }
133 
134     @Override
135     public void accept(ItemVisitor visitor) throws RepositoryException {
136         visitor.visit(this);
137     }
138 
139     @Override
140     public Node getParent() throws RepositoryException {
141         return parent;
142     }
143 
144     @Override
145     public Session getSession() throws RepositoryException {
146         return parent.getSession();
147     }
148 
149     @Override
150     public NodeType getPrimaryNodeType() throws RepositoryException {
151         return parent.getPrimaryNodeType();
152     }
153 
154     @Override
155     public Node getNode(String relPath) throws RepositoryException {
156         return children.stream()
157                 .filter(node -> relPath.equals(NodeUtil.getPathIfPossible(node)))
158                 .findFirst()
159                 .orElseThrow(() -> new PathNotFoundException(String.format("%s not found!", relPath)));
160     }
161 
162     @Override
163     public NodeIterator getNodes() throws RepositoryException {
164         return new NodeIteratorAdapter(this.children.iterator());
165     }
166 
167     @Override
168     public NodeIterator getNodes(String namePattern) throws RepositoryException {
169         return ChildrenCollectorFilter.collectChildNodes(this, namePattern);
170     }
171 
172     @Override
173     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
174         return ChildrenCollectorFilter.collectChildNodes(this, nameGlobs);
175     }
176 
177     @Override
178     public PropertyIterator getProperties() throws RepositoryException {
179         return new PropertyIteratorAdapter(this.properties);
180     }
181 
182     @Override
183     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
184         return ChildrenCollectorFilter.collectProperties(this, namePattern);
185     }
186 
187     @Override
188     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
189         return ChildrenCollectorFilter.collectProperties(this, nameGlobs);
190     }
191 
192     @Override
193     public void setPrimaryType(String primaryType) throws RepositoryException {
194         throw new UnsupportedOperationException("Not implemented.");
195     }
196 
197     @Override
198     public Node addNode(String s) throws RepositoryException {
199         throw new UnsupportedOperationException("Not implemented.");
200     }
201 
202     @Override
203     public Node addNode(String s, String s1) throws RepositoryException {
204         throw new UnsupportedOperationException("Not implemented.");
205     }
206 
207     @Override
208     public void orderBefore(String s, String s1) throws RepositoryException {
209         throw new UnsupportedOperationException("Not implemented.");
210     }
211 
212     @Override
213     public Property setProperty(String s, Value value) throws RepositoryException {
214         throw new UnsupportedOperationException("Not implemented.");
215     }
216 
217     @Override
218     public Property setProperty(String s, Value[] values) throws RepositoryException {
219         throw new UnsupportedOperationException("Not implemented.");
220     }
221 
222     @Override
223     public Property setProperty(String s, Binary binary) throws RepositoryException {
224         throw new UnsupportedOperationException("Not implemented.");
225     }
226 
227     @Override
228     public Property setProperty(String s, BigDecimal bigDecimal) throws RepositoryException {
229         throw new UnsupportedOperationException("Not implemented.");
230     }
231 
232     @Override
233     public PropertyIterator getReferences() throws RepositoryException {
234         throw new UnsupportedOperationException("Not implemented.");
235     }
236 
237     @Override
238     public PropertyIterator getReferences(String s) throws RepositoryException {
239         throw new UnsupportedOperationException("Not implemented.");
240     }
241 
242     @Override
243     public PropertyIterator getWeakReferences() throws RepositoryException {
244         throw new UnsupportedOperationException("Not implemented.");
245     }
246 
247     @Override
248     public PropertyIterator getWeakReferences(String s) throws RepositoryException {
249         throw new UnsupportedOperationException("Not implemented.");
250     }
251 
252     @Override
253     public Item getPrimaryItem() throws RepositoryException {
254         throw new UnsupportedOperationException("Not implemented.");
255     }
256 
257     @Override
258     public void addMixin(String s) throws RepositoryException {
259         throw new UnsupportedOperationException("Not implemented.");
260     }
261 
262     @Override
263     public void removeMixin(String s) throws RepositoryException {
264         throw new UnsupportedOperationException("Not implemented.");
265     }
266 
267     @Override
268     public boolean canAddMixin(String s) throws RepositoryException {
269         return false;
270     }
271 
272     @Override
273     public NodeDefinition getDefinition() throws RepositoryException {
274         throw new UnsupportedOperationException("Not implemented.");
275     }
276 
277     @Override
278     public Version checkin() throws RepositoryException {
279         throw new UnsupportedOperationException("Not implemented.");
280     }
281 
282     @Override
283     public void checkout() throws RepositoryException {
284         throw new UnsupportedOperationException("Not implemented.");
285     }
286 
287     @Override
288     public void doneMerge(Version version) throws RepositoryException {
289         throw new UnsupportedOperationException("Not implemented.");
290     }
291 
292     @Override
293     public void cancelMerge(Version version) throws RepositoryException {
294         throw new UnsupportedOperationException("Not implemented.");
295     }
296 
297     @Override
298     public void update(String s) throws RepositoryException {
299         throw new UnsupportedOperationException("Not implemented.");
300     }
301 
302     @Override
303     public NodeIterator merge(String s, boolean b) throws RepositoryException {
304         throw new UnsupportedOperationException("Not implemented.");
305     }
306 
307     @Override
308     public String getCorrespondingNodePath(String s) throws RepositoryException {
309         throw new UnsupportedOperationException("Not implemented.");
310     }
311 
312     @Override
313     public NodeIterator getSharedSet() throws RepositoryException {
314         throw new UnsupportedOperationException("Not implemented.");
315     }
316 
317     @Override
318     public void removeSharedSet() throws RepositoryException {
319         throw new UnsupportedOperationException("Not implemented.");
320     }
321 
322     @Override
323     public void removeShare() throws RepositoryException {
324         throw new UnsupportedOperationException("Not implemented.");
325     }
326 
327     @Override
328     public void restore(Version version, String s, boolean b) throws RepositoryException {
329         throw new UnsupportedOperationException("Not implemented.");
330     }
331 
332     @Override
333     public Version getBaseVersion() throws RepositoryException {
334         throw new UnsupportedOperationException("Not implemented.");
335     }
336 
337     @Override
338     public Lock lock(boolean b, boolean b1) throws RepositoryException {
339         throw new UnsupportedOperationException("Not implemented.");
340     }
341 
342     @Override
343     public Lock getLock() throws RepositoryException {
344         throw new UnsupportedOperationException("Not implemented.");
345     }
346 
347     @Override
348     public void unlock() throws RepositoryException {
349         throw new UnsupportedOperationException("Not implemented.");
350     }
351 
352     @Override
353     public void followLifecycleTransition(String s) throws RepositoryException {
354         throw new UnsupportedOperationException("Not implemented.");
355     }
356 
357     @Override
358     public String[] getAllowedLifecycleTransistions() throws RepositoryException {
359         return new String[0];
360     }
361 
362     @Override
363     public void save() throws RepositoryException {
364         throw new UnsupportedOperationException("Not implemented.");
365     }
366 
367     @Override
368     public void refresh(boolean b) throws RepositoryException {
369         throw new UnsupportedOperationException("Not implemented.");
370     }
371 
372     @Override
373     public void remove() throws RepositoryException {
374         throw new UnsupportedOperationException("Not implemented.");
375     }
376 }