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

File SingleAnnotationRESTResource.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

4
30
2
1
158
82
8
0.27
15
2
4

Classes

Class Line # Actions
SingleAnnotationRESTResource 57 30 0% 8 26
0.277777827.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.annotation.rest.internal;
21   
22    import java.util.logging.Level;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27    import javax.ws.rs.DELETE;
28    import javax.ws.rs.PUT;
29    import javax.ws.rs.Path;
30    import javax.ws.rs.PathParam;
31    import javax.ws.rs.WebApplicationException;
32    import javax.ws.rs.core.Response.Status;
33   
34    import org.xwiki.annotation.Annotation;
35    import org.xwiki.annotation.AnnotationServiceException;
36    import org.xwiki.annotation.rest.model.jaxb.AnnotationField;
37    import org.xwiki.annotation.rest.model.jaxb.AnnotationRequest;
38    import org.xwiki.annotation.rest.model.jaxb.AnnotationResponse;
39    import org.xwiki.annotation.rest.model.jaxb.AnnotationUpdateRequest;
40    import org.xwiki.component.annotation.Component;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.model.reference.EntityReferenceSerializer;
43    import org.xwiki.rest.XWikiRestException;
44   
45    import com.xpn.xwiki.XWikiException;
46   
47    /**
48    * This class allow to do delete a single annotation.
49    *
50    * @version $Id: 9071afa3aa3986e8c0ca4b92a2cb190120318151 $
51    * @since 2.3M1
52    */
53    @Component
54    @Named("org.xwiki.annotation.rest.internal.SingleAnnotationRESTResource")
55    @Path("/wikis/{wikiName}/spaces/{spaceName: .+}/pages/{pageName}/annotation/{id}")
56    @Singleton
 
57    public class SingleAnnotationRESTResource extends AbstractAnnotationRESTResource
58    {
59    /**
60    * Entity reference serializer used to get reference to the document to perform annotation operation on.
61    */
62    @Inject
63    private EntityReferenceSerializer<String> referenceSerializer;
64   
65    /**
66    * Deletes the specified annotation.
67    *
68    * @param space the space of the document to delete the annotation from
69    * @param page the name of the document to delete the annotation from
70    * @param wiki the wiki of the document to delete the annotation from
71    * @param id the id of the annotation to delete
72    * @param request the annotation request to configure the returned annotated content after the execution of the
73    * operation
74    * @return a annotation response for which the response code will be 0 in case of success and non-zero otherwise
75    * @throws XWikiRestException when failing to parse space
76    */
 
77  4 toggle @DELETE
78    public AnnotationResponse doDelete(@PathParam("spaceName") String space, @PathParam("pageName") String page,
79    @PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationRequest request)
80    throws XWikiRestException
81    {
82  4 try {
83  4 DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
84   
85    // Initialize the context with the correct value.
86  4 updateContext(documentReference);
87   
88  4 String documentName = referenceSerializer.serialize(documentReference);
89   
90    // check access to this function
91  4 if (!annotationRightService.canEditAnnotation(id, documentName, getXWikiUser())) {
92  0 throw new WebApplicationException(Status.UNAUTHORIZED);
93    }
94    // remove the annotation
95  4 annotationService.removeAnnotation(documentName, id);
96    // and then return the annotated content, as specified by the annotation request
97  4 AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
98  4 return response;
99    } catch (XWikiException e) {
100  0 logger.log(Level.SEVERE, e.getMessage(), e);
101  0 return getErrorResponse(e);
102    } catch (AnnotationServiceException e) {
103  0 logger.log(Level.SEVERE, e.getMessage(), e);
104  0 return getErrorResponse(e);
105    }
106    }
107   
108    /**
109    * Updates the specified annotation with the values of the fields in received collection.
110    *
111    * @param space the space of the document to update the annotation from
112    * @param page the name of the document to update the annotation from
113    * @param wiki the wiki of the document to update the annotation from
114    * @param id the id of the annotation to update
115    * @param updateRequest the request to update the annotation pointed by the id
116    * @return a annotation response for which the response code will be 0 in case of success and non-zero otherwise
117    * @throws XWikiRestException when failing to parse space
118    */
 
119  0 toggle @PUT
120    public AnnotationResponse doUpdate(@PathParam("spaceName") String space, @PathParam("pageName") String page,
121    @PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationUpdateRequest updateRequest)
122    throws XWikiRestException
123    {
124  0 try {
125  0 DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
126   
127    // Initialize the context with the correct value.
128  0 updateContext(documentReference);
129   
130  0 String documentName = referenceSerializer.serialize(documentReference);
131   
132    // check access to this function
133  0 if (!annotationRightService.canEditAnnotation(id, documentName, getXWikiUser())) {
134  0 throw new WebApplicationException(Status.UNAUTHORIZED);
135    }
136   
137    // id from the url
138  0 Annotation newAnnotation = new Annotation(id);
139    // fields from the posted content
140  0 for (AnnotationField field : updateRequest.getAnnotation().getFields()) {
141  0 newAnnotation.set(field.getName(), field.getValue());
142    }
143    // overwrite author if any was set because we know better who's logged in
144  0 newAnnotation.setAuthor(getXWikiUser());
145    // and update
146  0 annotationService.updateAnnotation(documentName, newAnnotation);
147    // and then return the annotated content, as specified by the annotation request
148  0 AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, updateRequest);
149  0 return response;
150    } catch (XWikiException e) {
151  0 logger.log(Level.SEVERE, e.getMessage(), e);
152  0 return getErrorResponse(e);
153    } catch (AnnotationServiceException e) {
154  0 logger.log(Level.SEVERE, e.getMessage(), e);
155  0 return getErrorResponse(e);
156    }
157    }
158    }