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.tags; |
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 |
|
|
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.Tag; |
47 |
|
import org.xwiki.rest.model.jaxb.Tags; |
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
52 |
|
@Component |
53 |
|
@Named("org.xwiki.rest.internal.representations.tags.FormUrlEncodedTagsReader") |
54 |
|
@Provider |
55 |
|
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) |
56 |
|
@Singleton |
|
|
| 52.4% |
Uncovered Elements: 20 (42) |
Complexity: 6 |
Complexity Density: 0.19 |
|
57 |
|
public class FormUrlEncodedTagsReader implements MessageBodyReader<Tags>, XWikiRestComponent |
58 |
|
{ |
59 |
|
private static final String TAGS_FIELD_NAME = "tags"; |
60 |
|
|
61 |
|
private static final String TAG_FIELD_NAME = "tag"; |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
63 |
7 |
@Override... |
64 |
|
public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType) |
65 |
|
{ |
66 |
7 |
return Tags.class.isAssignableFrom(type); |
67 |
|
} |
68 |
|
|
|
|
| 48.7% |
Uncovered Elements: 20 (39) |
Complexity: 5 |
Complexity Density: 0.16 |
|
69 |
1 |
@Override... |
70 |
|
public Tags readFrom(Class<Tags> type, Type genericType, Annotation[] annotations, MediaType mediaType, |
71 |
|
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, |
72 |
|
WebApplicationException |
73 |
|
{ |
74 |
1 |
ObjectFactory objectFactory = new ObjectFactory(); |
75 |
1 |
Tags tags = objectFactory.createTags(); |
76 |
|
|
77 |
1 |
HttpServletRequest httpServletRequest = |
78 |
|
(HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST); |
79 |
|
|
80 |
1 |
Representation representation = |
81 |
|
new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM); |
82 |
1 |
Form form = new Form(representation); |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
1 |
if (form.getNames().isEmpty()) { |
89 |
1 |
String text = httpServletRequest.getParameter(TAGS_FIELD_NAME); |
90 |
1 |
if (text != null) { |
91 |
1 |
String[] tagNames = text.split(" |,|\\|"); |
92 |
|
|
93 |
1 |
for (String tagName : tagNames) { |
94 |
1 |
Tag tag = objectFactory.createTag(); |
95 |
1 |
tag.setName(tagName); |
96 |
1 |
tags.getTags().add(tag); |
97 |
|
} |
98 |
|
} |
99 |
|
|
100 |
1 |
String[] tagNames = httpServletRequest.getParameterValues(TAG_FIELD_NAME); |
101 |
1 |
if (tagNames != null) { |
102 |
0 |
for (String tagName : tagNames) { |
103 |
0 |
Tag tag = objectFactory.createTag(); |
104 |
0 |
tag.setName(tagName); |
105 |
0 |
tags.getTags().add(tag); |
106 |
|
} |
107 |
|
} |
108 |
|
} else { |
109 |
0 |
String text = form.getFirstValue(TAGS_FIELD_NAME); |
110 |
0 |
if (text != null) { |
111 |
0 |
String[] tagNames = text.split(" |,|\\|"); |
112 |
|
|
113 |
0 |
for (String tagName : tagNames) { |
114 |
0 |
Tag tag = objectFactory.createTag(); |
115 |
0 |
tag.setName(tagName); |
116 |
0 |
tags.getTags().add(tag); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
0 |
for (String tagName : form.getValuesArray(TAG_FIELD_NAME)) { |
121 |
0 |
Tag tag = objectFactory.createTag(); |
122 |
0 |
tag.setName(tagName); |
123 |
0 |
tags.getTags().add(tag); |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
1 |
return tags; |
128 |
|
} |
129 |
|
|
130 |
|
} |