1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.internal.representations.tags

File FormUrlEncodedTagsReader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

8
32
2
1
130
87
6
0.19
16
2
3

Classes

Class Line # Actions
FormUrlEncodedTagsReader 57 32 0% 6 20
0.5238095552.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 $Id: 03c17f215720eea5f6d7ec0f60ae5336a213cc1a $
51    */
52    @Component
53    @Named("org.xwiki.rest.internal.representations.tags.FormUrlEncodedTagsReader")
54    @Provider
55    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
56    @Singleton
 
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   
 
63  7 toggle @Override
64    public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType)
65    {
66  7 return Tags.class.isAssignableFrom(type);
67    }
68   
 
69  1 toggle @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    * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
86    * read data using getParameter()
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    }