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.representations.comments; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.lang.annotation.Annotation; |
25 |
|
import java.lang.reflect.Type; |
26 |
|
|
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
import javax.servlet.http.HttpServletRequest; |
30 |
|
import javax.ws.rs.Consumes; |
31 |
|
import javax.ws.rs.WebApplicationException; |
32 |
|
import javax.ws.rs.core.MediaType; |
33 |
|
import javax.ws.rs.core.MultivaluedMap; |
34 |
|
import javax.ws.rs.ext.MessageBodyReader; |
35 |
|
import javax.ws.rs.ext.Provider; |
36 |
|
|
37 |
|
import org.restlet.Context; |
38 |
|
import org.restlet.data.Form; |
39 |
|
import org.restlet.representation.InputRepresentation; |
40 |
|
import org.restlet.representation.Representation; |
41 |
|
import org.xwiki.component.annotation.Component; |
42 |
|
import org.xwiki.rest.Constants; |
43 |
|
import org.xwiki.rest.XWikiRestComponent; |
44 |
|
import org.xwiki.rest.model.jaxb.Comment; |
45 |
|
import org.xwiki.rest.model.jaxb.ObjectFactory; |
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
|
50 |
|
@Component |
51 |
|
@Named("org.xwiki.rest.internal.representations.comments.FormUrlEncodedCommentReader") |
52 |
|
@Provider |
53 |
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
54 |
|
@Singleton |
|
|
| 77.8% |
Uncovered Elements: 4 (18) |
Complexity: 5 |
Complexity Density: 0.36 |
|
55 |
|
public class FormUrlEncodedCommentReader implements MessageBodyReader<Comment>, XWikiRestComponent |
56 |
|
{ |
57 |
|
private static final String COMMENT_TEXT_FIELD_NAME = "text"; |
58 |
|
|
59 |
|
private static final String COMMENT_REPLYTO_FIELD_NAME = "replyTo"; |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
11 |
@Override... |
62 |
|
public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) |
63 |
|
{ |
64 |
11 |
return Comment.class.isAssignableFrom(type); |
65 |
|
} |
66 |
|
|
|
|
| 73.3% |
Uncovered Elements: 4 (15) |
Complexity: 4 |
Complexity Density: 0.31 |
|
67 |
1 |
@Override... |
68 |
|
public Comment readFrom(Class<Comment> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
69 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
70 |
|
WebApplicationException |
71 |
|
{ |
72 |
1 |
ObjectFactory objectFactory = new ObjectFactory(); |
73 |
1 |
Comment comment = objectFactory.createComment(); |
74 |
|
|
75 |
1 |
HttpServletRequest httpServletRequest = |
76 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
77 |
|
|
78 |
1 |
Representation representation = |
79 |
|
new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM); |
80 |
1 |
Form form = new Form(representation); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
1 |
if (form.getNames().isEmpty()) { |
87 |
1 |
try { |
88 |
1 |
comment.setReplyTo(Integer.parseInt(httpServletRequest.getParameter(COMMENT_REPLYTO_FIELD_NAME))); |
89 |
|
} |
90 |
|
catch(NumberFormatException e) { |
91 |
|
|
92 |
|
} |
93 |
1 |
comment.setText(httpServletRequest.getParameter(COMMENT_TEXT_FIELD_NAME)); |
94 |
|
} else { |
95 |
0 |
try { |
96 |
0 |
comment.setReplyTo(Integer.parseInt(form.getFirstValue(COMMENT_REPLYTO_FIELD_NAME))); |
97 |
|
} |
98 |
|
catch(NumberFormatException e) { |
99 |
|
|
100 |
|
} |
101 |
0 |
comment.setText(form.getFirstValue(COMMENT_TEXT_FIELD_NAME)); |
102 |
|
} |
103 |
|
|
104 |
1 |
return comment; |
105 |
|
} |
106 |
|
|
107 |
|
} |