1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.rest.internal; |
21 |
|
|
22 |
|
import java.util.logging.Level; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
import javax.inject.Singleton; |
27 |
|
import javax.ws.rs.DELETE; |
28 |
|
import javax.ws.rs.PUT; |
29 |
|
import javax.ws.rs.Path; |
30 |
|
import javax.ws.rs.PathParam; |
31 |
|
import javax.ws.rs.WebApplicationException; |
32 |
|
import javax.ws.rs.core.Response.Status; |
33 |
|
|
34 |
|
import org.xwiki.annotation.Annotation; |
35 |
|
import org.xwiki.annotation.AnnotationServiceException; |
36 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationField; |
37 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationRequest; |
38 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationResponse; |
39 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationUpdateRequest; |
40 |
|
import org.xwiki.component.annotation.Component; |
41 |
|
import org.xwiki.model.reference.DocumentReference; |
42 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
43 |
|
import org.xwiki.rest.XWikiRestException; |
44 |
|
|
45 |
|
import com.xpn.xwiki.XWikiException; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
53 |
|
@Component |
54 |
|
@Named("org.xwiki.annotation.rest.internal.SingleAnnotationRESTResource") |
55 |
|
@Path("/wikis/{wikiName}/spaces/{spaceName: .+}/pages/{pageName}/annotation/{id}") |
56 |
|
@Singleton |
|
|
| 27.8% |
Uncovered Elements: 26 (36) |
Complexity: 8 |
Complexity Density: 0.27 |
|
57 |
|
public class SingleAnnotationRESTResource extends AbstractAnnotationRESTResource |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private EntityReferenceSerializer<String> referenceSerializer; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@param |
69 |
|
@param |
70 |
|
@param |
71 |
|
@param |
72 |
|
@param |
73 |
|
|
74 |
|
@return |
75 |
|
@throws |
76 |
|
|
|
|
| 60% |
Uncovered Elements: 6 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
77 |
4 |
@DELETE... |
78 |
|
public AnnotationResponse doDelete(@PathParam("spaceName") String space, @PathParam("pageName") String page, |
79 |
|
@PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationRequest request) |
80 |
|
throws XWikiRestException |
81 |
|
{ |
82 |
4 |
try { |
83 |
4 |
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page); |
84 |
|
|
85 |
|
|
86 |
4 |
updateContext(documentReference); |
87 |
|
|
88 |
4 |
String documentName = referenceSerializer.serialize(documentReference); |
89 |
|
|
90 |
|
|
91 |
4 |
if (!annotationRightService.canEditAnnotation(id, documentName, getXWikiUser())) { |
92 |
0 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
93 |
|
} |
94 |
|
|
95 |
4 |
annotationService.removeAnnotation(documentName, id); |
96 |
|
|
97 |
4 |
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request); |
98 |
4 |
return response; |
99 |
|
} catch (XWikiException e) { |
100 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
101 |
0 |
return getErrorResponse(e); |
102 |
|
} catch (AnnotationServiceException e) { |
103 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
104 |
0 |
return getErrorResponse(e); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
@param |
112 |
|
@param |
113 |
|
@param |
114 |
|
@param |
115 |
|
@param |
116 |
|
@return |
117 |
|
@throws |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.24 |
|
119 |
0 |
@PUT... |
120 |
|
public AnnotationResponse doUpdate(@PathParam("spaceName") String space, @PathParam("pageName") String page, |
121 |
|
@PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationUpdateRequest updateRequest) |
122 |
|
throws XWikiRestException |
123 |
|
{ |
124 |
0 |
try { |
125 |
0 |
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page); |
126 |
|
|
127 |
|
|
128 |
0 |
updateContext(documentReference); |
129 |
|
|
130 |
0 |
String documentName = referenceSerializer.serialize(documentReference); |
131 |
|
|
132 |
|
|
133 |
0 |
if (!annotationRightService.canEditAnnotation(id, documentName, getXWikiUser())) { |
134 |
0 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
0 |
Annotation newAnnotation = new Annotation(id); |
139 |
|
|
140 |
0 |
for (AnnotationField field : updateRequest.getAnnotation().getFields()) { |
141 |
0 |
newAnnotation.set(field.getName(), field.getValue()); |
142 |
|
} |
143 |
|
|
144 |
0 |
newAnnotation.setAuthor(getXWikiUser()); |
145 |
|
|
146 |
0 |
annotationService.updateAnnotation(documentName, newAnnotation); |
147 |
|
|
148 |
0 |
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, updateRequest); |
149 |
0 |
return response; |
150 |
|
} catch (XWikiException e) { |
151 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
152 |
0 |
return getErrorResponse(e); |
153 |
|
} catch (AnnotationServiceException e) { |
154 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
155 |
0 |
return getErrorResponse(e); |
156 |
|
} |
157 |
|
} |
158 |
|
} |