View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.decoration;
35  
36  import javax.jcr.AccessDeniedException;
37  import javax.jcr.ItemExistsException;
38  import javax.jcr.ItemNotFoundException;
39  import javax.jcr.Node;
40  import javax.jcr.NodeIterator;
41  import javax.jcr.PathNotFoundException;
42  import javax.jcr.Property;
43  import javax.jcr.PropertyIterator;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.Session;
46  import javax.jcr.lock.LockException;
47  import javax.jcr.nodetype.ConstraintViolationException;
48  import javax.jcr.nodetype.NoSuchNodeTypeException;
49  import javax.jcr.version.VersionException;
50  
51  import info.magnolia.jcr.wrapper.DelegateNodeWrapper;
52  
53  /**
54   * Node wrapper that applies wrappers and filtering by delegating to a {@link ContentDecorator}.
55   *
56   * @version $Id$
57   */
58  public class ContentDecoratorNodeWrapper extends DelegateNodeWrapper {
59  
60      private final ContentDecorator contentDecorator;
61  
62      public ContentDecoratorNodeWrapper(Node node, ContentDecorator contentDecorator) {
63          super(node);
64          this.contentDecorator = contentDecorator;
65      }
66  
67      @Override
68      public Session getSession() throws RepositoryException {
69          return wrapSession(super.getSession());
70      }
71  
72      @Override
73      public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
74          return wrapNode(super.getParent());
75      }
76  
77      @Override
78      public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
79          return wrapNode(super.addNode(relPath));
80      }
81  
82      @Override
83      public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
84          return wrapNode(super.addNode(relPath, primaryNodeTypeName));
85      }
86  
87      @Override
88      public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
89          Node node = super.getNode(relPath);
90          if (!contentDecorator.evaluateNode(node)) {
91              throw new PathNotFoundException(relPath);
92          }
93          return wrapNode(node);
94      }
95  
96      @Override
97      public NodeIterator getNodes() throws RepositoryException {
98          return wrapNodeIterator(super.getNodes());
99      }
100 
101     @Override
102     public NodeIterator getNodes(String namePattern) throws RepositoryException {
103         return wrapNodeIterator(super.getNodes(namePattern));
104     }
105 
106     @Override
107     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
108         return wrapNodeIterator(super.getNodes(nameGlobs));
109     }
110 
111     @Override
112     public boolean hasNode(String relPath) throws RepositoryException {
113         return super.hasNode(relPath) && contentDecorator.evaluateNode(super.getNode(relPath));
114     }
115 
116     @Override
117     public boolean hasNodes() throws RepositoryException {
118         return getNodes().hasNext();
119     }
120 
121     @Override
122     public PropertyIterator getProperties() throws RepositoryException {
123         return wrapPropertyIterator(super.getProperties());
124     }
125 
126     @Override
127     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
128         return wrapPropertyIterator(super.getProperties(namePattern));
129     }
130 
131     @Override
132     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
133         return wrapPropertyIterator(super.getProperties(nameGlobs));
134     }
135 
136     @Override
137     public PropertyIterator getWeakReferences() throws RepositoryException {
138         return wrapPropertyIterator(super.getWeakReferences());
139     }
140 
141     @Override
142     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
143         return wrapProperty(super.getProperty(relPath));
144     }
145 
146     @Override
147     public PropertyIterator getReferences(String name) throws RepositoryException {
148         return wrapPropertyIterator(super.getReferences(name));
149     }
150 
151     @Override
152     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
153         return wrapPropertyIterator(super.getWeakReferences(name));
154     }
155 
156     @Override
157     public PropertyIterator getReferences() throws RepositoryException {
158         return wrapPropertyIterator(super.getReferences());
159     }
160 
161     protected Session wrapSession(Session session) {
162         return contentDecorator.wrapSession(session);
163     }
164 
165     protected Node wrapNode(Node node) {
166         return contentDecorator.wrapNode(node);
167     }
168 
169     protected NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
170         return contentDecorator.wrapNodeIterator(nodeIterator);
171     }
172 
173     protected Property wrapProperty(Property property) {
174         return contentDecorator.wrapProperty(property);
175     }
176 
177     protected PropertyIterator wrapPropertyIterator(PropertyIterator propertyIterator) {
178         return contentDecorator.wrapPropertyIterator(propertyIterator);
179     }
180 }