use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class DefaultIOService method getAnnotation.
@Override
public Annotation getAnnotation(String target, String annotationID) throws IOServiceException {
try {
if (annotationID == null || target == null) {
return null;
}
// parse the target and extract the local reference serialized from it, by the same rules
EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
// build the target identifier for the annotation
String localTargetId = target;
// and the name of the document where it should be stored
String docName = target;
if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
localTargetId = localSerializer.serialize(targetReference);
docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
}
// get the document
XWikiContext deprecatedContext = getXWikiContext();
XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
// and the annotation class objects in it
// parse the annotation id as object index
BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), Integer.valueOf(annotationID.toString()));
if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
return null;
}
// use the object number as annotation id
return loadAnnotationFromObject(object, deprecatedContext);
} catch (NumberFormatException e) {
throw new IOServiceException("Could not parse annotation id " + annotationID, e);
} catch (XWikiException e) {
throw new IOServiceException("An exception has occurred while loading the annotation with id " + annotationID, e);
}
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class DefaultIOService method loadAnnotationFromObject.
/**
* Helper function to load an annotation object from an xwiki object.
*
* @param object the xwiki object to load an annotation from
* @param deprecatedContext XWikiContext to make operations on xwiki data
* @return the Annotation instance for the annotation stored in BaseObject
*/
protected Annotation loadAnnotationFromObject(BaseObject object, XWikiContext deprecatedContext) {
// load the annotation with its ID, special handling of the state since it needs deserialization, special
// handling of the original selection which shouldn't be set if it's empty
Annotation annotation = new Annotation(object.getNumber() + "");
annotation.setState(AnnotationState.valueOf(object.getStringValue(Annotation.STATE_FIELD)));
String originalSelection = object.getStringValue(Annotation.ORIGINAL_SELECTION_FIELD);
if (originalSelection != null && originalSelection.length() > 0) {
annotation.setOriginalSelection(originalSelection);
}
Collection<String> skippedFields = Arrays.asList(new String[] { Annotation.ORIGINAL_SELECTION_FIELD, Annotation.STATE_FIELD });
// get all the props, filter those that need to be skipped and save the rest
for (String propName : object.getPropertyNames()) {
if (!skippedFields.contains(propName)) {
try {
annotation.set(propName, ((BaseProperty) object.get(propName)).getValue());
} catch (XWikiException e) {
this.logger.warn("Unable to get property " + propName + " from object " + object.getClassName() + "[" + object.getNumber() + "]. Will not be saved in the annotation.", e);
}
}
}
return annotation;
}
use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.
the class DefaultIOService method updateAnnotations.
/**
* {@inheritDoc}
* <p>
* Implementation which gets all the annotation class objects in the document pointed by the target, and matches
* their ids against the ids in the passed collection of annotations. If they match, they are updated with the new
* data in the annotations in annotation.
* </p>
*
* @see org.xwiki.annotation.io.IOService#updateAnnotations(String, java.util.Collection)
*/
@Override
public void updateAnnotations(String target, Collection<Annotation> annotations) throws IOServiceException {
try {
EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
// get the document name from the parsed reference
String docName = target;
if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
}
// get the document pointed to by the target
XWikiContext deprecatedContext = getXWikiContext();
XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
List<String> updateNotifs = new ArrayList<String>();
boolean updated = false;
for (Annotation annotation : annotations) {
// parse annotation id as string. If cannot parse, then ignore annotation, is not valid
int annId = 0;
try {
annId = Integer.parseInt(annotation.getId());
} catch (NumberFormatException e) {
continue;
}
BaseObject object = document.getXObject(configuration.getAnnotationClassReference(), annId);
if (object == null) {
continue;
}
updated = updateObject(object, annotation, deprecatedContext) || updated;
updateNotifs.add(annotation.getId());
}
if (updated) {
// set the author of the document to the current user
document.setAuthor(deprecatedContext.getUser());
deprecatedContext.getWiki().saveDocument(document, "Updated annotations", deprecatedContext);
}
} catch (XWikiException e) {
throw new IOServiceException("An exception has occurred while updating the annotation", e);
}
}
use of com.xpn.xwiki.XWikiException 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 com.xpn.xwiki.XWikiException 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();
}
}
Aggregations