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.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
import java.util.logging.Level; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Singleton; |
33 |
|
import javax.ws.rs.GET; |
34 |
|
import javax.ws.rs.POST; |
35 |
|
import javax.ws.rs.Path; |
36 |
|
import javax.ws.rs.PathParam; |
37 |
|
import javax.ws.rs.WebApplicationException; |
38 |
|
import javax.ws.rs.core.Response; |
39 |
|
import javax.ws.rs.core.Response.Status; |
40 |
|
|
41 |
|
import org.apache.commons.lang.StringUtils; |
42 |
|
import org.restlet.Request; |
43 |
|
import org.restlet.data.Form; |
44 |
|
import org.xwiki.annotation.AnnotationServiceException; |
45 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest; |
46 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationField; |
47 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection; |
48 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationRequest; |
49 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationResponse; |
50 |
|
import org.xwiki.component.annotation.Component; |
51 |
|
import org.xwiki.model.reference.DocumentReference; |
52 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
53 |
|
import org.xwiki.rest.XWikiRestException; |
54 |
|
|
55 |
|
import com.xpn.xwiki.XWikiException; |
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named("org.xwiki.annotation.rest.internal.AnnotationsRESTResource") |
63 |
|
@Path("/wikis/{wikiName}/spaces/{spaceName: .+}/pages/{pageName}/annotations") |
64 |
|
@Singleton |
|
|
| 27.6% |
Uncovered Elements: 42 (58) |
Complexity: 11 |
Complexity Density: 0.23 |
|
65 |
|
public class AnnotationsRESTResource extends AbstractAnnotationRESTResource |
66 |
|
{ |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String ANNOTATION_REQUEST_REQUESTED_FIELD_PARAMETER = "request_field"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private static final String ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX = "filter_"; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private EntityReferenceSerializer<String> referenceSerializer; |
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
@param |
87 |
|
@param |
88 |
|
@return |
89 |
|
|
90 |
|
|
91 |
|
@throws |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 6 |
Complexity Density: 0.21 |
|
93 |
0 |
@GET... |
94 |
|
public Response doGetAnnotatedContent(@PathParam("spaceName") String space, @PathParam("pageName") String page, |
95 |
|
@PathParam("wikiName") String wiki) throws XWikiRestException |
96 |
|
{ |
97 |
0 |
try { |
98 |
0 |
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page); |
99 |
|
|
100 |
|
|
101 |
0 |
updateContext(documentReference); |
102 |
|
|
103 |
0 |
String documentName = referenceSerializer.serialize(documentReference); |
104 |
|
|
105 |
|
|
106 |
0 |
if (!annotationRightService.canViewAnnotatedTarget(documentName, getXWikiUser())) { |
107 |
0 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
0 |
Form form = Request.getCurrent().getResourceRef().getQueryAsForm(); |
116 |
0 |
AnnotationRequest request = new AnnotationRequest(); |
117 |
0 |
AnnotationFieldCollection fields = new AnnotationFieldCollection(); |
118 |
0 |
List<AnnotationField> annotationFields = new ArrayList<AnnotationField>(); |
119 |
0 |
AnnotationRequest.Request requestedFields = new AnnotationRequest.Request(); |
120 |
0 |
for (String name : form.getNames()) { |
121 |
0 |
if (StringUtils.startsWith(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX)) { |
122 |
0 |
for (String value : form.getValuesArray(name)) { |
123 |
0 |
AnnotationField field = new AnnotationField(); |
124 |
0 |
field.setName(StringUtils.substringAfter(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX)); |
125 |
0 |
field.setValue(value); |
126 |
0 |
annotationFields.add(field); |
127 |
|
} |
128 |
0 |
} else if (StringUtils.equals(name, ANNOTATION_REQUEST_REQUESTED_FIELD_PARAMETER)) { |
129 |
0 |
requestedFields.getFields().addAll(Arrays.asList(form.getValuesArray(name))); |
130 |
|
} |
131 |
|
} |
132 |
0 |
request.setRequest(requestedFields); |
133 |
0 |
fields.getFields().addAll(annotationFields); |
134 |
0 |
request.setFilter(fields); |
135 |
|
|
136 |
0 |
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request); |
137 |
|
|
138 |
0 |
return Response.ok(response).expires(new Date()).build(); |
139 |
|
} catch (AnnotationServiceException e) { |
140 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
141 |
0 |
return Response.ok(getErrorResponse(e)).build(); |
142 |
|
} catch (XWikiException e) { |
143 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
144 |
0 |
return Response.ok(getErrorResponse(e)).build(); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
@param |
153 |
|
@param |
154 |
|
@param |
155 |
|
@return |
156 |
|
@throws |
157 |
|
|
|
|
| 62.5% |
Uncovered Elements: 6 (16) |
Complexity: 4 |
Complexity Density: 0.29 |
|
158 |
4 |
@POST... |
159 |
|
public AnnotationResponse doPostAnnotation(@PathParam("wikiName") String wiki, |
160 |
|
@PathParam("spaceName") String space, @PathParam("pageName") String page, AnnotationAddRequest request) |
161 |
|
throws XWikiRestException |
162 |
|
{ |
163 |
4 |
try { |
164 |
4 |
DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page); |
165 |
|
|
166 |
|
|
167 |
4 |
updateContext(documentReference); |
168 |
|
|
169 |
4 |
String documentName = referenceSerializer.serialize(documentReference); |
170 |
|
|
171 |
|
|
172 |
4 |
if (!annotationRightService.canAddAnnotation(documentName, getXWikiUser())) { |
173 |
0 |
throw new WebApplicationException(Status.UNAUTHORIZED); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
4 |
Map<String, Object> annotationMetadata = getMap(request.getAnnotation()); |
178 |
4 |
annotationService.addAnnotation(documentName, request.getSelection(), request.getSelectionContext(), |
179 |
|
request.getSelectionOffset(), getXWikiUser(), annotationMetadata); |
180 |
|
|
181 |
4 |
AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request); |
182 |
4 |
return response; |
183 |
|
} catch (AnnotationServiceException e) { |
184 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
185 |
0 |
return getErrorResponse(e); |
186 |
|
} catch (XWikiException e) { |
187 |
0 |
logger.log(Level.SEVERE, e.getMessage(), e); |
188 |
0 |
return getErrorResponse(e); |
189 |
|
} |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
@param |
196 |
|
@return |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
198 |
4 |
protected Map<String, Object> getMap(AnnotationFieldCollection fields)... |
199 |
|
{ |
200 |
4 |
Map<String, Object> metadataMap = new HashMap<String, Object>(); |
201 |
4 |
for (AnnotationField f : fields.getFields()) { |
202 |
12 |
metadataMap.put(f.getName(), f.getValue()); |
203 |
|
} |
204 |
|
|
205 |
4 |
return metadataMap; |
206 |
|
} |
207 |
|
} |