Search in sources :

Example 1 with AnnotationField

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

the class AbstractAnnotationRESTResource method prepareAnnotationStubsSet.

/**
 * Helper function to translate a collection of annotations from the {@link Annotation} model to the JAXB model to
 * be serialized for REST communication.
 *
 * @param annotations the annotations collection to be translated
 * @param requestedFields the extra parameters that should be set for the prepared annotations
 * @return translate set of org.xwiki.annotation.internal.annotation.Annotation to set of
 *         org.xwiki.annotation.internal.annotation.Annotation
 */
private Collection<AnnotationStub> prepareAnnotationStubsSet(Collection<Annotation> annotations, List<String> requestedFields) {
    ObjectFactory factory = new ObjectFactory();
    List<AnnotationStub> set = new ArrayList<AnnotationStub>();
    for (Annotation xwikiAnnotation : annotations) {
        AnnotationStub annotation = factory.createAnnotationStub();
        annotation.setAnnotationId(xwikiAnnotation.getId());
        annotation.setState(xwikiAnnotation.getState().toString());
        // for all the requested extra fields, get them from the annotation and send them
        for (String extraField : requestedFields) {
            Object value = xwikiAnnotation.get(extraField);
            AnnotationField field = new AnnotationField();
            field.setName(extraField);
            // value.toString() by default, null if value is missing
            field.setValue(value != null ? value.toString() : null);
            annotation.getFields().add(field);
        }
        set.add(annotation);
    }
    return set;
}
Also used : ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) ArrayList(java.util.ArrayList) AnnotationStub(org.xwiki.annotation.rest.model.jaxb.AnnotationStub) Annotation(org.xwiki.annotation.Annotation)

Example 2 with AnnotationField

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

the class AbstractAnnotationRESTResource method filterAnnotations.

/**
 * Helper method to filter a set of annotations according to the criteria in the passed annotation request. The
 * fields in the filter of the request will be interpreted as a filter for equality with the value in the actual
 * annotation, and all the fields conditions will be put together with an "or" operation.
 *
 * @param annotations the collection of annotations to filter
 * @param request the request according which to filter
 * @return the filtered collection of annotations
 */
protected Collection<Annotation> filterAnnotations(Collection<Annotation> annotations, AnnotationRequest request) {
    Collection<Annotation> result = new ArrayList<Annotation>();
    Map<String, List<String>> filters = new HashMap<String, List<String>>();
    for (AnnotationField annotationField : request.getFilter().getFields()) {
        String filterName = annotationField.getName();
        List<String> values = filters.get(filterName);
        if (values == null) {
            values = new ArrayList<String>();
            filters.put(filterName, values);
        }
        if (annotationField.getValue() != null) {
            values.add(annotationField.getValue());
        }
    }
    if (filters.size() == 0) {
        return annotations;
    }
    for (Annotation ann : annotations) {
        boolean matches = true;
        for (Map.Entry<String, List<String>> filter : filters.entrySet()) {
            Object annotationValue = ann.get(filter.getKey());
            // if the values is not set or is not among the requested values,
            if (annotationValue == null || !filter.getValue().contains(annotationValue.toString())) {
                // it doesn't match and exit
                matches = false;
                break;
            }
        }
        // if it matches in the end, add it to the results
        if (matches) {
            result.add(ann);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Annotation(org.xwiki.annotation.Annotation) AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with AnnotationField

use of org.xwiki.annotation.rest.model.jaxb.AnnotationField 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 4 with AnnotationField

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

the class AbstractFormUrlEncodedAnnotationUpdateRequestReader method saveField.

@Override
protected boolean saveField(T readObject, String key, String value, ObjectFactory objectFactory) {
    // first try to pass this through the super reader, to read the specific request fields
    boolean read = super.saveField(readObject, key, value, objectFactory);
    // if the field was not read then it's a custom field, store it as an annotation extra field
    if (!read) {
        // use xwiki convention that first value is always the one used and ignore a field if it has already been
        // read
        boolean contains = false;
        for (AnnotationField readField : readObject.getAnnotation().getFields()) {
            if (readField.getName().equals(key)) {
                contains = true;
                break;
            }
        }
        if (!contains) {
            AnnotationField extraField = objectFactory.createAnnotationField();
            extraField.setName(key);
            extraField.setValue(value);
            readObject.getAnnotation().getFields().add(extraField);
        }
    }
    // always return true since this will always be able to store the extra fields
    return true;
}
Also used : AnnotationField(org.xwiki.annotation.rest.model.jaxb.AnnotationField)

Example 5 with AnnotationField

use of org.xwiki.annotation.rest.model.jaxb.AnnotationField 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)

Aggregations

AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)6 ArrayList (java.util.ArrayList)3 Annotation (org.xwiki.annotation.Annotation)3 XWikiException (com.xpn.xwiki.XWikiException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)2 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Request (org.restlet.Request)1 Form (org.restlet.data.Form)1 AnnotationAddRequest (org.xwiki.annotation.rest.model.jaxb.AnnotationAddRequest)1 AnnotationFieldCollection (org.xwiki.annotation.rest.model.jaxb.AnnotationFieldCollection)1 AnnotationRequest (org.xwiki.annotation.rest.model.jaxb.AnnotationRequest)1 AnnotationStub (org.xwiki.annotation.rest.model.jaxb.AnnotationStub)1