View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.jcr.wrapper;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  
38  import java.io.InputStream;
39  import java.math.BigDecimal;
40  import java.util.Calendar;
41  
42  import javax.jcr.AccessDeniedException;
43  import javax.jcr.Binary;
44  import javax.jcr.InvalidItemStateException;
45  import javax.jcr.InvalidLifecycleTransitionException;
46  import javax.jcr.Item;
47  import javax.jcr.ItemExistsException;
48  import javax.jcr.ItemNotFoundException;
49  import javax.jcr.ItemVisitor;
50  import javax.jcr.MergeException;
51  import javax.jcr.NoSuchWorkspaceException;
52  import javax.jcr.Node;
53  import javax.jcr.NodeIterator;
54  import javax.jcr.PathNotFoundException;
55  import javax.jcr.Property;
56  import javax.jcr.PropertyIterator;
57  import javax.jcr.ReferentialIntegrityException;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.Session;
60  import javax.jcr.UnsupportedRepositoryOperationException;
61  import javax.jcr.Value;
62  import javax.jcr.ValueFormatException;
63  import javax.jcr.lock.Lock;
64  import javax.jcr.lock.LockException;
65  import javax.jcr.nodetype.ConstraintViolationException;
66  import javax.jcr.nodetype.NoSuchNodeTypeException;
67  import javax.jcr.nodetype.NodeDefinition;
68  import javax.jcr.nodetype.NodeType;
69  import javax.jcr.version.ActivityViolationException;
70  import javax.jcr.version.Version;
71  import javax.jcr.version.VersionException;
72  import javax.jcr.version.VersionHistory;
73  
74  /**
75   * Wrapper for JCR node.
76   */
77  public abstract class DelegateNodeWrapper implements Node, Cloneable {
78  
79      protected Node wrapped;
80  
81      protected DelegateNodeWrapper() {
82      }
83  
84      protected DelegateNodeWrapper(Node node) {
85          setWrappedNode(node);
86      }
87  
88      public Node getWrappedNode() {
89          return this.wrapped;
90      }
91  
92      public void setWrappedNode(Node node) {
93          if (NodeUtil.isWrappedWith(node, this.getClass())) {
94              throw new IllegalArgumentException(node + " is already wrapped by " + this.getClass().getName() + " and double wrapping is not supported.");
95          }
96          this.wrapped = node;
97      }
98  
99      /**
100      * Recursively checks if node is wrapped by this class. Subclasses of the class ARE taken into account. Override if you want to allow rewrapping by subclasses.
101      */
102     protected boolean isWrapping(Node node) {
103         return NodeUtil.isWrappedWith(node, this.getClass());
104     }
105 
106     @Override
107     public String toString() {
108         return wrapped != null ? wrapped.toString() : "";
109     }
110 
111     /////////////
112     //
113     //  Delegating method stubs
114     //
115     /////////////
116 
117     @Override
118     public void addMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
119         getWrappedNode().addMixin(mixinName);
120     }
121 
122     @Override
123     public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
124         return getWrappedNode().addNode(relPath);
125     }
126 
127     @Override
128     public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
129         return getWrappedNode().addNode(relPath, primaryNodeTypeName);
130     }
131 
132     @Override
133     public boolean canAddMixin(String mixinName) throws NoSuchNodeTypeException, RepositoryException {
134         return getWrappedNode().canAddMixin(mixinName);
135     }
136 
137     @Override
138     public void cancelMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {
139         getWrappedNode().cancelMerge(version);
140     }
141 
142     @Override
143     public Version checkin() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException {
144         return getWrappedNode().checkin();
145     }
146 
147     @Override
148     public void checkout() throws UnsupportedRepositoryOperationException, LockException, ActivityViolationException, RepositoryException {
149         getWrappedNode().checkout();
150     }
151 
152     @Override
153     public void doneMerge(Version version) throws VersionException, InvalidItemStateException, UnsupportedRepositoryOperationException, RepositoryException {
154 
155     }
156 
157     @Override
158     public void followLifecycleTransition(String transition) throws UnsupportedRepositoryOperationException, InvalidLifecycleTransitionException, RepositoryException {
159         getWrappedNode().followLifecycleTransition(transition);
160     }
161 
162     @Override
163     public String[] getAllowedLifecycleTransistions() throws UnsupportedRepositoryOperationException, RepositoryException {
164         return getWrappedNode().getAllowedLifecycleTransistions();
165     }
166 
167     @Override
168     public Version getBaseVersion() throws UnsupportedRepositoryOperationException, RepositoryException {
169         return getWrappedNode().getBaseVersion();
170     }
171 
172     @Override
173     public String getCorrespondingNodePath(String workspaceName) throws ItemNotFoundException, NoSuchWorkspaceException, AccessDeniedException, RepositoryException {
174         return getWrappedNode().getCorrespondingNodePath(workspaceName);
175     }
176 
177     @Override
178     public NodeDefinition getDefinition() throws RepositoryException {
179         return getWrappedNode().getDefinition();
180     }
181 
182     @Override
183     public String getIdentifier() throws RepositoryException {
184         return getWrappedNode().getIdentifier();
185     }
186 
187     @Override
188     public int getIndex() throws RepositoryException {
189         return getWrappedNode().getIndex();
190     }
191 
192     @Override
193     public Lock getLock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, RepositoryException {
194         return getWrappedNode().getLock();
195     }
196 
197     @Override
198     public NodeType[] getMixinNodeTypes() throws RepositoryException {
199         return getWrappedNode().getMixinNodeTypes();
200     }
201 
202     @Override
203     public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
204         return getWrappedNode().getNode(relPath);
205     }
206 
207     @Override
208     public NodeIterator getNodes() throws RepositoryException {
209         return getWrappedNode().getNodes();
210     }
211 
212     @Override
213     public NodeIterator getNodes(String namePattern) throws RepositoryException {
214         return getWrappedNode().getNodes(namePattern);
215     }
216 
217     @Override
218     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
219         return getWrappedNode().getNodes(nameGlobs);
220     }
221 
222     @Override
223     public Item getPrimaryItem() throws ItemNotFoundException, RepositoryException {
224         return getWrappedNode().getPrimaryItem();
225     }
226 
227     @Override
228     public NodeType getPrimaryNodeType() throws RepositoryException {
229         return getWrappedNode().getPrimaryNodeType();
230     }
231 
232     @Override
233     public PropertyIterator getProperties() throws RepositoryException {
234         return getWrappedNode().getProperties();
235     }
236 
237     @Override
238     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
239         return getWrappedNode().getProperties(namePattern);
240     }
241 
242     @Override
243     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
244         return getWrappedNode().getProperties(nameGlobs);
245     }
246 
247     @Override
248     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
249         return getWrappedNode().getProperty(relPath);
250     }
251 
252     @Override
253     public PropertyIterator getReferences() throws RepositoryException {
254         return getWrappedNode().getReferences();
255     }
256 
257     @Override
258     public PropertyIterator getReferences(String name) throws RepositoryException {
259         return getWrappedNode().getReferences(name);
260     }
261 
262     @Override
263     public NodeIterator getSharedSet() throws RepositoryException {
264         return getWrappedNode().getSharedSet();
265     }
266 
267     @Override
268     public String getUUID() throws UnsupportedRepositoryOperationException, RepositoryException {
269         return getWrappedNode().getUUID();
270     }
271 
272     @Override
273     public VersionHistory getVersionHistory() throws UnsupportedRepositoryOperationException, RepositoryException {
274         return getWrappedNode().getVersionHistory();
275     }
276 
277     @Override
278     public PropertyIterator getWeakReferences() throws RepositoryException {
279         return getWrappedNode().getWeakReferences();
280     }
281 
282     @Override
283     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
284         return getWrappedNode().getWeakReferences(name);
285     }
286 
287     @Override
288     public boolean hasNode(String relPath) throws RepositoryException {
289         return getWrappedNode().hasNode(relPath);
290     }
291 
292     @Override
293     public boolean hasNodes() throws RepositoryException {
294         return getWrappedNode().hasNodes();
295     }
296 
297     @Override
298     public boolean hasProperties() throws RepositoryException {
299         return getWrappedNode().hasProperties();
300     }
301 
302     @Override
303     public boolean hasProperty(String relPath) throws RepositoryException {
304         return getWrappedNode().hasProperty(relPath);
305     }
306 
307     @Override
308     public boolean holdsLock() throws RepositoryException {
309         return getWrappedNode().holdsLock();
310     }
311 
312     @Override
313     public boolean isCheckedOut() throws RepositoryException {
314         return getWrappedNode().isCheckedOut();
315     }
316 
317     @Override
318     public boolean isLocked() throws RepositoryException {
319         return getWrappedNode().isLocked();
320     }
321 
322     @Override
323     public boolean isNodeType(String nodeTypeName) throws RepositoryException {
324         return getWrappedNode().isNodeType(nodeTypeName);
325     }
326 
327     @Override
328     public Lock lock(boolean isDeep, boolean isSessionScoped) throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {
329         return getWrappedNode().lock(isDeep, isSessionScoped);
330     }
331 
332     @Override
333     public NodeIterator merge(String srcWorkspace, boolean bestEffort) throws NoSuchWorkspaceException, AccessDeniedException, MergeException, LockException, InvalidItemStateException, RepositoryException {
334         return getWrappedNode().merge(srcWorkspace, bestEffort);
335     }
336 
337     @Override
338     public void orderBefore(String srcChildRelPath, String destChildRelPath) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
339         getWrappedNode().orderBefore(srcChildRelPath, destChildRelPath);
340     }
341 
342     @Override
343     public void removeMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
344         getWrappedNode().removeMixin(mixinName);
345     }
346 
347     @Override
348     public void removeShare() throws VersionException, LockException, ConstraintViolationException, RepositoryException {
349         getWrappedNode().removeShare();
350     }
351 
352     @Override
353     public void removeSharedSet() throws VersionException, LockException, ConstraintViolationException, RepositoryException {
354         getWrappedNode().removeSharedSet();
355     }
356 
357     @Override
358     public void restore(String versionName, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
359         getWrappedNode().restore(versionName, removeExisting);
360     }
361 
362     @Override
363     public void restore(Version version, boolean removeExisting) throws VersionException, ItemExistsException, InvalidItemStateException, UnsupportedRepositoryOperationException, LockException, RepositoryException {
364         getWrappedNode().restore(version, removeExisting);
365     }
366 
367     @Override
368     public void restore(Version version, String relPath, boolean removeExisting) throws PathNotFoundException, ItemExistsException, VersionException, ConstraintViolationException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
369         getWrappedNode().restore(version, relPath, removeExisting);
370     }
371 
372     @Override
373     public void restoreByLabel(String versionLabel, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
374         getWrappedNode().restoreByLabel(versionLabel, removeExisting);
375     }
376 
377     @Override
378     public void setPrimaryType(String nodeTypeName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
379         getWrappedNode().setPrimaryType(nodeTypeName);
380     }
381 
382     @Override
383     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
384         return getWrappedNode().setProperty(name, value);
385     }
386 
387     @Override
388     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
389         return getWrappedNode().setProperty(name, values);
390     }
391 
392     @Override
393     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
394         return getWrappedNode().setProperty(name, values);
395     }
396 
397     @Override
398     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
399         return getWrappedNode().setProperty(name, value);
400     }
401 
402     @Override
403     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
404         return getWrappedNode().setProperty(name, value);
405     }
406 
407     @Override
408     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
409         return getWrappedNode().setProperty(name, value);
410     }
411 
412     @Override
413     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
414         return getWrappedNode().setProperty(name, value);
415     }
416 
417     @Override
418     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
419         return getWrappedNode().setProperty(name, value);
420     }
421 
422     @Override
423     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
424         return getWrappedNode().setProperty(name, value);
425     }
426 
427     @Override
428     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
429         return getWrappedNode().setProperty(name, value);
430     }
431 
432     @Override
433     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
434         return getWrappedNode().setProperty(name, value);
435     }
436 
437     @Override
438     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
439         return getWrappedNode().setProperty(name, value);
440     }
441 
442     @Override
443     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
444         return getWrappedNode().setProperty(name, value, type);
445     }
446 
447     @Override
448     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
449         return getWrappedNode().setProperty(name, values, type);
450     }
451 
452     @Override
453     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
454         return getWrappedNode().setProperty(name, values, type);
455     }
456 
457     @Override
458     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
459         return getWrappedNode().setProperty(name, value, type);
460     }
461 
462     @Override
463     public void unlock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {
464         getWrappedNode().unlock();
465     }
466 
467     @Override
468     public void update(String srcWorkspace) throws NoSuchWorkspaceException, AccessDeniedException, LockException, InvalidItemStateException, RepositoryException {
469         getWrappedNode().update(srcWorkspace);
470     }
471 
472     @Override
473     public void accept(ItemVisitor visitor) throws RepositoryException {
474         getWrappedNode().accept(visitor);
475     }
476 
477     @Override
478     public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
479         return getWrappedNode().getAncestor(depth);
480     }
481 
482     @Override
483     public int getDepth() throws RepositoryException {
484         return getWrappedNode().getDepth();
485     }
486 
487     @Override
488     public String getName() throws RepositoryException {
489         return getWrappedNode().getName();
490     }
491 
492     @Override
493     public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
494         return getWrappedNode().getParent();
495     }
496 
497     @Override
498     public String getPath() throws RepositoryException {
499         return getWrappedNode().getPath();
500     }
501 
502     @Override
503     public Session getSession() throws RepositoryException {
504         return getWrappedNode().getSession();
505     }
506 
507     @Override
508     public boolean isModified() {
509         return getWrappedNode().isModified();
510     }
511 
512     @Override
513     public boolean isNew() {
514         return getWrappedNode().isNew();
515     }
516 
517     @Override
518     public boolean isNode() {
519         return getWrappedNode().isNode();
520     }
521 
522     @Override
523     public boolean isSame(Item otherItem) throws RepositoryException {
524 
525         if (this == otherItem) {
526             return true;
527         }
528         if (otherItem instanceof Node) {
529             return this.getIdentifier().equals(((Node) otherItem).getIdentifier())
530                     && getSession().getWorkspace().getName().equals(
531                     otherItem.getSession().getWorkspace().getName());
532         }
533         return false;
534     }
535 
536     @Override
537     public void refresh(boolean keepChanges) throws InvalidItemStateException, RepositoryException {
538         getWrappedNode().refresh(keepChanges);
539     }
540 
541     @Override
542     public void remove() throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
543         getWrappedNode().remove();
544     }
545 
546     @Override
547     public void save() throws AccessDeniedException, ItemExistsException, ConstraintViolationException, InvalidItemStateException, ReferentialIntegrityException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
548         getWrappedNode().save();
549     }
550 
551     /**
552      * Removes a wrapper by type. The wrapper can be deep in a chain of wrappers in which case wrappers before it will
553      * be cloned creating a new chain that leads to the same real node.
554      */
555     public Node deepUnwrap(Class<? extends DelegateNodeWrapper> wrapper) {
556 
557         if (this.getClass().equals(wrapper)) {
558             return getWrappedNode();
559         }
560 
561         Node next = getWrappedNode();
562 
563         // If the next node is the real node then we can skip cloning ourselves.
564         // This happens when the wrapper to remove isn't present
565         if (!(next instanceof DelegateNodeWrapper)) {
566             return this;
567         }
568 
569         // We let the next wrapper do deepUnwrap first, if it returns itself then the wrapper isn't in the chain and cloning is not necessary
570         Node deepUnwrappedNext = ((DelegateNodeWrapper) next).deepUnwrap(wrapper);
571         if (deepUnwrappedNext == next) {
572             return this;
573         }
574 
575         try {
576             DelegateNodeWrappernfo/magnolia/jcr/wrapper/DelegateNodeWrapper.html#DelegateNodeWrapper">DelegateNodeWrapper clone = ((DelegateNodeWrapper) this.clone());
577             clone.initClone(deepUnwrappedNext);
578             return clone;
579         } catch (CloneNotSupportedException e) {
580             throw new RuntimeException("Failed to unwrap " + this.getClass().getName() + " due to " + e.getMessage(), e);
581         }
582     }
583 
584     protected void initClone(Node newNode) {
585         setWrappedNode(newNode);
586     }
587 
588     @Override
589     protected Object clone() throws CloneNotSupportedException {
590         // just shallow clone ... keep it that way at least for wrappedNode otherwise deepUnwrap generates zillions of objects unnecessarily
591         return super.clone();
592     }
593 }