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.objects; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.io.InputStream; |
24 |
|
import java.lang.annotation.Annotation; |
25 |
|
import java.lang.reflect.Type; |
26 |
|
import java.util.Enumeration; |
27 |
|
|
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
import javax.servlet.http.HttpServletRequest; |
31 |
|
import javax.ws.rs.Consumes; |
32 |
|
import javax.ws.rs.WebApplicationException; |
33 |
|
import javax.ws.rs.core.MediaType; |
34 |
|
import javax.ws.rs.core.MultivaluedMap; |
35 |
|
import javax.ws.rs.ext.MessageBodyReader; |
36 |
|
import javax.ws.rs.ext.Provider; |
37 |
|
|
38 |
|
import org.restlet.Context; |
39 |
|
import org.restlet.data.Form; |
40 |
|
import org.restlet.representation.InputRepresentation; |
41 |
|
import org.restlet.representation.Representation; |
42 |
|
import org.xwiki.component.annotation.Component; |
43 |
|
import org.xwiki.rest.Constants; |
44 |
|
import org.xwiki.rest.XWikiRestComponent; |
45 |
|
import org.xwiki.rest.model.jaxb.Object; |
46 |
|
import org.xwiki.rest.model.jaxb.ObjectFactory; |
47 |
|
import org.xwiki.rest.model.jaxb.Property; |
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("org.xwiki.rest.internal.representations.objects.FormUrlEncodedObjectReader") |
54 |
|
@Provider |
55 |
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
56 |
|
@Singleton |
|
|
| 70.6% |
Uncovered Elements: 10 (34) |
Complexity: 6 |
Complexity Density: 0.25 |
|
57 |
|
public class FormUrlEncodedObjectReader implements MessageBodyReader<Object>, XWikiRestComponent |
58 |
|
{ |
59 |
|
private static final String CLASSNAME_FIELD_NAME = "className"; |
60 |
|
|
61 |
|
private static final String PROPERTY_PREFIX = "property#"; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
63 |
13 |
@Override... |
64 |
|
public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) |
65 |
|
{ |
66 |
13 |
return Object.class.isAssignableFrom(type); |
67 |
|
} |
68 |
|
|
|
|
| 67.7% |
Uncovered Elements: 10 (31) |
Complexity: 5 |
Complexity Density: 0.22 |
|
69 |
2 |
@Override... |
70 |
|
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
71 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
72 |
|
WebApplicationException |
73 |
|
{ |
74 |
2 |
ObjectFactory objectFactory = new ObjectFactory(); |
75 |
2 |
Object object = objectFactory.createObject(); |
76 |
|
|
77 |
2 |
HttpServletRequest httpServletRequest = |
78 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
79 |
|
|
80 |
2 |
Representation representation = |
81 |
|
new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM); |
82 |
2 |
Form form = new Form(representation); |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
2 |
if (form.getNames().isEmpty()) { |
89 |
2 |
object.setClassName(httpServletRequest.getParameter(CLASSNAME_FIELD_NAME)); |
90 |
|
|
91 |
2 |
Enumeration enumeration = httpServletRequest.getParameterNames(); |
92 |
6 |
while (enumeration.hasMoreElements()) { |
93 |
4 |
String name = (String) enumeration.nextElement(); |
94 |
4 |
if (name.startsWith(PROPERTY_PREFIX)) { |
95 |
2 |
Property property = objectFactory.createProperty(); |
96 |
2 |
property.setName(name.replace(PROPERTY_PREFIX, "")); |
97 |
2 |
property.setValue(httpServletRequest.getParameter(name)); |
98 |
2 |
object.getProperties().add(property); |
99 |
|
} |
100 |
|
} |
101 |
|
} else { |
102 |
0 |
object.setClassName(form.getFirstValue(CLASSNAME_FIELD_NAME)); |
103 |
|
|
104 |
0 |
for (String name : form.getNames()) { |
105 |
0 |
if (name.startsWith(PROPERTY_PREFIX)) { |
106 |
0 |
Property property = objectFactory.createProperty(); |
107 |
0 |
property.setName(name.replace(PROPERTY_PREFIX, "")); |
108 |
0 |
property.setValue(form.getFirstValue(name)); |
109 |
0 |
object.getProperties().add(property); |
110 |
|
} |
111 |
|
} |
112 |
|
} |
113 |
|
|
114 |
2 |
return object; |
115 |
|
} |
116 |
|
|
117 |
|
} |