1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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
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
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
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
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
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
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 }