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 package org.treetank.service.jaxrx.util;
32
33 import java.io.IOException;
34 import java.io.OutputStream;
35
36 import javax.ws.rs.WebApplicationException;
37 import javax.ws.rs.core.Response;
38
39 import org.treetank.access.NodeReadTrx;
40 import org.treetank.access.conf.SessionConfiguration;
41 import org.treetank.access.conf.StandardSettings;
42 import org.treetank.api.IStorage;
43 import org.treetank.api.INodeReadTrx;
44 import org.treetank.api.ISession;
45 import org.treetank.axis.AbsAxis;
46 import org.treetank.exception.TTException;
47 import org.treetank.service.xml.xpath.XPathAxis;
48
49
50
51
52
53
54
55 public class RestXPathProcessor {
56
57
58
59
60 private static transient String beginResult = "<jaxrx:result xmlns:jaxrx=\"http://jaxrx.org/\">";
61
62
63
64
65 private static transient String endResult = "</jaxrx:result>";
66
67
68
69
70 private final IStorage mDatabase;
71
72
73
74
75
76
77
78
79 public RestXPathProcessor(final IStorage pDatabase) {
80 mDatabase = pDatabase;
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public OutputStream getXpathResource(final String resourceName, final String xpath, final boolean nodeid,
105 final Long revision, final OutputStream output, final boolean wrapResult) throws IOException,
106 TTException {
107
108
109 String qQuery = xpath;
110 if (xpath.charAt(0) == '/')
111 qQuery = ".".concat(xpath);
112 if (mDatabase.existsResource(resourceName)) {
113 if (wrapResult) {
114 output.write(beginResult.getBytes());
115 doXPathRes(resourceName, revision, output, nodeid, qQuery);
116 output.write(endResult.getBytes());
117 } else {
118 doXPathRes(resourceName, revision, output, nodeid, qQuery);
119 }
120
121 } else {
122 throw new WebApplicationException(Response.Status.NOT_FOUND);
123 }
124 return output;
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public void getXpathResource(final String resourceName, final long rId, final String query,
151 final boolean doNodeId, final Long doRevision, final OutputStream output, final boolean doWrap)
152 throws TTException {
153
154
155 String qQuery = query;
156 if (query.charAt(0) == '/')
157 qQuery = ".".concat(query);
158
159 ISession session = null;
160 INodeReadTrx rtx = null;
161 try {
162 if (mDatabase.existsResource(resourceName)) {
163 session = mDatabase.getSession(new SessionConfiguration(resourceName, StandardSettings.KEY));
164
165
166 if (doRevision == null) {
167 rtx = new NodeReadTrx(session.beginBucketRtx(session.getMostRecentVersion()));
168 } else {
169 rtx = new NodeReadTrx(session.beginBucketRtx(doRevision));
170 }
171
172 final boolean exist = rtx.moveTo(rId);
173 if (exist) {
174 final AbsAxis axis = new XPathAxis(rtx, qQuery);
175 if (doWrap) {
176 output.write(beginResult.getBytes());
177 for (final long key : axis) {
178 WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision)
179 .call();
180 }
181
182 output.write(endResult.getBytes());
183 } else {
184 for (final long key : axis) {
185 WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision)
186 .call();
187 }
188
189 }
190 } else {
191 throw new WebApplicationException(404);
192 }
193 }
194
195 } catch (final Exception globExcep) {
196 throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
197 }
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 private void doXPathRes(final String resource, final Long revision, final OutputStream output,
217 final boolean nodeid, final String xpath) throws TTException {
218
219 ISession session = null;
220 INodeReadTrx rtx = null;
221 try {
222 if (mDatabase.existsResource(resource)) {
223 session = mDatabase.getSession(new SessionConfiguration(resource, StandardSettings.KEY));
224
225 if (revision == null) {
226 rtx = new NodeReadTrx(session.beginBucketRtx(session.getMostRecentVersion()));
227 } else {
228 rtx = new NodeReadTrx(session.beginBucketRtx(revision));
229 }
230
231 final AbsAxis axis = new XPathAxis(rtx, xpath);
232 for (final long key : axis) {
233 WorkerHelper.serializeXML(session, output, false, nodeid, key, revision).call();
234 }
235 }
236 } catch (final Exception globExcep) {
237 throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
238 } finally {
239 WorkerHelper.closeRTX(rtx, session);
240
241 }
242 }
243 }