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