Search in sources :

Example 11 with ObjectFactory

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

the class FormUrlEncodedPropertyReader method readFrom.

@Override
public Property readFrom(Class<Property> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Property property = objectFactory.createProperty();
    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()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        Enumeration<String> names = httpServletRequest.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            if (name.startsWith(PROPERTY_PREFIX)) {
                property.setName(name.replace(PROPERTY_PREFIX, ""));
                property.setValue(httpServletRequest.getParameter(name));
                break;
            }
        }
    } else {
        for (String name : form.getNames()) if (name.startsWith(PROPERTY_PREFIX)) {
            property.setName(name.replace(PROPERTY_PREFIX, ""));
            property.setValue(form.getFirstValue(name));
            break;
        }
    }
    return property;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) Property(org.xwiki.rest.model.jaxb.Property)

Example 12 with ObjectFactory

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

the class FormUrlEncodedPageReader method readFrom.

@Override
public Page readFrom(Class<Page> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Page page = objectFactory.createPage();
    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()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        page.setTitle(httpServletRequest.getParameter(TITLE_FIELD_NAME));
        page.setParent(httpServletRequest.getParameter(PARENT_FIELD_NAME));
        page.setHidden(Boolean.valueOf(httpServletRequest.getParameter(HIDDEN_FIELD_NAME)));
        page.setContent(httpServletRequest.getParameter(CONTENT_FIELD_NAME));
    } else {
        page.setTitle(form.getFirstValue(TITLE_FIELD_NAME));
        page.setParent(form.getFirstValue(PARENT_FIELD_NAME));
        page.setHidden(Boolean.valueOf(form.getFirstValue(HIDDEN_FIELD_NAME)));
        page.setContent(form.getFirstValue(CONTENT_FIELD_NAME));
    }
    return page;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) Page(org.xwiki.rest.model.jaxb.Page) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation)

Aggregations

ObjectFactory (org.xwiki.rest.model.jaxb.ObjectFactory)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Form (org.restlet.data.Form)5 InputRepresentation (org.restlet.representation.InputRepresentation)5 Representation (org.restlet.representation.Representation)5 Page (org.xwiki.rest.model.jaxb.Page)3 Property (org.xwiki.rest.model.jaxb.Property)3 Random (java.util.Random)2 JAXBContext (javax.xml.bind.JAXBContext)2 Before (org.junit.Before)2 Comment (org.xwiki.rest.model.jaxb.Comment)2 Tag (org.xwiki.rest.model.jaxb.Tag)2 Tags (org.xwiki.rest.model.jaxb.Tags)2 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 SolrTestUtils (org.xwiki.repository.test.SolrTestUtils)1 Object (org.xwiki.rest.model.jaxb.Object)1 Objects (org.xwiki.rest.model.jaxb.Objects)1