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.cms.core.version;
35  
36  import info.magnolia.jcr.decoration.ContentDecoratorSessionWrapper;
37  import info.magnolia.jcr.util.NodeTypes;
38  import info.magnolia.jcr.util.NodeUtil;
39  
40  import java.io.IOException;
41  import java.io.OutputStream;
42  import java.util.regex.Pattern;
43  
44  import javax.jcr.Item;
45  import javax.jcr.ItemExistsException;
46  import javax.jcr.Node;
47  import javax.jcr.PathNotFoundException;
48  import javax.jcr.RepositoryException;
49  import javax.jcr.Session;
50  import javax.jcr.lock.LockException;
51  import javax.jcr.nodetype.ConstraintViolationException;
52  import javax.jcr.version.VersionException;
53  
54  import org.apache.jackrabbit.commons.xml.DocumentViewExporter;
55  import org.apache.jackrabbit.commons.xml.Exporter;
56  import org.apache.jackrabbit.commons.xml.SystemViewExporter;
57  import org.apache.jackrabbit.commons.xml.ToXmlContentHandler;
58  import org.xml.sax.ContentHandler;
59  import org.xml.sax.SAXException;
60  
61  /**
62   * Session wrapper enabling Magnolia specific way of handling versioning on all child nodes. Wrapper will also ensure that version information are not exposed via export.
63   */
64  public class MgnlVersioningSession extends ContentDecoratorSessionWrapper<MgnlVersioningContentDecorator> {
65  
66      private static final Pattern TEMPORARY_NODE = Pattern.compile("^tmp_\\w{12}$");
67  
68      public MgnlVersioningSession(Session wrapped, MgnlVersioningContentDecorator contentDecorator) {
69          super(wrapped, contentDecorator);
70      }
71  
72      /**
73       * @deprecated since 5.5 - use {@link #MgnlVersioningSession(Session, MgnlVersioningContentDecorator)} instead.
74       */
75      @Deprecated
76      public MgnlVersioningSession(Session wrapped) {
77          this(wrapped, new MgnlVersioningContentDecorator());
78      }
79  
80      @Override
81      public void exportSystemView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
82          // customize exporter for root path to skip jcr:system
83          if ("/".equals(absPath)) {
84              export(absPath, new SystemViewExporter(getWrappedSession(), contentHandler, !noRecurse, !skipBinary) {
85  
86                  @Override
87                  protected void exportNode(String uri, String local, Node node) throws RepositoryException, SAXException {
88                      if ("jcr:system".equals(node.getName())) {
89                          // skip version history
90                          return;
91                      }
92                      super.exportNode(uri, local, node);
93                  }
94              });
95          } else {
96              getWrappedSession().exportSystemView(absPath, contentHandler, skipBinary, noRecurse);
97          }
98      }
99  
100     @Override
101     public void exportSystemView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
102         // for root path, create xml content handler and call other export method
103         if ("/".equals(absPath)) {
104             try {
105                 this.exportSystemView(absPath, new ToXmlContentHandler(out), skipBinary, noRecurse);
106             } catch (SAXException e) {
107                 Exception root = e.getException();
108                 if (root instanceof RepositoryException) {
109                     throw (RepositoryException) root;
110                 } else if (root instanceof IOException) {
111                     throw (IOException) root;
112                 } else {
113                     throw new RepositoryException("Error serializing system view XML", e);
114                 }
115             }
116         } else {
117             getWrappedSession().exportSystemView(absPath, out, skipBinary, noRecurse);
118         }
119     }
120 
121     @Override
122     public void exportDocumentView(String absPath, ContentHandler contentHandler, boolean skipBinary, boolean noRecurse) throws PathNotFoundException, SAXException, RepositoryException {
123         // customize exporter for root path to skip jcr:system
124         if ("/".equals(absPath)) {
125             export(absPath, new DocumentViewExporter(getWrappedSession(), contentHandler, !noRecurse, !skipBinary) {
126 
127                 @Override
128                 protected void exportNode(String uri, String local, Node node) throws RepositoryException, SAXException {
129                     if ("jcr:system".equals(node.getName())) {
130                         // skip version history
131                         return;
132                     }
133                     super.exportNode(uri, local, node);
134                 }
135             });
136         } else {
137             getWrappedSession().exportDocumentView(absPath, contentHandler, skipBinary, noRecurse);
138         }
139     }
140 
141     @Override
142     public void exportDocumentView(String absPath, OutputStream out, boolean skipBinary, boolean noRecurse) throws IOException, PathNotFoundException, RepositoryException {
143         // for root path, create xml content handler and call other export method
144         if ("/".equals(absPath)) {
145             try {
146                 this.exportDocumentView(absPath, new ToXmlContentHandler(out), skipBinary, noRecurse);
147             } catch (SAXException e) {
148                 Exception root = e.getException();
149                 if (root instanceof RepositoryException) {
150                     throw (RepositoryException) root;
151                 } else if (root instanceof IOException) {
152                     throw (IOException) root;
153                 } else {
154                     throw new RepositoryException("Error serializing system view XML", e);
155                 }
156             }
157         } else {
158             getWrappedSession().exportDocumentView(absPath, out, skipBinary, noRecurse);
159         }
160     }
161 
162     private void export(String path, Exporter exporter) throws PathNotFoundException, SAXException, RepositoryException {
163         Item item = getItem(path);
164         if (item.isNode()) {
165             exporter.export((Node) item);
166         } else {
167             throw new PathNotFoundException("XML export is not defined for properties: " + path);
168         }
169     }
170 
171     @Override
172     public void move(String srcAbsPath, String destAbsPath) throws PathNotFoundException, VersionException, ConstraintViolationException, LockException, ItemExistsException, RepositoryException {
173         Node node = getNode(srcAbsPath);
174         // we want to remove mixin when copying node via NodeUtil
175         if (node.isNew() && TEMPORARY_NODE.matcher(srcAbsPath).matches() && NodeUtil.hasMixin(node, NodeTypes.HasVersion.NAME)) {
176             node.removeMixin(NodeTypes.HasVersion.NAME);
177         }
178         getWrappedSession().move(srcAbsPath, destAbsPath);
179     }
180 }