1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.annotation.rest.internal; |
22 |
|
|
23 |
|
import java.io.PrintWriter; |
24 |
|
import java.io.StringWriter; |
25 |
|
import java.util.ArrayList; |
26 |
|
import java.util.Collection; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.logging.Level; |
31 |
|
|
32 |
|
import javax.inject.Inject; |
33 |
|
|
34 |
|
import org.xwiki.annotation.Annotation; |
35 |
|
import org.xwiki.annotation.AnnotationService; |
36 |
|
import org.xwiki.annotation.AnnotationServiceException; |
37 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotatedContent; |
38 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationField; |
39 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationRequest; |
40 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationResponse; |
41 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationStub; |
42 |
|
import org.xwiki.annotation.rest.model.jaxb.ObjectFactory; |
43 |
|
import org.xwiki.annotation.rights.AnnotationRightService; |
44 |
|
import org.xwiki.context.Execution; |
45 |
|
import org.xwiki.model.reference.DocumentReference; |
46 |
|
import org.xwiki.rest.XWikiResource; |
47 |
|
|
48 |
|
import com.xpn.xwiki.XWiki; |
49 |
|
import com.xpn.xwiki.XWikiContext; |
50 |
|
import com.xpn.xwiki.XWikiException; |
51 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
52 |
|
import com.xpn.xwiki.web.XWikiURLFactory; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
@version |
58 |
|
@since |
59 |
|
|
|
|
| 58.5% |
Uncovered Elements: 49 (118) |
Complexity: 18 |
Complexity Density: 0.19 |
|
60 |
|
public abstract class AbstractAnnotationRESTResource extends XWikiResource |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
protected static final String DEFAULT_ACTION = "view"; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@Inject |
72 |
|
protected AnnotationService annotationService; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
protected AnnotationRightService annotationRightService; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
protected Execution execution; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@param |
91 |
|
@param |
92 |
|
@return |
93 |
|
@throws |
94 |
|
@throws |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
96 |
8 |
protected AnnotationResponse getSuccessResponseWithAnnotatedContent(String documentName, AnnotationRequest request)... |
97 |
|
throws AnnotationServiceException, XWikiException |
98 |
|
{ |
99 |
8 |
ObjectFactory factory = new ObjectFactory(); |
100 |
8 |
AnnotationResponse response = factory.createAnnotationResponse(); |
101 |
|
|
102 |
|
|
103 |
8 |
Collection<Annotation> annotations = annotationService.getAnnotations(documentName); |
104 |
|
|
105 |
8 |
Collection<Annotation> filteredAnnotations = filterAnnotations(annotations, request); |
106 |
|
|
107 |
8 |
String renderedHTML = renderDocumentWithAnnotations(documentName, null, DEFAULT_ACTION, filteredAnnotations); |
108 |
|
|
109 |
8 |
AnnotatedContent annotatedContentResponse = factory.createAnnotatedContent(); |
110 |
8 |
annotatedContentResponse.getAnnotations().addAll( |
111 |
|
prepareAnnotationStubsSet(filteredAnnotations, request.getRequest().getFields())); |
112 |
8 |
annotatedContentResponse.setContent(renderedHTML); |
113 |
|
|
114 |
8 |
response.setAnnotatedContent(annotatedContentResponse); |
115 |
8 |
response.setResponseCode(0); |
116 |
|
|
117 |
8 |
return response; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
@link |
122 |
|
|
123 |
|
|
124 |
|
@param |
125 |
|
@param |
126 |
|
@return |
127 |
|
|
128 |
|
|
|
|
| 56.2% |
Uncovered Elements: 7 (16) |
Complexity: 2 |
Complexity Density: 0.14 |
|
129 |
8 |
private Collection<AnnotationStub> prepareAnnotationStubsSet(Collection<Annotation> annotations,... |
130 |
|
List<String> requestedFields) |
131 |
|
{ |
132 |
8 |
ObjectFactory factory = new ObjectFactory(); |
133 |
8 |
List<AnnotationStub> set = new ArrayList<AnnotationStub>(); |
134 |
8 |
for (Annotation xwikiAnnotation : annotations) { |
135 |
16 |
AnnotationStub annotation = factory.createAnnotationStub(); |
136 |
16 |
annotation.setAnnotationId(xwikiAnnotation.getId()); |
137 |
16 |
annotation.setState(xwikiAnnotation.getState().toString()); |
138 |
|
|
139 |
16 |
for (String extraField : requestedFields) { |
140 |
0 |
Object value = xwikiAnnotation.get(extraField); |
141 |
0 |
AnnotationField field = new AnnotationField(); |
142 |
0 |
field.setName(extraField); |
143 |
|
|
144 |
0 |
field.setValue(value != null ? value.toString() : null); |
145 |
0 |
annotation.getFields().add(field); |
146 |
|
} |
147 |
16 |
set.add(annotation); |
148 |
|
} |
149 |
8 |
return set; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@param |
156 |
|
@return |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
158 |
0 |
protected AnnotationResponse getErrorResponse(Throwable exception)... |
159 |
|
{ |
160 |
0 |
AnnotationResponse result = new ObjectFactory().createAnnotationResponse(); |
161 |
0 |
result.setResponseCode(1); |
162 |
0 |
String responseMessage = exception.getMessage(); |
163 |
0 |
if (responseMessage == null) { |
164 |
|
|
165 |
0 |
StringWriter stackTraceWriter = new StringWriter(); |
166 |
0 |
exception.printStackTrace(new PrintWriter(stackTraceWriter)); |
167 |
0 |
responseMessage = stackTraceWriter.toString(); |
168 |
|
} |
169 |
0 |
result.setResponseMessage(responseMessage); |
170 |
0 |
result.setAnnotatedContent(null); |
171 |
0 |
return result; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@param |
181 |
|
@param |
182 |
|
@param |
183 |
|
@return |
184 |
|
@throws |
185 |
|
@throws |
186 |
|
|
|
|
| 90.9% |
Uncovered Elements: 2 (22) |
Complexity: 2 |
Complexity Density: 0.1 |
|
187 |
8 |
private String renderDocumentWithAnnotations(String docName, String language, String action,... |
188 |
|
Collection<Annotation> annotations) throws XWikiException, AnnotationServiceException |
189 |
|
{ |
190 |
8 |
String isInRenderingEngineKey = "isInRenderingEngine"; |
191 |
8 |
XWikiContext context = this.xcontextProvider.get(); |
192 |
8 |
Object isInRenderingEngine = context.get(isInRenderingEngineKey); |
193 |
|
|
194 |
|
|
195 |
8 |
XWikiURLFactory oldFactory = context.getURLFactory(); |
196 |
8 |
int oldMode = context.getMode(); |
197 |
8 |
String result = null; |
198 |
8 |
try { |
199 |
8 |
context.setMode(XWikiContext.MODE_SERVLET); |
200 |
8 |
XWikiURLFactory urlf = |
201 |
|
context.getWiki().getURLFactoryService().createURLFactory(context.getMode(), context); |
202 |
8 |
context.setURLFactory(urlf); |
203 |
|
|
204 |
8 |
setUpDocuments(docName, language); |
205 |
|
|
206 |
8 |
context.setAction(action); |
207 |
8 |
context.put(isInRenderingEngineKey, true); |
208 |
|
|
209 |
8 |
result = annotationService.getAnnotatedRenderedContent(docName, null, "xhtml/1.0", annotations); |
210 |
|
} finally { |
211 |
8 |
if (isInRenderingEngine != null) { |
212 |
0 |
context.put(isInRenderingEngineKey, isInRenderingEngine); |
213 |
|
} else { |
214 |
8 |
context.remove(isInRenderingEngineKey); |
215 |
|
} |
216 |
8 |
context.setURLFactory(oldFactory); |
217 |
8 |
context.setMode(oldMode); |
218 |
|
} |
219 |
8 |
return result; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
@param |
227 |
|
@param |
228 |
|
@throws |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
230 |
8 |
private void setUpDocuments(String docName, String language) throws XWikiException... |
231 |
|
{ |
232 |
8 |
XWikiContext context = xcontextProvider.get(); |
233 |
8 |
XWiki xwiki = context.getWiki(); |
234 |
|
|
235 |
|
|
236 |
8 |
xwiki.prepareResources(context); |
237 |
|
|
238 |
8 |
XWikiDocument doc = xwiki.getDocument(docName, context); |
239 |
|
|
240 |
8 |
context.put("doc", doc); |
241 |
8 |
context.put("cdoc", doc); |
242 |
|
|
243 |
8 |
XWikiDocument tdoc = doc.getTranslatedDocument(language, context); |
244 |
8 |
context.put("tdoc", tdoc); |
245 |
|
|
246 |
8 |
context.getWiki().renderTemplate("xwikivars.vm", context); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
@param |
255 |
|
@param |
256 |
|
@return |
257 |
|
|
|
|
| 18.8% |
Uncovered Elements: 26 (32) |
Complexity: 7 |
Complexity Density: 0.32 |
|
258 |
8 |
protected Collection<Annotation> filterAnnotations(Collection<Annotation> annotations, AnnotationRequest request)... |
259 |
|
{ |
260 |
8 |
Collection<Annotation> result = new ArrayList<Annotation>(); |
261 |
|
|
262 |
8 |
Map<String, List<String>> filters = new HashMap<String, List<String>>(); |
263 |
8 |
for (AnnotationField annotationField : request.getFilter().getFields()) { |
264 |
0 |
String filterName = annotationField.getName(); |
265 |
0 |
List<String> values = filters.get(filterName); |
266 |
0 |
if (values == null) { |
267 |
0 |
values = new ArrayList<String>(); |
268 |
0 |
filters.put(filterName, values); |
269 |
|
} |
270 |
0 |
if (annotationField.getValue() != null) { |
271 |
0 |
values.add(annotationField.getValue()); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
8 |
if (filters.size() == 0) { |
276 |
8 |
return annotations; |
277 |
|
} |
278 |
|
|
279 |
0 |
for (Annotation ann : annotations) { |
280 |
0 |
boolean matches = true; |
281 |
0 |
for (Map.Entry<String, List<String>> filter : filters.entrySet()) { |
282 |
0 |
Object annotationValue = ann.get(filter.getKey()); |
283 |
|
|
284 |
0 |
if (annotationValue == null || !filter.getValue().contains(annotationValue.toString())) { |
285 |
|
|
286 |
0 |
matches = false; |
287 |
0 |
break; |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
0 |
if (matches) { |
292 |
0 |
result.add(ann); |
293 |
|
} |
294 |
|
} |
295 |
0 |
return result; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
@return |
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
301 |
12 |
protected String getXWikiUser()... |
302 |
|
{ |
303 |
12 |
return this.xcontextProvider.get().getUser(); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
@param |
310 |
|
@param |
311 |
|
@param |
312 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
313 |
8 |
protected void updateContext(DocumentReference documentReference)... |
314 |
|
{ |
315 |
8 |
try { |
316 |
|
|
317 |
8 |
XWikiContext deprecatedContext = (XWikiContext) execution.getContext().getProperty("xwikicontext"); |
318 |
8 |
deprecatedContext.setWikiId(documentReference.getWikiReference().getName()); |
319 |
|
|
320 |
|
|
321 |
8 |
XWiki xwiki = deprecatedContext.getWiki(); |
322 |
8 |
XWikiDocument currentDocument = |
323 |
|
xwiki.getDocument(documentReference, deprecatedContext); |
324 |
8 |
deprecatedContext.setDoc(currentDocument); |
325 |
|
} catch (Exception e) { |
326 |
|
|
327 |
0 |
logger.log(Level.SEVERE, |
328 |
|
String.format("Failed to update the context for page [%s].", documentReference), e); |
329 |
|
} |
330 |
|
} |
331 |
|
} |