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.Item;
38  import javax.jcr.ItemExistsException;
39  import javax.jcr.ItemNotFoundException;
40  import javax.jcr.Node;
41  import javax.jcr.PathNotFoundException;
42  import javax.jcr.Property;
43  import javax.jcr.RepositoryException;
44  import javax.jcr.Session;
45  import javax.jcr.lock.LockException;
46  import javax.jcr.nodetype.ConstraintViolationException;
47  import javax.jcr.version.VersionException;
48  
49  import info.magnolia.jcr.wrapper.DelegateSessionWrapper;
50  
51  /**
52   * Session wrapper that applies wrappers and filtering by delegating to a {@link ContentDecorator}.
53   *
54   * @version $Id$
55   */
56  public class ContentDecoratorSessionWrapper extends DelegateSessionWrapper {
57  
58      private final ContentDecorator contentDecorator;
59  
60      public ContentDecoratorSessionWrapper(Session session, ContentDecorator contentDecorator) {
61          super(session);
62          this.contentDecorator = contentDecorator;
63      }
64  
65      @Override
66      public boolean itemExists(String absPath) throws RepositoryException {
67          boolean exists = super.itemExists(absPath);
68          if (!exists) {
69              return false;
70          }
71          Item item = super.getItem(absPath);
72          if (item.isNode()) {
73              return contentDecorator.evaluateNode((Node) item);
74          } else {
75              return contentDecorator.evaluateProperty((Property) item);
76          }
77      }
78  
79      @Override
80      public Item getItem(String absPath) throws PathNotFoundException, RepositoryException {
81          Item item = super.getItem(absPath);
82          if (item.isNode()) {
83              if (!contentDecorator.evaluateNode((Node) item)) {
84                  throw new PathNotFoundException(absPath);
85              }
86              return wrapNode((Node) item);
87          } else {
88              if (!contentDecorator.evaluateProperty((Property) item)) {
89                  throw new PathNotFoundException(absPath);
90              }
91              return wrapProperty((Property) item);
92          }
93      }
94  
95      @Override
96      public void removeItem(String absPath) throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
97          if (!itemExists(absPath)) {
98              throw new PathNotFoundException(absPath);
99          }
100         super.removeItem(absPath);
101     }
102 
103     @Override
104     public Node getRootNode() throws RepositoryException {
105         return wrapNode(super.getRootNode());
106     }
107 
108     @Override
109     public boolean nodeExists(String absPath) throws RepositoryException {
110         return super.nodeExists(absPath) && contentDecorator.evaluateNode(super.getNode(absPath));
111     }
112 
113     @Override
114     public Node getNode(String absPath) throws PathNotFoundException, RepositoryException {
115         Node node = super.getNode(absPath);
116         if (!contentDecorator.evaluateNode(node)) {
117             throw new PathNotFoundException(absPath);
118         }
119         return wrapNode(node);
120     }
121 
122     @Override
123     public Node getNodeByIdentifier(String id) throws ItemNotFoundException, RepositoryException {
124         Node node = super.getNodeByIdentifier(id);
125         if (!contentDecorator.evaluateNode(node)) {
126             throw new ItemNotFoundException(id);
127         }
128         return wrapNode(node);
129     }
130 
131     @Override
132     public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
133         Node node = super.getNodeByUUID(uuid);
134         if (!contentDecorator.evaluateNode(node)) {
135             throw new ItemNotFoundException(uuid);
136         }
137         return wrapNode(node);
138     }
139 
140     @Override
141     public boolean propertyExists(String absPath) throws RepositoryException {
142         return super.propertyExists(absPath) && contentDecorator.evaluateProperty(super.getProperty(absPath));
143     }
144 
145     @Override
146     public Property getProperty(String absPath) throws PathNotFoundException, RepositoryException {
147         Property property = super.getProperty(absPath);
148         if (!contentDecorator.evaluateProperty(property)) {
149             throw new PathNotFoundException(absPath);
150         }
151         return wrapProperty(property);
152     }
153 
154     @Override
155     public void move(String srcAbsPath, String destAbsPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
156         if (!nodeExists(srcAbsPath)) {
157             throw new PathNotFoundException(srcAbsPath);
158         }
159         super.move(srcAbsPath, destAbsPath);
160     }
161 
162     protected Node wrapNode(Node node) {
163         return contentDecorator.wrapNode(node);
164     }
165 
166     protected Property wrapProperty(Property property) {
167         return contentDecorator.wrapProperty(property);
168     }
169 }