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.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() && isWrapping(node)) {
83              throw new IllegalArgumentException(node + " is already decorated by " + this.contentDecorator.getClass());
84          }
85          this.wrapped = node;
86      }
87  
88      /**
89       * Recursively checks if node is decorated by this class. Subclasses ARE NOT taken into account.
90       */
91      @Override
92      protected boolean isWrapping(Node node) {
93          if (getContentDecorator() instanceof AbstractContentDecorator) {
94              return ((AbstractContentDecorator) getContentDecorator()).isDecorating(node);
95          } else {
96              return super.isWrapping(node);
97          }
98      }
99  
100     public D getContentDecorator() {
101         return contentDecorator;
102     }
103 
104     @Override
105     public Session getSession() throws RepositoryException {
106         return wrapSession(super.getSession());
107     }
108 
109     @Override
110     public Item getAncestor(int depth) throws ItemNotFoundException, AccessDeniedException, RepositoryException {
111         Item item = super.getAncestor(depth);
112         if (item.isNode()) {
113             return wrapNode((Node) item);
114         } else {
115             return wrapProperty((Property) item);
116         }
117     }
118 
119     @Override
120     public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
121         return wrapNode(super.getParent());
122     }
123 
124     @Override
125     public Node addNode(String relPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
126         return wrapNode(super.addNode(relPath));
127     }
128 
129     @Override
130     public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
131         return wrapNode(super.addNode(relPath, primaryNodeTypeName));
132     }
133 
134     @Override
135     public Version checkin() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException {
136         return wrapVersion(super.checkin());
137     }
138 
139     @Override
140     public VersionHistory getVersionHistory() throws UnsupportedRepositoryOperationException, RepositoryException {
141         return wrapVersionHistory(super.getVersionHistory());
142     }
143 
144     @Override
145     public Version getBaseVersion() throws UnsupportedRepositoryOperationException, RepositoryException {
146         return wrapVersion(super.getBaseVersion());
147     }
148 
149     @Override
150     public Node getNode(String relPath) throws PathNotFoundException, RepositoryException {
151         Node node = super.getNode(relPath);
152         if (!contentDecorator.evaluateNode(node)) {
153             throw new PathNotFoundException(relPath);
154         }
155         return wrapNode(node);
156     }
157 
158     @Override
159     public NodeIterator getNodes() throws RepositoryException {
160         return wrapNodeIterator(super.getNodes());
161     }
162 
163     @Override
164     public NodeIterator getNodes(String namePattern) throws RepositoryException {
165         return wrapNodeIterator(super.getNodes(namePattern));
166     }
167 
168     @Override
169     public NodeIterator getNodes(String[] nameGlobs) throws RepositoryException {
170         return wrapNodeIterator(super.getNodes(nameGlobs));
171     }
172 
173     @Override
174     public boolean hasNode(String relPath) throws RepositoryException {
175         return super.hasNode(relPath) && contentDecorator.evaluateNode(super.getNode(relPath));
176     }
177 
178     @Override
179     public boolean hasNodes() throws RepositoryException {
180         return getNodes().hasNext();
181     }
182 
183     @Override
184     public PropertyIterator getProperties() throws RepositoryException {
185         return wrapPropertyIterator(super.getProperties());
186     }
187 
188     @Override
189     public PropertyIterator getProperties(String namePattern) throws RepositoryException {
190         return wrapPropertyIterator(super.getProperties(namePattern));
191     }
192 
193     @Override
194     public PropertyIterator getProperties(String[] nameGlobs) throws RepositoryException {
195         return wrapPropertyIterator(super.getProperties(nameGlobs));
196     }
197 
198     @Override
199     public PropertyIterator getWeakReferences() throws RepositoryException {
200         return wrapPropertyIterator(super.getWeakReferences());
201     }
202 
203     @Override
204     public Property getProperty(String relPath) throws PathNotFoundException, RepositoryException {
205         return wrapProperty(super.getProperty(relPath));
206     }
207 
208     @Override
209     public PropertyIterator getReferences(String name) throws RepositoryException {
210         return wrapPropertyIterator(super.getReferences(name));
211     }
212 
213     @Override
214     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
215         return wrapPropertyIterator(super.getWeakReferences(name));
216     }
217 
218     @Override
219     public PropertyIterator getReferences() throws RepositoryException {
220         return wrapPropertyIterator(super.getReferences());
221     }
222 
223     @Override
224     public Property setProperty(String name, BigDecimal value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
225         return wrapProperty(super.setProperty(name, value));
226     }
227 
228     @Override
229     public Property setProperty(String name, Binary value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
230         return wrapProperty(super.setProperty(name, value));
231     }
232 
233     @Override
234     public Property setProperty(String name, boolean value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
235         return wrapProperty(super.setProperty(name, value));
236     }
237 
238     @Override
239     public Property setProperty(String name, Calendar value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
240         return wrapProperty(super.setProperty(name, value));
241     }
242 
243     @Override
244     public Property setProperty(String name, double value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
245         return wrapProperty(super.setProperty(name, value));
246     }
247 
248     @Override
249     public Property setProperty(String name, InputStream value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
250         return wrapProperty(super.setProperty(name, value));
251     }
252 
253     @Override
254     public Property setProperty(String name, long value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
255         return wrapProperty(super.setProperty(name, value));
256     }
257 
258     @Override
259     public Property setProperty(String name, Node value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
260         return wrapProperty(super.setProperty(name, value));
261     }
262 
263     @Override
264     public Property setProperty(String name, String value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
265         return wrapProperty(super.setProperty(name, value));
266     }
267 
268     @Override
269     public Property setProperty(String name, String value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
270         return wrapProperty(super.setProperty(name, value, type));
271     }
272 
273     @Override
274     public Property setProperty(String name, String[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
275         return wrapProperty(super.setProperty(name, values));
276     }
277 
278     @Override
279     public Property setProperty(String name, String[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
280         return wrapProperty(super.setProperty(name, values, type));
281     }
282 
283     @Override
284     public Property setProperty(String name, Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
285         return wrapProperty(super.setProperty(name, value));
286     }
287 
288     @Override
289     public Property setProperty(String name, Value value, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
290         return wrapProperty(super.setProperty(name, value, type));
291     }
292 
293     @Override
294     public Property setProperty(String name, Value[] values) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
295         return wrapProperty(super.setProperty(name, values));
296     }
297 
298     @Override
299     public Property setProperty(String name, Value[] values, int type) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
300         return wrapProperty(super.setProperty(name, values, type));
301     }
302 
303     protected Session wrapSession(Session session) {
304         return contentDecorator.wrapSession(session);
305     }
306 
307     protected Node wrapNode(Node node) {
308         return contentDecorator.wrapNode(node);
309     }
310 
311     protected NodeIterator wrapNodeIterator(NodeIterator nodeIterator) {
312         return contentDecorator.wrapNodeIterator(nodeIterator);
313     }
314 
315     protected Property wrapProperty(Property property) {
316         return contentDecorator.wrapProperty(property);
317     }
318 
319     protected PropertyIterator wrapPropertyIterator(PropertyIterator propertyIterator) {
320         return contentDecorator.wrapPropertyIterator(propertyIterator);
321     }
322 
323     protected Version wrapVersion(Version version) {
324         return contentDecorator.wrapVersion(version);
325     }
326 
327     protected VersionIterator wrapVersionIterator(VersionIterator versionIterator) {
328         return contentDecorator.wrapVersionIterator(versionIterator);
329     }
330 
331     protected VersionHistory wrapVersionHistory(VersionHistory versionHistory) {
332         return contentDecorator.wrapVersionHistory(versionHistory);
333     }
334 
335     @Override
336     public boolean equals(Object obj) {
337         if (obj == null) {
338             return false;
339         }
340 
341         if (!(obj instanceof ContentDecoratorNodeWrapper)) {
342             return false;
343         }
344         ContentDecoratorNodeWrappergnolia/jcr/decoration/ContentDecoratorNodeWrapper.html#ContentDecoratorNodeWrapper">ContentDecoratorNodeWrapper that = (ContentDecoratorNodeWrapper) obj;
345         return (this.wrapped == null ? that.wrapped == null : this.wrapped.equals(that.wrapped))
346                 && (this.contentDecorator == null ? that.contentDecorator == null : this.contentDecorator.equals(that.contentDecorator));
347     }
348 
349     @Override
350     public int hashCode() {
351         return (this.wrapped == null ? 7 : this.wrapped.hashCode()) + (this.contentDecorator == null ? 6 : this.contentDecorator.hashCode());
352     }
353 }