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

File FormUrlEncodedPropertyReader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

8
20
2
1
107
70
6
0.3
10
2
3

Classes

Class Line # Actions
FormUrlEncodedPropertyReader 56 20 0% 6 9
0.770%
 

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.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 $Id: 303633b2215e8e096ee1c8ff83e2bf26993b0da0 $
50    */
51    @Component
52    @Named("org.xwiki.rest.internal.representations.objects.FormUrlEncodedPropertyReader")
53    @Provider
54    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
55    @Singleton
 
56    public class FormUrlEncodedPropertyReader implements MessageBodyReader<Property>, XWikiRestComponent
57    {
58    private static final String PROPERTY_PREFIX = "property#";
59   
 
60  10 toggle @Override
61    public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType)
62    {
63  10 return Property.class.isAssignableFrom(type);
64    }
65   
 
66  1 toggle @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    * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
83    * read data using getParameter()
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    }