use of org.xwiki.annotation.rest.model.jaxb.AnnotationResponse in project xwiki-platform by xwiki.
the class AbstractAnnotationRESTResource method getSuccessResponseWithAnnotatedContent.
/**
* Builds an annotation response containing the annotated content along with the annotation stubs, according to the
* requirements in the passed annotations request.
*
* @param request the annotations request
* @param documentName the name of the document to provide an annotated response for
* @return an annotation response with the annotated content and the annotation stubs
* @throws AnnotationServiceException in case something goes wrong handling the annotations
* @throws XWikiException in case something goes wrong manipulating the xwiki context & documents
*/
protected AnnotationResponse getSuccessResponseWithAnnotatedContent(String documentName, AnnotationRequest request) throws AnnotationServiceException, XWikiException {
ObjectFactory factory = new ObjectFactory();
AnnotationResponse response = factory.createAnnotationResponse();
// get the annotations on this content
Collection<Annotation> annotations = annotationService.getAnnotations(documentName);
// filter them according to the request
Collection<Annotation> filteredAnnotations = filterAnnotations(annotations, request);
// render the document with the filtered annotations on it
String renderedHTML = renderDocumentWithAnnotations(documentName, null, DEFAULT_ACTION, filteredAnnotations);
// prepare the annotated content
AnnotatedContent annotatedContentResponse = factory.createAnnotatedContent();
annotatedContentResponse.getAnnotations().addAll(prepareAnnotationStubsSet(filteredAnnotations, request.getRequest().getFields()));
annotatedContentResponse.setContent(renderedHTML);
// set the annotated content along with the return code in the response and return it
response.setAnnotatedContent(annotatedContentResponse);
response.setResponseCode(0);
return response;
}
use of org.xwiki.annotation.rest.model.jaxb.AnnotationResponse 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);
}
}
use of org.xwiki.annotation.rest.model.jaxb.AnnotationResponse 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();
}
}
use of org.xwiki.annotation.rest.model.jaxb.AnnotationResponse in project xwiki-platform by xwiki.
the class AbstractAnnotationRESTResource method getErrorResponse.
/**
* Helper function to create an error response from a passed exception. <br>
*
* @param exception the exception that was encountered during regular execution of service
* @return an error response
*/
protected AnnotationResponse getErrorResponse(Throwable exception) {
AnnotationResponse result = new ObjectFactory().createAnnotationResponse();
result.setResponseCode(1);
String responseMessage = exception.getMessage();
if (responseMessage == null) {
// serialize the stack trace and send it as an error response
StringWriter stackTraceWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTraceWriter));
responseMessage = stackTraceWriter.toString();
}
result.setResponseMessage(responseMessage);
result.setAnnotatedContent(null);
return result;
}
use of org.xwiki.annotation.rest.model.jaxb.AnnotationResponse 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);
}
}
Aggregations