use of org.xwiki.annotation.rest.model.jaxb.ObjectFactory 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;
}
use of org.xwiki.annotation.rest.model.jaxb.ObjectFactory 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.ObjectFactory 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.ObjectFactory in project xwiki-platform by xwiki.
the class AbstractFormUrlEncodedAnnotationRequestReader method readFrom.
@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
ObjectFactory objectFactory = new ObjectFactory();
T annotationRequest = getReadObjectInstance(objectFactory);
Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
Form form = new Form(representation);
/*
* If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
* read data using getParameter()
*/
if (!form.getNames().isEmpty()) {
for (String paramName : form.getNames()) {
for (String paramValue : form.getValuesArray(paramName)) {
saveField(annotationRequest, paramName, paramValue, objectFactory);
}
}
} else {
HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
for (Map.Entry<String, String[]> entry : httpServletRequest.getParameterMap().entrySet()) {
// skip method & media parameters, used by REST to carry its own parameters
if ("method".equals(entry.getKey()) || "media".equals(entry.getKey())) {
continue;
}
// save all the values of this field, one by one
String[] paramValues = entry.getValue();
for (String value : paramValues) {
saveField(annotationRequest, entry.getKey(), value, objectFactory);
}
}
}
return annotationRequest;
}
Aggregations