1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rest; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Locale; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Provider; |
29 |
|
import javax.ws.rs.GET; |
30 |
|
import javax.ws.rs.Path; |
31 |
|
import javax.ws.rs.Produces; |
32 |
|
import javax.ws.rs.WebApplicationException; |
33 |
|
import javax.ws.rs.core.Context; |
34 |
|
import javax.ws.rs.core.Response.Status; |
35 |
|
import javax.ws.rs.core.UriInfo; |
36 |
|
|
37 |
|
import org.slf4j.Logger; |
38 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
39 |
|
import org.xwiki.component.descriptor.ComponentInstantiationStrategy; |
40 |
|
import org.xwiki.component.manager.ComponentManager; |
41 |
|
import org.xwiki.component.phase.Initializable; |
42 |
|
import org.xwiki.component.phase.InitializationException; |
43 |
|
import org.xwiki.localization.LocaleUtils; |
44 |
|
import org.xwiki.model.reference.DocumentReference; |
45 |
|
import org.xwiki.model.reference.SpaceReference; |
46 |
|
import org.xwiki.query.QueryManager; |
47 |
|
import org.xwiki.rest.internal.Utils; |
48 |
|
import org.xwiki.rest.model.jaxb.ObjectFactory; |
49 |
|
|
50 |
|
import com.xpn.xwiki.XWikiContext; |
51 |
|
import com.xpn.xwiki.XWikiException; |
52 |
|
import com.xpn.xwiki.api.Document; |
53 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@version |
60 |
|
|
61 |
|
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP) |
|
|
| 66.7% |
Uncovered Elements: 24 (72) |
Complexity: 24 |
Complexity Density: 0.56 |
|
62 |
|
public class XWikiResource implements XWikiRestComponent, Initializable |
63 |
|
{ |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Context |
69 |
|
protected UriInfo uriInfo; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@deprecated@link |
75 |
|
|
76 |
|
@Deprecated |
77 |
|
protected java.util.logging.Logger logger; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@since |
83 |
|
@since |
84 |
|
|
85 |
|
@Inject |
86 |
|
protected Logger slf4Jlogger; |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
protected ObjectFactory objectFactory; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
@Named("context") |
98 |
|
protected ComponentManager componentManager; |
99 |
|
|
100 |
|
@Inject |
101 |
|
protected Provider<XWikiContext> xcontextProvider; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
@Inject |
107 |
|
protected QueryManager queryManager; |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
112 |
|
protected static class DocumentInfo |
113 |
|
{ |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
private Document document; |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
private boolean created; |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
125 |
667 |
public DocumentInfo(Document document, boolean created)... |
126 |
|
{ |
127 |
667 |
this.document = document; |
128 |
667 |
this.created = created; |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
131 |
667 |
public Document getDocument()... |
132 |
|
{ |
133 |
667 |
return document; |
134 |
|
} |
135 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
136 |
185 |
public boolean isCreated()... |
137 |
|
{ |
138 |
185 |
return created; |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
145 |
1571 |
@Override... |
146 |
|
public void initialize() throws InitializationException |
147 |
|
{ |
148 |
1571 |
logger = java.util.logging.Logger.getLogger(this.getClass().getName()); |
149 |
|
|
150 |
1571 |
objectFactory = new ObjectFactory(); |
151 |
|
|
152 |
1571 |
this.slf4Jlogger.trace("Resource {} initialized. Serving user: '{}'\n", getClass().getName(), |
153 |
|
Utils.getXWikiUser(componentManager)); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
@param |
158 |
|
@return |
159 |
|
@throws |
160 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
161 |
974 |
public List<String> parseSpaceSegments(String spaceSegments) throws XWikiRestException... |
162 |
|
{ |
163 |
|
|
164 |
974 |
List<String> spaces = new ArrayList<>(); |
165 |
|
|
166 |
974 |
int i = 1; |
167 |
974 |
for (String space : spaceSegments.split("/")) { |
168 |
1398 |
if (i % 2 == 0) { |
169 |
|
|
170 |
212 |
if (!"spaces".equals(space)) { |
171 |
0 |
throw new XWikiRestException("Malformed URL: the spaces section is invalid."); |
172 |
|
} |
173 |
|
} else { |
174 |
1186 |
spaces.add(space); |
175 |
|
} |
176 |
1398 |
i++; |
177 |
|
} |
178 |
974 |
if (spaces.isEmpty()) { |
179 |
0 |
throw new XWikiRestException("Malformed URL: the spaces section is empty."); |
180 |
|
} |
181 |
974 |
return spaces; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
@param |
186 |
|
@param |
187 |
|
@return |
188 |
|
@throws |
189 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
190 |
0 |
public SpaceReference getSpaceReference(String spaceSegments, String wikiName) throws XWikiRestException... |
191 |
|
{ |
192 |
0 |
return Utils.getSpaceReference(parseSpaceSegments(spaceSegments), wikiName); |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
@param |
200 |
|
@param |
201 |
|
@param |
202 |
|
@param |
203 |
|
@param |
204 |
|
@param |
205 |
|
@param |
206 |
|
@return |
207 |
|
@throws |
208 |
|
@throws |
209 |
|
@throws |
210 |
|
|
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
212 |
896 |
public DocumentInfo getDocumentInfo(String wikiName, String spaceName, String pageName, String language,... |
213 |
|
String version, boolean failIfDoesntExist, boolean failIfLocked) throws XWikiException, XWikiRestException |
214 |
|
{ |
215 |
896 |
return getDocumentInfo(wikiName, parseSpaceSegments(spaceName), pageName, language, version, failIfDoesntExist, |
216 |
|
failIfLocked); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
@param |
224 |
|
@param |
225 |
|
@param |
226 |
|
@param |
227 |
|
@param |
228 |
|
@param |
229 |
|
@param |
230 |
|
@return |
231 |
|
@throws |
232 |
|
@throws |
233 |
|
|
234 |
|
|
|
|
| 80.6% |
Uncovered Elements: 6 (31) |
Complexity: 13 |
Complexity Density: 0.68 |
|
235 |
918 |
public DocumentInfo getDocumentInfo(String wikiName, List<String> spaces, String pageName, String localeString,... |
236 |
|
String version, boolean failIfDoesntExist, boolean failIfLocked) throws XWikiException |
237 |
|
{ |
238 |
918 |
if ((wikiName == null) || (spaces == null || spaces.isEmpty()) || (pageName == null)) { |
239 |
0 |
throw new IllegalArgumentException( |
240 |
|
String.format("wikiName, spaceName and pageName must all be not null. Current values: (%s:%s.%s)", |
241 |
|
wikiName, spaces, pageName)); |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
918 |
Locale locale; |
248 |
918 |
if (localeString != null) { |
249 |
14 |
try { |
250 |
14 |
locale = LocaleUtils.toLocale(localeString); |
251 |
|
} catch (Exception e) { |
252 |
|
|
253 |
1 |
throw new WebApplicationException(Status.NOT_FOUND); |
254 |
|
} |
255 |
|
} else { |
256 |
904 |
locale = null; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
917 |
DocumentReference reference = new DocumentReference(wikiName, spaces, pageName, locale); |
261 |
|
|
262 |
|
|
263 |
917 |
Document doc = Utils.getXWikiApi(componentManager).getDocument(reference); |
264 |
|
|
265 |
|
|
266 |
917 |
if (doc == null) { |
267 |
0 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
268 |
|
} |
269 |
|
|
270 |
917 |
if (failIfDoesntExist && doc.isNew()) { |
271 |
250 |
throw new WebApplicationException(Status.NOT_FOUND); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
667 |
if (version != null) { |
276 |
21 |
doc = doc.getDocumentRevision(version); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
667 |
if (failIfLocked && doc.getLocked()) { |
281 |
0 |
throw new WebApplicationException(Status.PRECONDITION_FAILED); |
282 |
|
} |
283 |
|
|
284 |
667 |
return new DocumentInfo(doc, doc.isNew()); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
@return |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
293 |
0 |
@GET... |
294 |
|
@Produces("uritemplate") |
295 |
|
public String getUriTemplate() |
296 |
|
{ |
297 |
0 |
if (this.getClass().getAnnotation(Path.class) != null) { |
298 |
0 |
return this.getClass().getAnnotation(Path.class).value(); |
299 |
|
} |
300 |
|
|
301 |
0 |
Class<?>[] interfaces = this.getClass().getInterfaces(); |
302 |
|
|
303 |
0 |
for (Class<?> i : interfaces) { |
304 |
0 |
if (i.getAnnotation(Path.class) != null) { |
305 |
0 |
return i.getAnnotation(Path.class).value(); |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
0 |
return null; |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
@return |
316 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
317 |
403 |
protected XWikiContext getXWikiContext()... |
318 |
|
{ |
319 |
403 |
return this.xcontextProvider.get(); |
320 |
|
} |
321 |
|
} |