Search in sources :

Example 1 with ObjectFactory

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;
}
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 ObjectFactory

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;
}
Also used : AnnotatedContent(org.xwiki.annotation.rest.model.jaxb.AnnotatedContent) ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) Annotation(org.xwiki.annotation.Annotation)

Example 3 with ObjectFactory

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;
}
Also used : ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) StringWriter(java.io.StringWriter) AnnotationResponse(org.xwiki.annotation.rest.model.jaxb.AnnotationResponse) PrintWriter(java.io.PrintWriter)

Example 4 with ObjectFactory

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Aggregations

ObjectFactory (org.xwiki.annotation.rest.model.jaxb.ObjectFactory)4 Annotation (org.xwiki.annotation.Annotation)2 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Form (org.restlet.data.Form)1 InputRepresentation (org.restlet.representation.InputRepresentation)1 Representation (org.restlet.representation.Representation)1 AnnotatedContent (org.xwiki.annotation.rest.model.jaxb.AnnotatedContent)1 AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)1 AnnotationStub (org.xwiki.annotation.rest.model.jaxb.AnnotationStub)1