View Javadoc

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