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.wrapper;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  import java.security.AccessControlException;
40  
41  import javax.jcr.AccessDeniedException;
42  import javax.jcr.Credentials;
43  import javax.jcr.InvalidItemStateException;
44  import javax.jcr.InvalidSerializedDataException;
45  import javax.jcr.Item;
46  import javax.jcr.ItemExistsException;
47  import javax.jcr.ItemNotFoundException;
48  import javax.jcr.LoginException;
49  import javax.jcr.NamespaceException;
50  import javax.jcr.Node;
51  import javax.jcr.PathNotFoundException;
52  import javax.jcr.Property;
53  import javax.jcr.ReferentialIntegrityException;
54  import javax.jcr.Repository;
55  import javax.jcr.RepositoryException;
56  import javax.jcr.Session;
57  import javax.jcr.UnsupportedRepositoryOperationException;
58  import javax.jcr.ValueFactory;
59  import javax.jcr.Workspace;
60  import javax.jcr.lock.LockException;
61  import javax.jcr.nodetype.ConstraintViolationException;
62  import javax.jcr.nodetype.NoSuchNodeTypeException;
63  import javax.jcr.retention.RetentionManager;
64  import javax.jcr.security.AccessControlManager;
65  import javax.jcr.version.VersionException;
66  
67  import org.xml.sax.ContentHandler;
68  import org.xml.sax.SAXException;
69  
70  /**
71   * Wrapper for JCR Session.
72   */
73  public abstract class DelegateSessionWrapper implements Session, Cloneable {
74  
75      protected Session wrapped;
76  
77      protected DelegateSessionWrapper(Session wrapped) {
78          this.wrapped = wrapped;
79      }
80  
81      public Session getWrappedSession() {
82          return wrapped;
83      }
84  
85      public void setWrappedSession(Session session) {
86          this.wrapped = session;
87      }
88  
89      @Override
90      public String toString() {
91          return wrapped != null ? wrapped.toString() : "";
92      }
93  
94      /////////////
95      //
96      //  Delegating method stubs
97      //
98      /////////////
99  
100     @Override
101     public void addLockToken(String lt) {
102         getWrappedSession().addLockToken(lt);
103     }
104 
105     @Override
106     public void checkPermission(String absPath, String actions) throws AccessControlException, RepositoryException {
107         getWrappedSession().checkPermission(absPath, actions);
108     }
109 
110     @Override
111     public void exportDocumentView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
112         getWrappedSession().exportDocumentView(absPath, contentHandler, skipBinary, noRecurse);
113     }
114 
115     @Override
116     public void exportDocumentView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
117         getWrappedSession().exportDocumentView(absPath, out, skipBinary, noRecurse);
118     }
119 
120     @Override
121     public void exportSystemView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
122         getWrappedSession().exportSystemView(absPath, contentHandler, skipBinary, noRecurse);
123     }
124 
125     @Override
126     public void exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
127         getWrappedSession().exportSystemView(absPath, out, skipBinary, noRecurse);
128     }
129 
130     @Override
131     public AccessControlManager getAccessControlManager() throws UnsupportedRepositoryOperationException, RepositoryException {
132         return getWrappedSession().getAccessControlManager();
133     }
134 
135     @Override
136     public Object getAttribute(String name) {
137         return getWrappedSession().getAttribute(name);
138     }
139 
140     @Override
141     public String[] getAttributeNames() {
142         return getWrappedSession().getAttributeNames();
143     }
144 
145     @Override
146     public ContentHandler getImportContentHandler(String parentAbsPath, int uuidBehavior) throws PathNotFoundException, ConstraintViolationException, VersionException, LockException, RepositoryException {
147         return getWrappedSession().getImportContentHandler(parentAbsPath, uuidBehavior);
148     }
149 
150     @Override
151     public Item getItem(String absPath) throws PathNotFoundException, RepositoryException {
152         return getWrappedSession().getItem(absPath);
153     }
154 
155     @Override
156     public String[] getLockTokens() {
157         return getWrappedSession().getLockTokens();
158     }
159 
160     @Override
161     public String getNamespacePrefix(String uri) throws NamespaceException, RepositoryException {
162         return getWrappedSession().getNamespacePrefix(uri);
163     }
164 
165     @Override
166     public String[] getNamespacePrefixes() throws RepositoryException {
167         return getWrappedSession().getNamespacePrefixes();
168     }
169 
170     @Override
171     public String getNamespaceURI(String prefix) throws NamespaceException, RepositoryException {
172         return getWrappedSession().getNamespaceURI(prefix);
173     }
174 
175     @Override
176     public Node getNode(String absPath) throws PathNotFoundException, RepositoryException {
177         return getWrappedSession().getNode(absPath);
178     }
179 
180     @Override
181     public Node getNodeByIdentifier(String id) throws ItemNotFoundException, RepositoryException {
182         return getWrappedSession().getNodeByIdentifier(id);
183     }
184 
185     @Override
186     public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
187         return getWrappedSession().getNodeByUUID(uuid);
188     }
189 
190     @Override
191     public Property getProperty(String absPath) throws PathNotFoundException, RepositoryException {
192         return getWrappedSession().getProperty(absPath);
193     }
194 
195     @Override
196     public Repository getRepository() {
197         return getWrappedSession().getRepository();
198     }
199 
200     @Override
201     public RetentionManager getRetentionManager() throws UnsupportedRepositoryOperationException, RepositoryException {
202         return getWrappedSession().getRetentionManager();
203     }
204 
205     @Override
206     public Node getRootNode() throws RepositoryException {
207         return getWrappedSession().getRootNode();
208     }
209 
210     @Override
211     public String getUserID() {
212         return getWrappedSession().getUserID();
213     }
214 
215     @Override
216     public ValueFactory getValueFactory() throws UnsupportedRepositoryOperationException, RepositoryException {
217         return getWrappedSession().getValueFactory();
218     }
219 
220     @Override
221     public Workspace getWorkspace() {
222         return getWrappedSession().getWorkspace();
223     }
224 
225     @Override
226     public boolean hasCapability(String methodName, Object target, Object[] arguments) throws RepositoryException {
227         return getWrappedSession().hasCapability(methodName, target, arguments);
228     }
229 
230     @Override
231     public boolean hasPendingChanges() throws RepositoryException {
232         return getWrappedSession().hasPendingChanges();
233     }
234 
235     @Override
236     public boolean hasPermission(String absPath, String actions) throws RepositoryException {
237         return getWrappedSession().hasPermission(absPath, actions);
238     }
239 
240     @Override
241     public Session impersonate(Credentials credentials) throws LoginException, RepositoryException {
242         return getWrappedSession().impersonate(credentials);
243     }
244 
245     @Override
246     public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException {
247         getWrappedSession().importXML(parentAbsPath, in, uuidBehavior);
248     }
249 
250     @Override
251     public boolean isLive() {
252         return getWrappedSession().isLive();
253     }
254 
255     @Override
256     public boolean itemExists(String absPath) throws RepositoryException {
257         return getWrappedSession().itemExists(absPath);
258     }
259 
260     @Override
261     public void logout() {
262         getWrappedSession().logout();
263     }
264 
265     @Override
266     public void move(String srcAbsPath, String destAbsPath) throws ItemExistsException, PathNotFoundException, VersionException, ConstraintViolationException, LockException, RepositoryException {
267         getWrappedSession().move(srcAbsPath, destAbsPath);
268     }
269 
270     @Override
271     public boolean nodeExists(String absPath) throws RepositoryException {
272         return getWrappedSession().nodeExists(absPath);
273     }
274 
275     @Override
276     public boolean propertyExists(String absPath) throws RepositoryException {
277         return getWrappedSession().propertyExists(absPath);
278     }
279 
280     @Override
281     public void refresh(boolean keepChanges) throws RepositoryException {
282         getWrappedSession().refresh(keepChanges);
283     }
284 
285     @Override
286     public void removeItem(String absPath) throws VersionException, LockException, ConstraintViolationException, AccessDeniedException, RepositoryException {
287         getWrappedSession().removeItem(absPath);
288     }
289 
290     @Override
291     public void removeLockToken(String lt) {
292         getWrappedSession().removeLockToken(lt);
293     }
294 
295     @Override
296     public void save() throws AccessDeniedException, ItemExistsException, ReferentialIntegrityException, ConstraintViolationException, InvalidItemStateException, VersionException, LockException, NoSuchNodeTypeException, RepositoryException {
297         getWrappedSession().save();
298     }
299 
300     @Override
301     public void setNamespacePrefix(String prefix, String uri) throws NamespaceException, RepositoryException {
302         getWrappedSession().setNamespacePrefix(prefix, uri);
303     }
304 
305     /**
306      * @return the unwrapped proper JCRSession
307      */
308     public Session unwrap() {
309         Session session = getWrappedSession();
310         if (session instanceof DelegateSessionWrapper) {
311             session = ((DelegateSessionWrapper) session).unwrap();
312         }
313         return session;
314     }
315 
316     /**
317      * Removes a wrapper by type. The wrapper can be deep in a chain of wrappers in which case wrappers before it will
318      * be cloned creating a new chain that leads to the same real node.
319      */
320     public Session deepUnwrap(Class<? extends DelegateSessionWrapper> wrapper) {
321 
322         if (this.getClass().equals(wrapper)) {
323             return getWrappedSession();
324         }
325 
326         Session next = getWrappedSession();
327 
328         // If the next session is the real node then we can skip cloning ourselves.
329         // This happens when the wrapper to remove isn't present
330         if (!(next instanceof DelegateSessionWrapper)) {
331             return this;
332         }
333 
334         // We let the next wrapper do deepUnwrap first, if it returns itself then the wrapper isn't in the chain and cloning is not necessary
335         Session deepUnwrappedNext = ((DelegateSessionWrapper) next).deepUnwrap(wrapper);
336         if (deepUnwrappedNext == next) {
337             return this;
338         }
339 
340         try {
341             DelegateSessionWrapper clone = ((DelegateSessionWrapper) this.clone());
342             clone.initClone(deepUnwrappedNext);
343             return clone;
344         } catch (CloneNotSupportedException e) {
345             throw new RuntimeException("Failed to unwrap " + this.getClass().getName() + " due to " + e.getMessage(), e);
346         }
347     }
348 
349     protected void initClone(Session newSession) {
350         setWrappedSession(newSession);
351     }
352 
353     @Override
354     protected Object clone() throws CloneNotSupportedException {
355         // just shallow clone ... keep it that way at least for wrappedSession otherwise deepUnwrap generates zillions of objects unnecessarily
356         return super.clone();
357     }
358 
359     @Override
360     public boolean equals(Object obj) {
361         if (obj == this) {
362             return true;
363         }
364         if (obj == null || !(obj instanceof DelegateSessionWrapper)) {
365             return false;
366         }
367         DelegateSessionWrapper that = (DelegateSessionWrapper) obj;
368         return this.getClass().equals(that.getClass()) && this.wrapped.equals(that.wrapped);
369     }
370 
371 }