Search in sources :

Example 1 with AnnotationFieldCollection

use of org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection in project xwiki-platform by xwiki.

the class AnnotationsRESTResource method doGetAnnotatedContent.

/**
 * @param wiki the wiki of the document to get annotations for
 * @param space the space of the document to get annotations for
 * @param page the name of the document to get annotation for
 * @return annotations of a given XWiki page. Note that we're returning a response holding the AnnotatedContent
 *         instead of an AnnotatedContent object because we need to be able to set custom expire fields to prevent
 *         IE from caching this resource.
 * @throws XWikiRestException when failing to parse space
 */
@GET
public Response doGetAnnotatedContent(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki) throws XWikiRestException {
    try {
        DocumentReference documentReference = new DocumentReference(wiki, parseSpaceSegments(space), page);
        // Initialize the context with the correct value.
        updateContext(documentReference);
        String documentName = referenceSerializer.serialize(documentReference);
        // check access to this function
        if (!annotationRightService.canViewAnnotatedTarget(documentName, getXWikiUser())) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        // Manually construct the Annotation request entity
        // Historically it used to be passed as method argument, but this only worked because of a bug in
        // earlier version of Restlet (1.1.x) ; the JAX-RS specification explicitly states that such entity
        // parameters "[are] mapped from the request entity body.", and thus cannot be passed to a GET resource.
        // See paragraph 3.3.2.1 "Entity Parameters" of JAX-RS 1.1 specification.
        Form form = Request.getCurrent().getResourceRef().getQueryAsForm();
        AnnotationRequest request = new AnnotationRequest();
        AnnotationFieldCollection fields = new AnnotationFieldCollection();
        List<AnnotationField> annotationFields = new ArrayList<AnnotationField>();
        AnnotationRequest.Request requestedFields = new AnnotationRequest.Request();
        for (String name : form.getNames()) {
            if (StringUtils.startsWith(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX)) {
                for (String value : form.getValuesArray(name)) {
                    AnnotationField field = new AnnotationField();
                    field.setName(StringUtils.substringAfter(name, ANNOTATION_REQUEST_FILTER_PARAMETER_PREFIX));
                    field.setValue(value);
                    annotationFields.add(field);
                }
            } else if (StringUtils.equals(name, ANNOTATION_REQUEST_REQUESTED_FIELD_PARAMETER)) {
                requestedFields.getFields().addAll(Arrays.asList(form.getValuesArray(name)));
            }
        }
        request.setRequest(requestedFields);
        fields.getFields().addAll(annotationFields);
        request.setFilter(fields);
        AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
        // make this content expire now because cacheControl is not implemented in this version of restlet
        return Response.ok(response).expires(new Date()).build();
    } catch (AnnotationServiceException e) {
        getLogger().error(e.getMessage(), e);
        return Response.ok(getErrorResponse(e)).build();
    } catch (XWikiException e) {
        getLogger().error(e.getMessage(), e);
        return Response.ok(getErrorResponse(e)).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AnnotationRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationRequest) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) Form(org.restlet.data.Form) ArrayList(java.util.ArrayList) AnnotationAddRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest) AnnotationRequest(org.xwiki.annotation.rest.model.jaxb.AnnotationRequest) Request(org.restlet.Request) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) Date(java.util.Date) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) AnnotationFieldCollection(org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) GET(javax.ws.rs.GET)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Request (org.restlet.Request)1 Form (org.restlet.data.Form)1 AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)1 AnnotationAddRequest (org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest)1 AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)1 AnnotationFieldCollection (org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection)1 AnnotationRequest (org.xwiki.annotation.rest.model.jaxb.AnnotationRequest)1 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1