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

File FormUrlEncodedPageReader.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
16
2
1
105
65
3
0.19
8
2
1.5

Classes

Class Line # Actions
FormUrlEncodedPageReader 55 16 0% 3 5
0.7575%
 

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.pages;
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    import org.xwiki.component.annotation.Component;
42    import org.xwiki.rest.Constants;
43    import org.xwiki.rest.XWikiRestComponent;
44    import org.xwiki.rest.model.jaxb.ObjectFactory;
45    import org.xwiki.rest.model.jaxb.Page;
46   
47    /**
48    * @version $Id: 6725ea5c84658b394e0808ad9064a5928bc87319 $
49    */
50    @Component
51    @Named("org.xwiki.rest.internal.representations.pages.FormUrlEncodedPageReader")
52    @Provider
53    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
54    @Singleton
 
55    public class FormUrlEncodedPageReader implements MessageBodyReader<Page>, XWikiRestComponent
56    {
57    private static String TITLE_FIELD_NAME = "title";
58   
59    private static String PARENT_FIELD_NAME = "parent";
60   
61    private static String HIDDEN_FIELD_NAME = "hidden";
62   
63    private static String CONTENT_FIELD_NAME = "content";
64   
 
65  10 toggle @Override
66    public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType)
67    {
68  10 return Page.class.isAssignableFrom(type);
69    }
70   
 
71  1 toggle @Override
72    public Page readFrom(Class<Page> type, Type genericType, Annotation[] annotations, MediaType mediaType,
73    MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException,
74    WebApplicationException
75    {
76  1 ObjectFactory objectFactory = new ObjectFactory();
77  1 Page page = objectFactory.createPage();
78   
79  1 HttpServletRequest httpServletRequest =
80    (HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST);
81   
82  1 Representation representation =
83    new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
84  1 Form form = new Form(representation);
85   
86    /*
87    * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
88    * read data using getParameter()
89    */
90  1 if (form.getNames().isEmpty()) {
91  1 page.setTitle(httpServletRequest.getParameter(TITLE_FIELD_NAME));
92  1 page.setParent(httpServletRequest.getParameter(PARENT_FIELD_NAME));
93  1 page.setHidden(Boolean.valueOf(httpServletRequest.getParameter(HIDDEN_FIELD_NAME)));
94  1 page.setContent(httpServletRequest.getParameter(CONTENT_FIELD_NAME));
95    } else {
96  0 page.setTitle(form.getFirstValue(TITLE_FIELD_NAME));
97  0 page.setParent(form.getFirstValue(PARENT_FIELD_NAME));
98  0 page.setHidden(Boolean.valueOf(form.getFirstValue(HIDDEN_FIELD_NAME)));
99  0 page.setContent(form.getFirstValue(CONTENT_FIELD_NAME));
100    }
101   
102  1 return page;
103    }
104   
105    }