Search in sources :

Example 1 with AnnotationServiceException

use of org.xwiki.annotation.AnnotationServiceException in project xwiki-platform by xwiki.

the class AnnotationsRESTResource method doPostAnnotation.

/**
 * Add annotation to a given page.
 *
 * @param wiki the wiki of the document to add annotation on
 * @param space the space of the document to add annotation on
 * @param page the name of the document to add annotation on
 * @param request the request object with the annotation to be added
 * @return AnnotationRequestResponse, responseCode = 0 if no error
 * @throws XWikiRestException when failing to parse space
 */
@POST
public AnnotationResponse doPostAnnotation(@PathParam("wikiName") String wiki, @PathParam("spaceName") String space, @PathParam("pageName") String page, AnnotationAddRequest request) 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.canAddAnnotation(documentName, getXWikiUser())) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        // add the annotation
        Map<String, Object> annotationMetadata = getMap(request.getAnnotation());
        annotationService.addAnnotation(documentName, request.getSelection(), request.getSelectionContext(), request.getSelectionOffset(), getXWikiUser(), annotationMetadata);
        // and then return the annotated content, as specified by the annotation request
        AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
        return response;
    } catch (AnnotationServiceException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    } catch (XWikiException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) POST(javax.ws.rs.POST)

Example 2 with AnnotationServiceException

use of org.xwiki.annotation.AnnotationServiceException 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)

Example 3 with AnnotationServiceException

use of org.xwiki.annotation.AnnotationServiceException in project xwiki-platform by xwiki.

the class DefaultAnnotationService method addAnnotation.

@Override
public void addAnnotation(String target, String selection, String selectionContext, int offset, String author, Map<String, Object> metadata) throws AnnotationServiceException {
    try {
        // create the annotation with this data and send it to the storage service
        // TODO: also think of mapping the annotation on the document at add time and fail it if it's not mappable,
        // for extra security
        // TODO: normalize spaces at this level
        String leftContext = selectionContext.substring(0, offset);
        String rightContext = selectionContext.substring(offset + selection.length());
        Annotation annotation = new Annotation(selection, leftContext, rightContext);
        annotation.setAuthor(author);
        // skip these fields as we don't want to overwrite them with whatever is in this map. Setters should be used
        // for these values or constructor
        Collection<String> skippedFields = Arrays.asList(new String[] { Annotation.SELECTION_FIELD, Annotation.SELECTION_LEFT_CONTEXT_FIELD, Annotation.SELECTION_RIGHT_CONTEXT_FIELD, Annotation.ORIGINAL_SELECTION_FIELD, Annotation.AUTHOR_FIELD, Annotation.STATE_FIELD });
        for (Map.Entry<String, Object> field : metadata.entrySet()) {
            if (!skippedFields.contains(field.getKey())) {
                annotation.set(field.getKey(), field.getValue());
            }
        }
        ioService.addAnnotation(target, annotation);
    } catch (IOServiceException e) {
        throw new AnnotationServiceException("An exception occurred when accessing the storage services", e);
    }
}
Also used : IOServiceException(org.xwiki.annotation.io.IOServiceException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) Map(java.util.Map) Annotation(org.xwiki.annotation.Annotation)

Example 4 with AnnotationServiceException

use of org.xwiki.annotation.AnnotationServiceException in project xwiki-platform by xwiki.

the class SingleAnnotationRESTResource method doUpdate.

/**
 * Updates the specified annotation with the values of the fields in received collection.
 *
 * @param space the space of the document to update the annotation from
 * @param page the name of the document to update the annotation from
 * @param wiki the wiki of the document to update the annotation from
 * @param id the id of the annotation to update
 * @param updateRequest the request to update the annotation pointed by the id
 * @return a annotation response for which the response code will be 0 in case of success and non-zero otherwise
 * @throws XWikiRestException when failing to parse space
 */
@PUT
public AnnotationResponse doUpdate(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationUpdateRequest updateRequest) 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.canEditAnnotation(id, documentName, getXWikiUser())) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        // id from the url
        Annotation newAnnotation = new Annotation(id);
        // fields from the posted content
        for (AnnotationField field : updateRequest.getAnnotation().getFields()) {
            newAnnotation.set(field.getName(), field.getValue());
        }
        // overwrite author if any was set because we know better who's logged in
        newAnnotation.setAuthor(getXWikiUser());
        // and update
        annotationService.updateAnnotation(documentName, newAnnotation);
        // and then return the annotated content, as specified by the annotation request
        AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, updateRequest);
        return response;
    } catch (XWikiException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    } catch (AnnotationServiceException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) DocumentReference(org.xwiki.model.reference.DocumentReference) Annotation(org.xwiki.annotation.Annotation) XWikiException(com.xpn.xwiki.XWikiException) PUT(javax.ws.rs.PUT)

Example 5 with AnnotationServiceException

use of org.xwiki.annotation.AnnotationServiceException in project xwiki-platform by xwiki.

the class SingleAnnotationRESTResource method doDelete.

/**
 * Deletes the specified annotation.
 *
 * @param space the space of the document to delete the annotation from
 * @param page the name of the document to delete the annotation from
 * @param wiki the wiki of the document to delete the annotation from
 * @param id the id of the annotation to delete
 * @param request the annotation request to configure the returned annotated content after the execution of the
 *            operation
 * @return a annotation response for which the response code will be 0 in case of success and non-zero otherwise
 * @throws XWikiRestException when failing to parse space
 */
@DELETE
public AnnotationResponse doDelete(@PathParam("spaceName") String space, @PathParam("pageName") String page, @PathParam("wikiName") String wiki, @PathParam("id") String id, AnnotationRequest request) 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.canEditAnnotation(id, documentName, getXWikiUser())) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        // remove the annotation
        annotationService.removeAnnotation(documentName, id);
        // and then return the annotated content, as specified by the annotation request
        AnnotationResponse response = getSuccessResponseWithAnnotatedContent(documentName, request);
        return response;
    } catch (XWikiException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    } catch (AnnotationServiceException e) {
        getLogger().error(e.getMessage(), e);
        return getErrorResponse(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) AnnotationServiceException(org.xwiki.annotation.AnnotationServiceException) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) DELETE(javax.ws.rs.DELETE)

Aggregations

AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)6 XWikiException (com.xpn.xwiki.XWikiException)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 Annotation (org.xwiki.annotation.Annotation)2 IOServiceException (org.xwiki.annotation.io.IOServiceException)2 AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Map (java.util.Map)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 Request (org.restlet.Request)1 Form (org.restlet.data.Form)1 AnnotationPrintRenderer (org.xwiki.annotation.renderer.AnnotationPrintRenderer)1 AnnotationAddRequest (org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest)1 AnnotationFieldCollection (org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection)1