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

File FormUrlEncodedCommentReader.java

 

Coverage histogram

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

Code metrics

2
14
2
1
107
67
5
0.36
7
2
2.5

Classes

Class Line # Actions
FormUrlEncodedCommentReader 55 14 0% 5 4
0.777777877.8%
 

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.comments;
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.Comment;
45    import org.xwiki.rest.model.jaxb.ObjectFactory;
46   
47    /**
48    * @version $Id: 631028cc94d9a513946149dc7d7b35465fdae04d $
49    */
50    @Component
51    @Named("org.xwiki.rest.internal.representations.comments.FormUrlEncodedCommentReader")
52    @Provider
53    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
54    @Singleton
 
55    public class FormUrlEncodedCommentReader implements MessageBodyReader<Comment>, XWikiRestComponent
56    {
57    private static final String COMMENT_TEXT_FIELD_NAME = "text";
58   
59    private static final String COMMENT_REPLYTO_FIELD_NAME = "replyTo";
60   
 
61  11 toggle @Override
62    public boolean isReadable(Class< ? > type, Type genericType, Annotation[] annotations, MediaType mediaType)
63    {
64  11 return Comment.class.isAssignableFrom(type);
65    }
66   
 
67  1 toggle @Override
68    public Comment readFrom(Class<Comment> type, Type genericType, Annotation[] annotations, MediaType mediaType,
69    MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException,
70    WebApplicationException
71    {
72  1 ObjectFactory objectFactory = new ObjectFactory();
73  1 Comment comment = objectFactory.createComment();
74   
75  1 HttpServletRequest httpServletRequest =
76    (HttpServletRequest) Context.getCurrent().getAttributes().get(Constants.HTTP_REQUEST);
77   
78  1 Representation representation =
79    new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
80  1 Form form = new Form(representation);
81   
82    /*
83    * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
84    * read data using getParameter()
85    */
86  1 if (form.getNames().isEmpty()) {
87  1 try {
88  1 comment.setReplyTo(Integer.parseInt(httpServletRequest.getParameter(COMMENT_REPLYTO_FIELD_NAME)));
89    }
90    catch(NumberFormatException e) {
91    // Just ignore, there won't be a reply to.
92    }
93  1 comment.setText(httpServletRequest.getParameter(COMMENT_TEXT_FIELD_NAME));
94    } else {
95  0 try {
96  0 comment.setReplyTo(Integer.parseInt(form.getFirstValue(COMMENT_REPLYTO_FIELD_NAME)));
97    }
98    catch(NumberFormatException e) {
99    // Just ignore, there won't be a reply to.
100    }
101  0 comment.setText(form.getFirstValue(COMMENT_TEXT_FIELD_NAME));
102    }
103   
104  1 return comment;
105    }
106   
107    }