1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.script; |
21 |
|
|
22 |
|
import java.util.Collection; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.xwiki.annotation.Annotation; |
30 |
|
import org.xwiki.annotation.AnnotationService; |
31 |
|
import org.xwiki.annotation.AnnotationServiceException; |
32 |
|
import org.xwiki.annotation.rights.AnnotationRightService; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.context.Execution; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.model.reference.EntityReference; |
37 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
38 |
|
import org.xwiki.script.service.ScriptService; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWikiContext; |
41 |
|
import com.xpn.xwiki.XWikiException; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
49 |
|
@Component |
50 |
|
@Named("annotations") |
51 |
|
@Singleton |
|
|
| 22.1% |
Uncovered Elements: 81 (104) |
Complexity: 33 |
Complexity Density: 0.46 |
|
52 |
|
public class AnnotationScriptService implements ScriptService |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@Inject |
58 |
|
private AnnotationService annotationService; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@Inject |
64 |
|
private AnnotationRightService rightsService; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@Inject |
70 |
|
private Execution execution; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private EntityReferenceSerializer<String> serializer; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@param |
82 |
|
@param |
83 |
|
@param |
84 |
|
@param |
85 |
|
@param |
86 |
|
@param |
87 |
|
@return |
88 |
|
|
89 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
90 |
0 |
public boolean addAnnotation(String target, String selection, String selectionContext, int offset, String author,... |
91 |
|
Map<String, Object> metadata) |
92 |
|
{ |
93 |
0 |
if (!this.rightsService.canAddAnnotation(target, getCurrentUser())) { |
94 |
0 |
setAccessExceptionOnContext(); |
95 |
0 |
return false; |
96 |
|
} |
97 |
0 |
try { |
98 |
0 |
this.annotationService.addAnnotation(target, selection, selectionContext, offset, author, metadata); |
99 |
0 |
return true; |
100 |
|
} catch (AnnotationServiceException e) { |
101 |
0 |
setExceptionOnContext(e); |
102 |
0 |
return false; |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@link |
109 |
|
|
110 |
|
@link |
111 |
|
|
112 |
|
@param |
113 |
|
@return |
114 |
|
|
115 |
|
@see |
116 |
|
@see |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
118 |
0 |
public String getAnnotatedHTML(String sourceReference)... |
119 |
|
{ |
120 |
0 |
if (!this.rightsService.canViewAnnotatedTarget(sourceReference, getCurrentUser())) { |
121 |
0 |
setAccessExceptionOnContext(); |
122 |
0 |
return null; |
123 |
|
} |
124 |
0 |
try { |
125 |
0 |
return this.annotationService.getAnnotatedHTML(sourceReference); |
126 |
|
} catch (AnnotationServiceException e) { |
127 |
0 |
setExceptionOnContext(e); |
128 |
0 |
return null; |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
|
142 |
|
@param |
143 |
|
@param |
144 |
|
|
145 |
|
@return |
146 |
|
|
147 |
|
@see |
148 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
149 |
0 |
public String getAnnotatedRenderedContent(String sourceReference, String sourceSyntax, String outputSyntax,... |
150 |
|
Collection<Annotation> annotations) |
151 |
|
{ |
152 |
0 |
if (!this.rightsService.canViewAnnotatedTarget(sourceReference, getCurrentUser())) { |
153 |
0 |
setAccessExceptionOnContext(); |
154 |
0 |
return null; |
155 |
|
} |
156 |
0 |
try { |
157 |
0 |
return this.annotationService.getAnnotatedRenderedContent(sourceReference, sourceSyntax, outputSyntax, |
158 |
|
annotations); |
159 |
|
} catch (AnnotationServiceException e) { |
160 |
0 |
setExceptionOnContext(e); |
161 |
0 |
return null; |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
@param |
169 |
|
@param |
170 |
|
@return |
171 |
|
|
172 |
|
@see |
173 |
|
|
|
|
| 44.4% |
Uncovered Elements: 5 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
174 |
8 |
public Annotation getAnnotation(String target, String id)... |
175 |
|
{ |
176 |
8 |
if (!this.rightsService.canViewAnnotations(target, getCurrentUser())) { |
177 |
0 |
setAccessExceptionOnContext(); |
178 |
0 |
return null; |
179 |
|
} |
180 |
8 |
try { |
181 |
8 |
return this.annotationService.getAnnotation(target, id); |
182 |
|
} catch (AnnotationServiceException e) { |
183 |
0 |
setExceptionOnContext(e); |
184 |
0 |
return null; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
@param |
192 |
|
@param |
193 |
|
@return |
194 |
|
|
195 |
|
@see |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
197 |
8 |
public Annotation getAnnotation(EntityReference reference, String id)... |
198 |
|
{ |
199 |
8 |
String serializedRef = this.serializer.serialize(reference); |
200 |
|
|
201 |
8 |
return getAnnotation(serializedRef, id); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
@param |
208 |
|
@return |
209 |
|
|
210 |
|
@see |
211 |
|
|
|
|
| 44.4% |
Uncovered Elements: 5 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
212 |
1 |
public Collection<Annotation> getAnnotations(String target)... |
213 |
|
{ |
214 |
1 |
if (!this.rightsService.canViewAnnotations(target, getCurrentUser())) { |
215 |
0 |
setAccessExceptionOnContext(); |
216 |
0 |
return null; |
217 |
|
} |
218 |
1 |
try { |
219 |
1 |
return this.annotationService.getAnnotations(target); |
220 |
|
} catch (AnnotationServiceException e) { |
221 |
0 |
setExceptionOnContext(e); |
222 |
0 |
return null; |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@param |
231 |
|
@return |
232 |
|
|
233 |
|
@see |
234 |
|
@see |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
236 |
0 |
public Collection<Annotation> getValidAnnotations(String target)... |
237 |
|
{ |
238 |
0 |
if (!this.rightsService.canViewAnnotations(target, getCurrentUser())) { |
239 |
0 |
setAccessExceptionOnContext(); |
240 |
0 |
return null; |
241 |
|
} |
242 |
0 |
try { |
243 |
0 |
return this.annotationService.getValidAnnotations(target); |
244 |
|
} catch (AnnotationServiceException e) { |
245 |
0 |
setExceptionOnContext(e); |
246 |
0 |
return null; |
247 |
|
} |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@param |
255 |
|
@return |
256 |
|
|
257 |
|
@see |
258 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
259 |
0 |
public boolean removeAnnotation(String target, String annotationID)... |
260 |
|
{ |
261 |
0 |
if (!this.rightsService.canEditAnnotation(annotationID, target, getCurrentUser())) { |
262 |
0 |
setAccessExceptionOnContext(); |
263 |
0 |
return false; |
264 |
|
} |
265 |
0 |
try { |
266 |
0 |
this.annotationService.removeAnnotation(target, annotationID); |
267 |
0 |
return true; |
268 |
|
} catch (AnnotationServiceException e) { |
269 |
0 |
setExceptionOnContext(e); |
270 |
0 |
return false; |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
@param |
279 |
|
@param |
280 |
|
@return |
281 |
|
|
282 |
|
@see |
283 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
284 |
0 |
public boolean updateAnnotation(String target, Annotation annotation)... |
285 |
|
{ |
286 |
0 |
if (!this.rightsService.canEditAnnotation(annotation.getId(), target, getCurrentUser())) { |
287 |
0 |
setAccessExceptionOnContext(); |
288 |
0 |
return false; |
289 |
|
} |
290 |
0 |
try { |
291 |
0 |
this.annotationService.updateAnnotation(target, annotation); |
292 |
0 |
return true; |
293 |
|
} catch (AnnotationServiceException e) { |
294 |
0 |
setExceptionOnContext(e); |
295 |
0 |
return false; |
296 |
|
} |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
@param |
308 |
|
@param |
309 |
|
@param |
310 |
|
@param |
311 |
|
@return |
312 |
|
|
313 |
|
@deprecated@link |
314 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
315 |
0 |
@Deprecated... |
316 |
|
public boolean canEditAnnotation(String annotationId, String wiki, String space, String page) |
317 |
|
{ |
318 |
0 |
return canEditAnnotation(annotationId, new DocumentReference(wiki, space, page)); |
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
@param |
325 |
|
@param |
326 |
|
@return |
327 |
|
|
328 |
|
@since |
329 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
330 |
8 |
public boolean canEditAnnotation(String annotationId, DocumentReference reference)... |
331 |
|
{ |
332 |
8 |
String target = this.serializer.serialize(reference); |
333 |
|
|
334 |
8 |
return this.rightsService.canEditAnnotation(annotationId, target, getCurrentUser()); |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
@param |
343 |
|
@param |
344 |
|
@param |
345 |
|
@return |
346 |
|
@see |
347 |
|
@deprecated@link |
348 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
349 |
0 |
@Deprecated... |
350 |
|
public boolean canAddAnnotation(String wiki, String space, String page) |
351 |
|
{ |
352 |
0 |
return canAddAnnotation(new DocumentReference(wiki, space, page)); |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
@param |
359 |
|
@return |
360 |
|
@see |
361 |
|
@since |
362 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
363 |
4 |
public boolean canAddAnnotation(DocumentReference reference)... |
364 |
|
{ |
365 |
4 |
String target = this.serializer.serialize(reference); |
366 |
|
|
367 |
4 |
return this.rightsService.canAddAnnotation(target, getCurrentUser()); |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
@return |
374 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
375 |
21 |
private String getCurrentUser()... |
376 |
|
{ |
377 |
21 |
return getXWikiContext().getUser(); |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
@return |
384 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
385 |
21 |
private XWikiContext getXWikiContext()... |
386 |
|
{ |
387 |
21 |
return (XWikiContext) this.execution.getContext().getProperty("xwikicontext"); |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
@param |
394 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
395 |
0 |
private void setExceptionOnContext(Exception exception)... |
396 |
|
{ |
397 |
0 |
getXWikiContext().put("lastexception", exception); |
398 |
|
} |
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
403 |
0 |
private void setAccessExceptionOnContext()... |
404 |
|
{ |
405 |
0 |
setExceptionOnContext(new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, |
406 |
|
XWikiException.ERROR_XWIKI_ACCESS_DENIED, "You are not allowed to perform this action")); |
407 |
|
} |
408 |
|
} |