View Javadoc

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