View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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 info.magnolia.jcr.wrapper.DelegateNodeWrapper;
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.Item;
46  import javax.jcr.ItemExistsException;
47  import javax.jcr.ItemNotFoundException;
48  import javax.jcr.Node;
49  import javax.jcr.NodeIterator;
50  import javax.jcr.PathNotFoundException;
51  import javax.jcr.Property;
52  import javax.jcr.PropertyIterator;
53  import javax.jcr.RepositoryException;
54  import javax.jcr.Session;
55  import javax.jcr.UnsupportedRepositoryOperationException;
56  import javax.jcr.Value;
57  import javax.jcr.ValueFormatException;
58  import javax.jcr.lock.LockException;
59  import javax.jcr.nodetype.ConstraintViolationException;
60  import javax.jcr.nodetype.NoSuchNodeTypeException;
61  import javax.jcr.version.Version;
62  import javax.jcr.version.VersionException;
63  import javax.jcr.version.VersionHistory;
64  import javax.jcr.version.VersionIterator;
65  
66  /**
67   * Node wrapper that applies wrappers and filtering by delegating to a {@link ContentDecorator}.
68   *
69   * @param <D> implementation of decorator.
70   */
71  public class ContentDecoratorNodeWrapper<D extends ContentDecorator> extends DelegateNodeWrapper {
72  
73      private final D contentDecorator;
74  
75      public ContentDecoratorNodeWrapper(Node node, D contentDecorator) {
76          this.contentDecorator = contentDecorator;
77          setWrappedNode(node);
78      }
79  
80      @Override
81      public void setWrappedNode(Node node) {
82          if (!contentDecorator.isMultipleWrapEnabled()) {
83              super.setWrappedNode(node);
84          } else {
85              this.wrapped = node;
86          }
87      }
88  
89      public D getContentDecorator() {
90          return contentDecorator;
91      }
92  
93      @Override
94      public Session getSession() throws RepositoryException {
95          return wrapSession(super.getSession());
96      }
97  
98      @Override
99      public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
100         Item item = super.getAncestor(depth);
101         if (item.isNode()) {
102             return wrapNode((Node) item);
103         } else {
104             return wrapProperty((Property) item);
105         }
106     }
107 
108     @Override
109     public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
110         return wrapNode(super.getParent());
111     }
112 
113     @Override
114     public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
115         return wrapNode(super.addNode(relPath));
116     }
117 
118     @Override
119     public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
120         return wrapNode(super.addNode(relPath, primaryNodeTypeName));
121     }
122 
123     @Override
124     public Version checkin() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException {
125         return wrapVersion(super.checkin());
126     }
127 
128     @Override
129     public VersionHistory getVersionHistory() throws UnsupportedRepositoryOperationException, RepositoryException {
130         return wrapVersionHistory(super.getVersionHistory());
131     }
132 
133     @Override
134     public Version getBaseVersion() throws UnsupportedRepositoryOperationException, RepositoryException {
135         return wrapVersion(super.getBaseVersion());
136     }
137 
138     @Override
139     public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
140         Node node = super.getNode(relPath);
141         if (!contentDecorator.evaluateNode(node)) {
142             throw new PathNotFoundException(relPath);
143         }
144         return wrapNode(node);
145     }
146 
147     @Override
148     public NodeIterator getNodes() throws RepositoryException {
149         return wrapNodeIterator(super.getNodes());
150     }
151 
152     @Override
153     public NodeIterator getNodes(String namePattern) throws RepositoryException {
154         return wrapNodeIterator(super.getNodes(namePattern));
155     }
156 
157     @Override
158     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
159         return wrapNodeIterator(super.getNodes(nameGlobs));
160     }
161 
162     @Override
163     public boolean hasNode(String relPath) throws RepositoryException {
164         return super.hasNode(relPath) && contentDecorator.evaluateNode(super.getNode(relPath));
165     }
166 
167     @Override
168     public boolean hasNodes() throws RepositoryException {
169         return getNodes().hasNext();
170     }
171 
172     @Override
173     public PropertyIterator getProperties() throws RepositoryException {
174         return wrapPropertyIterator(super.getProperties());
175     }
176 
177     @Override
178     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
179         return wrapPropertyIterator(super.getProperties(namePattern));
180     }
181 
182     @Override
183     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
184         return wrapPropertyIterator(super.getProperties(nameGlobs));
185     }
186 
187     @Override
188     public PropertyIterator getWeakReferences() throws RepositoryException {
189         return wrapPropertyIterator(super.getWeakReferences());
190     }
191 
192     @Override
193     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
194         return wrapProperty(super.getProperty(relPath));
195     }
196 
197     @Override
198     public PropertyIterator getReferences(String name) throws RepositoryException {
199         return wrapPropertyIterator(super.getReferences(name));
200     }
201 
202     @Override
203     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
204         return wrapPropertyIterator(super.getWeakReferences(name));
205     }
206 
207     @Override
208     public PropertyIterator getReferences() throws RepositoryException {
209         return wrapPropertyIterator(super.getReferences());
210     }
211 
212     @Override
213     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
214         return wrapProperty(super.setProperty(name, value));
215     }
216 
217     @Override
218     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
219         return wrapProperty(super.setProperty(name, value));
220     }
221 
222     @Override
223     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
224         return wrapProperty(super.setProperty(name, value));
225     }
226 
227     @Override
228     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
229         return wrapProperty(super.setProperty(name, value));
230     }
231 
232     @Override
233     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
234         return wrapProperty(super.setProperty(name, value));
235     }
236 
237     @Override
238     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
239         return wrapProperty(super.setProperty(name, value));
240     }
241 
242     @Override
243     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
244         return wrapProperty(super.setProperty(name, value));
245     }
246 
247     @Override
248     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
249         return wrapProperty(super.setProperty(name, value));
250     }
251 
252     @Override
253     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
254         return wrapProperty(super.setProperty(name, value));
255     }
256 
257     @Override
258     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
259         return wrapProperty(super.setProperty(name, value, type));
260     }
261 
262     @Override
263     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
264         return wrapProperty(super.setProperty(name, values));
265     }
266 
267     @Override
268     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
269         return wrapProperty(super.setProperty(name, values, type));
270     }
271 
272     @Override
273     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
274         return wrapProperty(super.setProperty(name, value));
275     }
276 
277     @Override
278     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
279         return wrapProperty(super.setProperty(name, value, type));
280     }
281 
282     @Override
283     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
284         return wrapProperty(super.setProperty(name, values));
285     }
286 
287     @Override
288     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
289         return wrapProperty(super.setProperty(name, values, type));
290     }
291 
292     protected Session wrapSession(Session session) {
293         return contentDecorator.wrapSession(session);
294     }
295 
296     protected Node wrapNode(Node node) {
297         return contentDecorator.wrapNode(node);
298     }
299 
300     protected NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
301         return contentDecorator.wrapNodeIterator(nodeIterator);
302     }
303 
304     protected Property wrapProperty(Property property) {
305         return contentDecorator.wrapProperty(property);
306     }
307 
308     protected PropertyIterator wrapPropertyIterator(PropertyIterator propertyIterator) {
309         return contentDecorator.wrapPropertyIterator(propertyIterator);
310     }
311 
312     protected Version wrapVersion(Version version) {
313         return contentDecorator.wrapVersion(version);
314     }
315 
316     protected VersionIterator wrapVersionIterator(VersionIterator versionIterator) {
317         return contentDecorator.wrapVersionIterator(versionIterator);
318     }
319 
320     protected VersionHistory wrapVersionHistory(VersionHistory versionHistory) {
321         return contentDecorator.wrapVersionHistory(versionHistory);
322     }
323 
324     @Override
325     public boolean equals(Object obj) {
326         if (obj == null) {
327             return false;
328         }
329 
330         if (!(obj instanceof ContentDecoratorNodeWrapper)) {
331             return false;
332         }
333         ContentDecoratorNodeWrapper that = (ContentDecoratorNodeWrapper) obj;
334         return (this.wrapped == null ? that.wrapped == null : this.wrapped.equals(that.wrapped))
335                 && (this.contentDecorator == null ? that.contentDecorator == null : this.contentDecorator.equals(that.contentDecorator));
336     }
337 
338     @Override
339     public int hashCode() {
340         return (this.wrapped == null ? 7 : this.wrapped.hashCode()) + (this.contentDecorator == null ? 6 : this.contentDecorator.hashCode());
341     }
342 }