Search in sources :

Example 1 with Object

use of org.xwiki.rest.model.jaxb.Object 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 2 with Object

use of org.xwiki.rest.model.jaxb.Object 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 3 with Object

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

the class ObjectPropertiesResourceImpl method getObjectProperties.

@Override
public Properties getObjectProperties(String wikiName, String spaceName, String pageName, String className, Integer objectNumber, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        com.xpn.xwiki.objects.BaseObject baseObject = getBaseObject(doc, 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);
        Properties properties = objectFactory.createProperties();
        properties.getProperties().addAll(object.getProperties());
        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);
        properties.getLinks().add(objectLink);
        return properties;
    } 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) Properties(org.xwiki.rest.model.jaxb.Properties) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with Object

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

the class ObjectPropertyAtPageVersionResourceImpl method getObjectProperty.

@Override
public Property getObjectProperty(String wikiName, String spaceName, String pageName, String version, String className, Integer objectNumber, String propertyName, Boolean withPrettyNames) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, version, true, false);
        Document doc = documentInfo.getDocument();
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        xwikiDocument = Utils.getXWiki(componentManager).getDocument(xwikiDocument, doc.getVersion(), 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, true, Utils.getXWikiApi(componentManager), withPrettyNames);
        for (Property property : object.getProperties()) {
            if (property.getName().equals(propertyName)) {
                String objectUri = Utils.createURI(uriInfo.getBaseUri(), ObjectAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), version, 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) ObjectAtPageVersionResource(org.xwiki.rest.resources.objects.ObjectAtPageVersionResource) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with Object

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

the class FormUrlEncodedObjectReader method readFrom.

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

Aggregations

Object (org.xwiki.rest.model.jaxb.Object)27 Test (org.junit.Test)16 GetMethod (org.apache.commons.httpclient.methods.GetMethod)15 Property (org.xwiki.rest.model.jaxb.Property)15 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)15 Link (org.xwiki.rest.model.jaxb.Link)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)8 Page (org.xwiki.rest.model.jaxb.Page)8 ObjectResource (org.xwiki.rest.resources.objects.ObjectResource)7 XWikiException (com.xpn.xwiki.XWikiException)6 Document (com.xpn.xwiki.api.Document)6 WebApplicationException (javax.ws.rs.WebApplicationException)6 PutMethod (org.apache.commons.httpclient.methods.PutMethod)6 ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)6 Objects (org.xwiki.rest.model.jaxb.Objects)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWikiRestException (org.xwiki.rest.XWikiRestException)5 BaseObject (com.xpn.xwiki.objects.BaseObject)3 NameValuePair (org.apache.commons.httpclient.NameValuePair)3 ArrayList (java.util.ArrayList)2