1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.annotation.rest.internal.representations; |
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.Map; |
27 |
|
|
28 |
|
import javax.servlet.http.HttpServletRequest; |
29 |
|
import javax.ws.rs.WebApplicationException; |
30 |
|
import javax.ws.rs.core.MediaType; |
31 |
|
import javax.ws.rs.core.MultivaluedMap; |
32 |
|
import javax.ws.rs.ext.MessageBodyReader; |
33 |
|
|
34 |
|
import org.restlet.Context; |
35 |
|
import org.restlet.Request; |
36 |
|
import org.restlet.data.Form; |
37 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationField; |
38 |
|
import org.xwiki.annotation.rest.model.jaxb.AnnotationRequest; |
39 |
|
import org.xwiki.annotation.rest.model.jaxb.ObjectFactory; |
40 |
|
import org.xwiki.rest.Constants; |
41 |
|
import org.xwiki.rest.XWikiRestComponent; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@param |
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 59.5% |
Uncovered Elements: 15 (37) |
Complexity: 8 |
Complexity Density: 0.3 |
|
51 |
|
public abstract class AbstractFormUrlEncodedAnnotationRequestReader<T extends AnnotationRequest> implements |
52 |
|
MessageBodyReader<T>, XWikiRestComponent |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
protected static final String REQUESTED_FIELD = "request_field"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
protected static final String FILTER_FIELD_PREFIX = "filter_"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
@return |
71 |
|
|
72 |
|
protected abstract T getReadObjectInstance(ObjectFactory factory); |
73 |
|
|
|
|
| 71.4% |
Uncovered Elements: 6 (21) |
Complexity: 5 |
Complexity Density: 0.29 |
|
74 |
8 |
@Override... |
75 |
|
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
76 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
77 |
|
WebApplicationException |
78 |
|
{ |
79 |
8 |
ObjectFactory objectFactory = new ObjectFactory(); |
80 |
8 |
T annotationRequest = getReadObjectInstance(objectFactory); |
81 |
|
|
82 |
8 |
try { |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
8 |
Form form = new Form(Request.getCurrent().getEntity()); |
87 |
|
|
88 |
0 |
if (form.getNames().size() != 0) { |
89 |
0 |
for (String paramName : form.getNames()) { |
90 |
0 |
for (String paramValue : form.getValuesArray(paramName)) { |
91 |
0 |
saveField(annotationRequest, paramName, paramValue, objectFactory); |
92 |
|
} |
93 |
|
} |
94 |
|
} |
95 |
|
} catch (IllegalStateException e) { |
96 |
|
|
97 |
|
|
98 |
8 |
HttpServletRequest httpServletRequest = |
99 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
100 |
8 |
for (Object entryObj : httpServletRequest.getParameterMap().entrySet()) { |
101 |
36 |
Map.Entry entry = (Map.Entry) entryObj; |
102 |
|
|
103 |
|
|
104 |
36 |
if ("method".equals(entry.getKey()) || "media".equals(entry.getKey())) { |
105 |
12 |
continue; |
106 |
|
} |
107 |
|
|
108 |
24 |
String[] paramValues = (String[]) entry.getValue(); |
109 |
24 |
for (String value : paramValues) { |
110 |
24 |
saveField(annotationRequest, (String) entry.getKey(), value, objectFactory); |
111 |
|
} |
112 |
|
} |
113 |
|
} |
114 |
|
|
115 |
8 |
return annotationRequest; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@param |
123 |
|
@param |
124 |
|
@param |
125 |
|
@param |
126 |
|
@return |
127 |
|
|
|
|
| 35.7% |
Uncovered Elements: 9 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
128 |
12 |
protected boolean saveField(T readObject, String key, String value, ObjectFactory objectFactory)... |
129 |
|
{ |
130 |
|
|
131 |
12 |
if (REQUESTED_FIELD.equals(key)) { |
132 |
0 |
readObject.getRequest().getFields().add(value); |
133 |
0 |
return true; |
134 |
|
} |
135 |
|
|
136 |
12 |
if (key.startsWith(FILTER_FIELD_PREFIX)) { |
137 |
0 |
AnnotationField filterField = objectFactory.createAnnotationField(); |
138 |
|
|
139 |
0 |
filterField.setName(key.substring(FILTER_FIELD_PREFIX.length())); |
140 |
0 |
filterField.setValue(value); |
141 |
0 |
readObject.getFilter().getFields().add(filterField); |
142 |
0 |
return true; |
143 |
|
} |
144 |
|
|
145 |
12 |
return false; |
146 |
|
} |
147 |
|
} |