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.pages; |
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.ObjectFactory; |
45 |
|
import org.xwiki.rest.model.jaxb.Page; |
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
|
50 |
|
@Component |
51 |
|
@Named("org.xwiki.rest.internal.representations.pages.FormUrlEncodedPageReader") |
52 |
|
@Provider |
53 |
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
54 |
|
@Singleton |
|
|
| 75% |
Uncovered Elements: 5 (20) |
Complexity: 3 |
Complexity Density: 0.19 |
|
55 |
|
public class FormUrlEncodedPageReader implements MessageBodyReader<Page>, XWikiRestComponent |
56 |
|
{ |
57 |
|
private static String TITLE_FIELD_NAME = "title"; |
58 |
|
|
59 |
|
private static String PARENT_FIELD_NAME = "parent"; |
60 |
|
|
61 |
|
private static String HIDDEN_FIELD_NAME = "hidden"; |
62 |
|
|
63 |
|
private static String CONTENT_FIELD_NAME = "content"; |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
10 |
@Override... |
66 |
|
public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) |
67 |
|
{ |
68 |
10 |
return Page.class.isAssignableFrom(type); |
69 |
|
} |
70 |
|
|
|
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 2 |
Complexity Density: 0.13 |
|
71 |
1 |
@Override... |
72 |
|
public Page readFrom(Class<Page> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
73 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
74 |
|
WebApplicationException |
75 |
|
{ |
76 |
1 |
ObjectFactory objectFactory = new ObjectFactory(); |
77 |
1 |
Page page = objectFactory.createPage(); |
78 |
|
|
79 |
1 |
HttpServletRequest httpServletRequest = |
80 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
81 |
|
|
82 |
1 |
Representation representation = |
83 |
|
new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM); |
84 |
1 |
Form form = new Form(representation); |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
1 |
if (form.getNames().isEmpty()) { |
91 |
1 |
page.setTitle(httpServletRequest.getParameter(TITLE_FIELD_NAME)); |
92 |
1 |
page.setParent(httpServletRequest.getParameter(PARENT_FIELD_NAME)); |
93 |
1 |
page.setHidden(Boolean.valueOf(httpServletRequest.getParameter(HIDDEN_FIELD_NAME))); |
94 |
1 |
page.setContent(httpServletRequest.getParameter(CONTENT_FIELD_NAME)); |
95 |
|
} else { |
96 |
0 |
page.setTitle(form.getFirstValue(TITLE_FIELD_NAME)); |
97 |
0 |
page.setParent(form.getFirstValue(PARENT_FIELD_NAME)); |
98 |
0 |
page.setHidden(Boolean.valueOf(form.getFirstValue(HIDDEN_FIELD_NAME))); |
99 |
0 |
page.setContent(form.getFirstValue(CONTENT_FIELD_NAME)); |
100 |
|
} |
101 |
|
|
102 |
1 |
return page; |
103 |
|
} |
104 |
|
|
105 |
|
} |