Search in sources :

Example 1 with Property

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

the class ModelFactory method toRestClass.

public Class toRestClass(URI baseUri, com.xpn.xwiki.api.Class xwikiClass) {
    Class clazz = this.objectFactory.createClass();
    clazz.setId(xwikiClass.getName());
    clazz.setName(xwikiClass.getName());
    DocumentReference reference = xwikiClass.getReference();
    String wikiName = reference.getWikiReference().getName();
    for (java.lang.Object xwikiPropertyClassObject : xwikiClass.getProperties()) {
        PropertyClass xwikiPropertyClass = (PropertyClass) xwikiPropertyClassObject;
        Property property = this.objectFactory.createProperty();
        property.setName(xwikiPropertyClass.getName());
        property.setType(xwikiPropertyClass.getxWikiClass().getName());
        for (java.lang.Object xwikiPropertyObject : xwikiPropertyClass.getProperties()) {
            com.xpn.xwiki.api.Property xwikiProperty = (com.xpn.xwiki.api.Property) xwikiPropertyObject;
            java.lang.Object value = xwikiProperty.getValue();
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(xwikiProperty.getName());
            if (value != null) {
                attribute.setValue(value.toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        String propertyUri = Utils.createURI(baseUri, ClassPropertyResource.class, wikiName, xwikiClass.getName(), xwikiPropertyClass.getName()).toString();
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        clazz.getProperties().add(property);
    }
    String classUri = Utils.createURI(baseUri, ClassResource.class, wikiName, xwikiClass.getName()).toString();
    Link classLink = this.objectFactory.createLink();
    classLink.setHref(classUri);
    classLink.setRel(Relations.SELF);
    clazz.getLinks().add(classLink);
    String propertiesUri = Utils.createURI(baseUri, ClassPropertiesResource.class, wikiName, xwikiClass.getName()).toString();
    Link propertyLink = this.objectFactory.createLink();
    propertyLink.setHref(propertiesUri);
    propertyLink.setRel(Relations.PROPERTIES);
    clazz.getLinks().add(propertyLink);
    String objectsUri = Utils.createURI(baseUri, AllObjectsForClassNameResource.class, wikiName, xwikiClass.getName()).toString();
    Link objectsLink = this.objectFactory.createLink();
    objectsLink.setHref(objectsUri);
    objectsLink.setRel(Relations.OBJECTS);
    clazz.getLinks().add(objectsLink);
    return clazz;
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) Attribute(org.xwiki.rest.model.jaxb.Attribute) ClassPropertyResource(org.xwiki.rest.resources.classes.ClassPropertyResource) PropertyClass(com.xpn.xwiki.api.PropertyClass) ClassPropertiesResource(org.xwiki.rest.resources.classes.ClassPropertiesResource) AllObjectsForClassNameResource(org.xwiki.rest.resources.objects.AllObjectsForClassNameResource) ListClass(com.xpn.xwiki.objects.classes.ListClass) PropertyClass(com.xpn.xwiki.api.PropertyClass) Class(org.xwiki.rest.model.jaxb.Class) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Example 2 with Property

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

the class ModelFactory method toRestObject.

public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
    Object object = this.objectFactory.createObject();
    fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
    XWikiContext xwikiContext = this.xcontextProvider.get();
    BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
    for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
        com.xpn.xwiki.objects.classes.PropertyClass propertyClass = (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
        Property property = this.objectFactory.createProperty();
        for (java.lang.Object o : propertyClass.getProperties()) {
            BaseProperty baseProperty = (BaseProperty) o;
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(baseProperty.getName());
            /* Check for null values in order to prevent NPEs */
            if (baseProperty.getValue() != null) {
                attribute.setValue(baseProperty.getValue().toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        if (propertyClass instanceof ListClass) {
            ListClass listClass = (ListClass) propertyClass;
            List allowedValueList = listClass.getList(xwikiContext);
            if (!allowedValueList.isEmpty()) {
                Formatter f = new Formatter();
                for (int i = 0; i < allowedValueList.size(); i++) {
                    if (i != allowedValueList.size() - 1) {
                        f.format("%s,", allowedValueList.get(i).toString());
                    } else {
                        f.format("%s", allowedValueList.get(i).toString());
                    }
                }
                Attribute attribute = this.objectFactory.createAttribute();
                attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
                attribute.setValue(f.toString());
                property.getAttributes().add(attribute);
            }
        }
        property.setName(propertyClass.getName());
        property.setType(propertyClass.getClassType());
        try {
            property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
        } catch (XWikiException e) {
        // Should never happen
        }
        String propertyUri;
        if (useVersion) {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        } else {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        }
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        object.getProperties().add(property);
    }
    Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
    object.getLinks().add(objectLink);
    return object;
}
Also used : Attribute(org.xwiki.rest.model.jaxb.Attribute) Formatter(java.util.Formatter) XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.api.PropertyClass) ListClass(com.xpn.xwiki.objects.classes.ListClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) Object(org.xwiki.rest.model.jaxb.Object) List(java.util.List) ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException) Link(org.xwiki.rest.model.jaxb.Link)

Example 3 with Property

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

the class ClassPropertyResourceImpl method getClassProperty.

@Override
public Property getClassProperty(String wikiName, String className, String propertyName) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        com.xpn.xwiki.api.Class xwikiClass = Utils.getXWikiApi(componentManager).getClass(className);
        if (xwikiClass == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Class clazz = DomainObjectFactory.createClass(objectFactory, uriInfo.getBaseUri(), wikiName, xwikiClass);
        for (Property property : clazz.getProperties()) {
            if (property.getName().equals(propertyName)) {
                String classUri = Utils.createURI(uriInfo.getBaseUri(), ClassResource.class, wikiName, xwikiClass.getName()).toString();
                Link classLink = objectFactory.createLink();
                classLink.setHref(classUri);
                classLink.setRel(Relations.CLASS);
                property.getLinks().add(classLink);
                return property;
            }
        }
        throw new WebApplicationException(Status.NOT_FOUND);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Class(org.xwiki.rest.model.jaxb.Class) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with Property

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

the class CurrentUserPropertyResourceImpl method buildResponse.

private Response buildResponse(XWikiDocument document, String propertyName, XWikiContext xcontext) {
    BaseObject baseObject = document.getXObject(USER_REFERENCE);
    Object object = this.factory.toRestObject(this.uriInfo.getBaseUri(), new Document(document, xcontext), baseObject, false, 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);
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BaseObject(com.xpn.xwiki.objects.BaseObject) 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) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with Property

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

the class ObjectsResourceImpl method addObject.

@Override
public Response addObject(String wikiName, String spaceName, String pageName, Boolean minorRevision, Object object) throws XWikiRestException {
    if (object.getClassName() == null) {
        throw new WebApplicationException(Status.BAD_REQUEST);
    }
    try {
        List<String> spaces = parseSpaceSegments(spaceName);
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, 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));
        BaseObject xwikiObject = xwikiDocument.newObject(object.getClassName(), Utils.getXWikiContext(componentManager));
        if (xwikiObject == null) {
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        // We must initialize all the fields to an empty value in order to correctly create the object
        BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
        for (java.lang.Object propertyNameObject : xwikiClass.getPropertyNames()) {
            String propertyName = (String) propertyNameObject;
            xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
        }
        for (Property property : object.getProperties()) {
            xwikiObject.set(property.getName(), property.getValue(), Utils.getXWikiContext(componentManager));
        }
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.created(Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, wikiName, spaces, pageName, object.getClassName(), xwikiObject.getNumber())).entity(DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), false)).build();
    } 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) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Property(org.xwiki.rest.model.jaxb.Property) XWikiException(com.xpn.xwiki.XWikiException)

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