1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rest.internal; |
21 |
|
|
22 |
|
import java.net.URI; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.ws.rs.core.UriBuilder; |
28 |
|
|
29 |
|
import org.apache.commons.httpclient.URIException; |
30 |
|
import org.apache.commons.httpclient.util.URIUtil; |
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.xwiki.component.manager.ComponentLookupException; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.EntityType; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReference; |
39 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
40 |
|
import org.xwiki.model.reference.SpaceReference; |
41 |
|
import org.xwiki.model.reference.SpaceReferenceResolver; |
42 |
|
import org.xwiki.model.reference.WikiReference; |
43 |
|
import org.xwiki.query.QueryFilter; |
44 |
|
import org.xwiki.query.internal.NoOpQueryFilter; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWikiContext; |
47 |
|
import com.xpn.xwiki.XWikiException; |
48 |
|
import com.xpn.xwiki.api.Document; |
49 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
50 |
|
import com.xpn.xwiki.objects.BaseObject; |
51 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
52 |
|
import com.xpn.xwiki.user.api.XWikiUser; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@version |
58 |
|
|
|
|
| 89.3% |
Uncovered Elements: 16 (149) |
Complexity: 41 |
Complexity Density: 0.42 |
|
59 |
|
public class Utils |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@since |
65 |
|
@version |
66 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
67 |
|
public class EncodedElement |
68 |
|
{ |
69 |
|
private String encodedElement; |
70 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
71 |
0 |
public EncodedElement(String encodedElement)... |
72 |
|
{ |
73 |
0 |
this.encodedElement = encodedElement; |
74 |
|
} |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
0 |
@Override... |
77 |
|
public String toString() |
78 |
|
{ |
79 |
0 |
return this.encodedElement; |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@param |
87 |
|
@param |
88 |
|
@param |
89 |
|
@return |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
91 |
284 |
public static String getPageId(String wikiName, List<String> spaceName, String pageName)... |
92 |
|
{ |
93 |
284 |
EntityReferenceSerializer<String> serializer = |
94 |
|
com.xpn.xwiki.web.Utils.getComponent(EntityReferenceSerializer.TYPE_STRING); |
95 |
284 |
return serializer.serialize(new DocumentReference(wikiName, spaceName, pageName)); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
@param |
100 |
|
@param |
101 |
|
@return |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
103 |
155 |
public static SpaceReference getSpaceReference(List<String> spaces, String wikiName)... |
104 |
|
{ |
105 |
155 |
EntityReference parentReference = new WikiReference(wikiName); |
106 |
155 |
SpaceReference spaceReference = null; |
107 |
|
|
108 |
155 |
for (String space : spaces) { |
109 |
165 |
spaceReference = new SpaceReference(space, parentReference); |
110 |
165 |
parentReference = spaceReference; |
111 |
|
} |
112 |
|
|
113 |
155 |
return spaceReference; |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
116 |
73 |
public static String getLocalSpaceId(List<String> spaces)... |
117 |
|
{ |
118 |
73 |
EntityReferenceSerializer<String> serializer = |
119 |
|
com.xpn.xwiki.web.Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local"); |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
73 |
return serializer.serialize(getSpaceReference(spaces, "whatever")); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
@param |
129 |
|
@return |
130 |
|
@throws |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
132 |
82 |
public static String getSpaceId(String wikiName, List<String> spaces)... |
133 |
|
{ |
134 |
82 |
EntityReferenceSerializer<String> defaultEntityReferenceSerializer = |
135 |
|
com.xpn.xwiki.web.Utils.getComponent(EntityReferenceSerializer.TYPE_STRING); |
136 |
82 |
SpaceReference spaceReference = getSpaceReference(spaces, wikiName); |
137 |
82 |
return defaultEntityReferenceSerializer.serialize(spaceReference); |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
140 |
2291 |
public static SpaceReference resolveLocalSpaceId(String spaceId, String wikiName)... |
141 |
|
{ |
142 |
2291 |
SpaceReferenceResolver<String> resolver = |
143 |
|
com.xpn.xwiki.web.Utils.getComponent(SpaceReferenceResolver.TYPE_STRING); |
144 |
2291 |
return resolver.resolve(spaceId, new WikiReference(wikiName)); |
145 |
|
} |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
2291 |
public static List<String> getSpacesFromSpaceId(String spaceId)... |
148 |
|
{ |
149 |
2291 |
return getSpacesHierarchy(resolveLocalSpaceId(spaceId, "whatever")); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
152 |
2296 |
public static List<String> getSpacesHierarchy(SpaceReference spaceReference) ... |
153 |
|
{ |
154 |
2296 |
List<String> spaces = new ArrayList<>(); |
155 |
4810 |
for(EntityReference ref = spaceReference; ref != null && ref.getType() == EntityType.SPACE; |
156 |
|
ref = ref.getParent()) { |
157 |
2514 |
spaces.add(ref.getName()); |
158 |
|
} |
159 |
2296 |
Collections.reverse(spaces); |
160 |
2296 |
return spaces; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@param |
168 |
|
@param |
169 |
|
@return |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
171 |
9 |
public static String getPageFullName(String wikiName, List<String> spaces, String pageName)... |
172 |
|
{ |
173 |
9 |
XWikiDocument xwikiDocument = new XWikiDocument(new DocumentReference(wikiName, spaces, pageName)); |
174 |
|
|
175 |
9 |
Document document = new Document(xwikiDocument, null); |
176 |
|
|
177 |
9 |
return document.getFullName(); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
@param |
184 |
|
@param |
185 |
|
@param |
186 |
|
@param |
187 |
|
@param |
188 |
|
@return |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
190 |
1 |
public static String getObjectId(String wikiName, List<String> spaces, String pageName, String className,... |
191 |
|
int objectNumber) |
192 |
|
{ |
193 |
1 |
XWikiDocument xwikiDocument = new XWikiDocument(new DocumentReference(wikiName, spaces, pageName)); |
194 |
|
|
195 |
1 |
Document document = new Document(xwikiDocument, null); |
196 |
|
|
197 |
1 |
return String.format("%s:%s[%d]", document.getPrefixedFullName(), className, objectNumber); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@return |
204 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
205 |
3 |
public static String getPageId(String wikiName, String pageFullName)... |
206 |
|
{ |
207 |
3 |
DocumentReferenceResolver<String> defaultDocumentReferenceResolver = |
208 |
|
com.xpn.xwiki.web.Utils.getComponent(DocumentReferenceResolver.TYPE_STRING); |
209 |
|
|
210 |
3 |
DocumentReference documentReference = |
211 |
|
defaultDocumentReferenceResolver.resolve(pageFullName, new WikiReference(wikiName)); |
212 |
3 |
XWikiDocument xwikiDocument = new XWikiDocument(documentReference); |
213 |
|
|
214 |
3 |
Document document = new Document(xwikiDocument, null); |
215 |
|
|
216 |
3 |
return document.getPrefixedFullName(); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
@param |
223 |
|
@param |
224 |
|
@return |
225 |
|
@throws |
226 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
227 |
134 |
public static Document getParentDocument(Document doc, com.xpn.xwiki.api.XWiki xwikiApi) throws XWikiException... |
228 |
|
{ |
229 |
134 |
if (StringUtils.isEmpty(doc.getParent())) { |
230 |
124 |
return null; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
10 |
String parentName = doc.getParent(); |
237 |
10 |
if (!parentName.contains(".")) { |
238 |
0 |
parentName = doc.getSpace() + "." + parentName; |
239 |
|
} |
240 |
10 |
return xwikiApi.getDocument(parentName); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
@param |
247 |
|
@return |
248 |
|
@throws |
249 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
250 |
11458 |
public static XWikiContext getXWikiContext(ComponentManager componentManager)... |
251 |
|
{ |
252 |
11458 |
Execution execution; |
253 |
11458 |
XWikiContext xwikiContext; |
254 |
11458 |
try { |
255 |
11458 |
execution = componentManager.getInstance(Execution.class); |
256 |
11458 |
xwikiContext = (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
257 |
11458 |
return xwikiContext; |
258 |
|
} catch (Exception e) { |
259 |
0 |
throw new RuntimeException("Unable to get XWiki context", e); |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
@param |
267 |
|
@return |
268 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
269 |
4014 |
public static com.xpn.xwiki.XWiki getXWiki(ComponentManager componentManager)... |
270 |
|
{ |
271 |
4014 |
return getXWikiContext(componentManager).getWiki(); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
@param |
278 |
|
@return |
279 |
|
@throws |
280 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
281 |
2045 |
public static com.xpn.xwiki.api.XWiki getXWikiApi(ComponentManager componentManager)... |
282 |
|
{ |
283 |
2045 |
return new com.xpn.xwiki.api.XWiki(getXWiki(componentManager), getXWikiContext(componentManager)); |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
@param |
290 |
|
@return |
291 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
292 |
1703 |
public static String getXWikiUser(ComponentManager componentManager)... |
293 |
|
{ |
294 |
1703 |
XWikiUser user = getXWikiContext(componentManager).getXWikiUser(); |
295 |
1703 |
if (user == null) { |
296 |
840 |
return XWikiRightService.GUEST_USER_FULLNAME; |
297 |
|
} |
298 |
|
|
299 |
863 |
return user.getUser(); |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
@param |
306 |
|
@return |
307 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
308 |
0 |
public static String getAuthorName(DocumentReference authorReference, ComponentManager componentManager)... |
309 |
|
{ |
310 |
0 |
return getXWikiContext(componentManager).getWiki().getPlainUserName(authorReference, |
311 |
|
getXWikiContext(componentManager)); |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
@param |
318 |
|
@param |
319 |
|
@param |
320 |
|
@return |
321 |
|
@throws |
322 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
323 |
0 |
public static BaseObject getBaseObject(Document doc, String className, int objectNumber,... |
324 |
|
ComponentManager componentManager) throws XWikiException |
325 |
|
{ |
326 |
0 |
XWikiDocument xwikiDocument = |
327 |
|
Utils.getXWiki(componentManager).getDocument(doc.getPrefixedFullName(), |
328 |
|
Utils.getXWikiContext(componentManager)); |
329 |
|
|
330 |
0 |
return xwikiDocument.getObject(className, objectNumber); |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
@link |
338 |
|
|
339 |
|
|
340 |
|
@param |
341 |
|
@param |
342 |
|
@param |
343 |
|
@return |
344 |
|
|
|
|
| 77.4% |
Uncovered Elements: 7 (31) |
Complexity: 9 |
Complexity Density: 0.47 |
|
345 |
14603 |
public static URI createURI(URI baseURI, java.lang.Class< ? > resourceClass, java.lang.Object... pathElements)... |
346 |
|
{ |
347 |
14603 |
UriBuilder uriBuilder = UriBuilder.fromUri(baseURI).path(resourceClass); |
348 |
|
|
349 |
14603 |
List<String> pathVariableNames = null; |
350 |
14603 |
if (pathElements.length > 0) { |
351 |
|
|
352 |
|
|
353 |
|
|
354 |
13815 |
pathVariableNames = getVariableNamesFromPathTemplate(uriBuilder.toString()); |
355 |
|
} |
356 |
|
|
357 |
14603 |
Object[] encodedPathElements = new String[pathElements.length]; |
358 |
52083 |
for (int i = 0; i < pathElements.length; i++) { |
359 |
37480 |
Object pathElement = pathElements[i]; |
360 |
37480 |
if (pathElement != null) { |
361 |
37480 |
try { |
362 |
|
|
363 |
37480 |
if (i < pathVariableNames.size() && "spaceName".equals(pathVariableNames.get(i))) { |
364 |
3469 |
if (!(pathElement instanceof List)) { |
365 |
0 |
throw new RuntimeException("The 'spaceName' parameter must be a list!"); |
366 |
|
} |
367 |
3469 |
encodedPathElements[i] = generateEncodedSpacesURISegment((List) pathElements[i]); |
368 |
34011 |
} else if (pathElement instanceof EncodedElement) { |
369 |
0 |
encodedPathElements[i] = pathElement.toString(); |
370 |
|
} else { |
371 |
34011 |
encodedPathElements[i] = URIUtil.encodePath(pathElement.toString()); |
372 |
|
} |
373 |
|
} catch (URIException e) { |
374 |
0 |
throw new RuntimeException("Failed to encode path element: " + pathElements[i], e); |
375 |
|
} |
376 |
|
} else { |
377 |
0 |
encodedPathElements[i] = null; |
378 |
|
} |
379 |
|
} |
380 |
14603 |
return uriBuilder.buildFromEncoded(encodedPathElements); |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
|
|
385 |
|
|
386 |
|
@param |
387 |
|
@return |
388 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 5 |
Complexity Density: 0.36 |
|
389 |
13815 |
private static List<String> getVariableNamesFromPathTemplate(String pathTemplate)... |
390 |
|
{ |
391 |
13815 |
List<String> variables = new ArrayList<>(); |
392 |
|
|
393 |
13815 |
boolean inVariable = false; |
394 |
13815 |
StringBuilder varName = new StringBuilder(); |
395 |
|
|
396 |
|
|
397 |
1222393 |
for (int i = 0; i < pathTemplate.length(); ++i) { |
398 |
1208578 |
char c = pathTemplate.charAt(i); |
399 |
1208578 |
if (inVariable) { |
400 |
382364 |
if (c == '}') { |
401 |
37478 |
variables.add(varName.toString()); |
402 |
|
|
403 |
37478 |
varName.delete(0, varName.length()); |
404 |
37478 |
inVariable = false; |
405 |
|
} else { |
406 |
344886 |
varName.append(c); |
407 |
|
} |
408 |
826214 |
} else if (c == '{') { |
409 |
37478 |
inVariable = true; |
410 |
|
} |
411 |
|
} |
412 |
|
|
413 |
13815 |
return variables; |
414 |
|
} |
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
@param |
425 |
|
@return |
426 |
|
@throws |
427 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
428 |
3469 |
private static String generateEncodedSpacesURISegment(List<Object> spaces) throws URIException... |
429 |
|
{ |
430 |
3469 |
StringBuilder spaceSegment = new StringBuilder(); |
431 |
3469 |
for (Object space : spaces) { |
432 |
3992 |
if (spaceSegment.length() > 0) { |
433 |
523 |
spaceSegment.append("/spaces/"); |
434 |
|
} |
435 |
3992 |
spaceSegment.append(URIUtil.encodePath(space.toString())); |
436 |
|
} |
437 |
3469 |
return spaceSegment.toString(); |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
|
@param |
442 |
|
@return |
443 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
444 |
7 |
public static QueryFilter getHiddenQueryFilter(ComponentManager componentManager)... |
445 |
|
{ |
446 |
7 |
QueryFilter filter; |
447 |
7 |
try { |
448 |
7 |
filter = componentManager.getInstance(QueryFilter.class, "hidden"); |
449 |
|
} catch (ComponentLookupException e) { |
450 |
|
|
451 |
0 |
filter = new NoOpQueryFilter(); |
452 |
|
} |
453 |
7 |
return filter; |
454 |
|
} |
455 |
|
} |