Search in sources :

Example 21 with Property

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

the class ObjectPropertyResourceImpl method getObjectProperty.

@Override
public Property getObjectProperty(String wikiName, String spaceName, String pageName, String className, Integer objectNumber, String propertyName, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        com.xpn.xwiki.objects.BaseObject baseObject = xwikiDocument.getObject(className, objectNumber);
        if (baseObject == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
        for (Property property : object.getProperties()) {
            if (property.getName().equals(propertyName)) {
                String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), object.getClassName(), object.getNumber()).toString();
                Link objectLink = objectFactory.createLink();
                objectLink.setHref(objectUri);
                objectLink.setRel(Relations.OBJECT);
                property.getLinks().add(objectLink);
                return property;
            }
        }
        throw new WebApplicationException(Status.NOT_FOUND);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 22 with Property

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

the class ObjectPropertyResourceImpl method updateObjectProperty.

@Override
public Response updateObjectProperty(String wikiName, String spaceName, String pageName, String className, Integer objectNumber, String propertyName, Boolean minorRevision, Property property) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        com.xpn.xwiki.objects.BaseObject baseObject = xwikiDocument.getObject(className, objectNumber);
        if (baseObject == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        baseObject.set(propertyName, property.getValue(), Utils.getXWikiContext(componentManager));
        doc.save("", Boolean.TRUE.equals(minorRevision));
        baseObject = xwikiDocument.getObject(className, objectNumber);
        Object object = DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, baseObject, false, Utils.getXWikiApi(componentManager), false);
        for (Property p : object.getProperties()) {
            if (p.getName().equals(propertyName)) {
                return Response.status(Status.ACCEPTED).entity(p).build();
            }
        }
        throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Object(org.xwiki.rest.model.jaxb.Object) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Property(org.xwiki.rest.model.jaxb.Property) XWikiException(com.xpn.xwiki.XWikiException)

Example 23 with Property

use of org.xwiki.rest.model.jaxb.Property 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)

Aggregations

Property (org.xwiki.rest.model.jaxb.Property)23 Object (org.xwiki.rest.model.jaxb.Object)15 Test (org.junit.Test)10 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)10 Link (org.xwiki.rest.model.jaxb.Link)9 GetMethod (org.apache.commons.httpclient.methods.GetMethod)8 XWikiException (com.xpn.xwiki.XWikiException)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 Page (org.xwiki.rest.model.jaxb.Page)6 Document (com.xpn.xwiki.api.Document)5 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWikiRestException (org.xwiki.rest.XWikiRestException)5 ObjectResource (org.xwiki.rest.resources.objects.ObjectResource)5 PostMethod (org.apache.commons.httpclient.methods.PostMethod)4 PutMethod (org.apache.commons.httpclient.methods.PutMethod)4 ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)4 Objects (org.xwiki.rest.model.jaxb.Objects)4 BaseObject (com.xpn.xwiki.objects.BaseObject)3 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3