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.ObjectFactory; |
46 |
|
import org.xwiki.rest.model.jaxb.Property; |
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
|
51 |
|
@Component |
52 |
|
@Named("org.xwiki.rest.internal.representations.objects.FormUrlEncodedPropertyReader") |
53 |
|
@Provider |
54 |
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
55 |
|
@Singleton |
|
|
| 70% |
Uncovered Elements: 9 (30) |
Complexity: 6 |
Complexity Density: 0.3 |
|
56 |
|
public class FormUrlEncodedPropertyReader implements MessageBodyReader<Property>, XWikiRestComponent |
57 |
|
{ |
58 |
|
private static final String PROPERTY_PREFIX = "property#"; |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
60 |
10 |
@Override... |
61 |
|
public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) |
62 |
|
{ |
63 |
10 |
return Property.class.isAssignableFrom(type); |
64 |
|
} |
65 |
|
|
|
|
| 66.7% |
Uncovered Elements: 9 (27) |
Complexity: 5 |
Complexity Density: 0.26 |
|
66 |
1 |
@Override... |
67 |
|
public Property readFrom(Class<Property> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
68 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
69 |
|
WebApplicationException |
70 |
|
{ |
71 |
1 |
ObjectFactory objectFactory = new ObjectFactory(); |
72 |
1 |
Property property = objectFactory.createProperty(); |
73 |
|
|
74 |
1 |
HttpServletRequest httpServletRequest = |
75 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
76 |
|
|
77 |
1 |
Representation representation = |
78 |
|
new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM); |
79 |
1 |
Form form = new Form(representation); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
1 |
if (form.getNames().isEmpty()) { |
86 |
1 |
Enumeration names = httpServletRequest.getParameterNames(); |
87 |
2 |
while (names.hasMoreElements()) { |
88 |
2 |
String name = (String) names.nextElement(); |
89 |
2 |
if (name.startsWith(PROPERTY_PREFIX)) { |
90 |
1 |
property.setName(name.replace(PROPERTY_PREFIX, "")); |
91 |
1 |
property.setValue(httpServletRequest.getParameter(name)); |
92 |
1 |
break; |
93 |
|
} |
94 |
|
} |
95 |
|
} else { |
96 |
0 |
for (String name : form.getNames()) |
97 |
0 |
if (name.startsWith(PROPERTY_PREFIX)) { |
98 |
0 |
property.setName(name.replace(PROPERTY_PREFIX, "")); |
99 |
0 |
property.setValue(form.getFirstValue(name)); |
100 |
0 |
break; |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
1 |
return property; |
105 |
|
} |
106 |
|
|
107 |
|
} |