Search in sources :

Example 1 with ObjectSummary

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

the class ModelFactory method toRestObjectSummary.

public ObjectSummary toRestObjectSummary(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
    ObjectSummary objectSummary = objectFactory.createObjectSummary();
    fillObjectSummary(objectSummary, doc, xwikiObject, withPrettyNames);
    Link objectLink = getObjectLink(objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.OBJECT);
    objectSummary.getLinks().add(objectLink);
    String propertiesUri;
    if (useVersion) {
        propertiesUri = Utils.createURI(baseUri, ObjectPropertiesAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
    } else {
        propertiesUri = Utils.createURI(baseUri, ObjectPropertiesResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
    }
    Link propertyLink = objectFactory.createLink();
    propertyLink.setHref(propertiesUri);
    propertyLink.setRel(Relations.PROPERTIES);
    objectSummary.getLinks().add(propertyLink);
    return objectSummary;
}
Also used : ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Link(org.xwiki.rest.model.jaxb.Link)

Example 2 with ObjectSummary

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

the class AllObjectsForClassNameResourceImpl method getObjects.

@Override
public Objects getObjects(String wikiName, String className, Integer start, Integer number, String order, Boolean withPrettyNames) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    try {
        Objects objects = new Objects();
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        String query = "select doc, obj from BaseObject as obj, XWikiDocument as doc where obj.name=doc.fullName and obj.className=:className";
        if ("date".equals(order)) {
            query += " order by doc.date desc";
        }
        List<Object> queryResult = null;
        queryResult = queryManager.createQuery(query, Query.XWQL).bindValue("className", className).setLimit(number).setOffset(start).execute();
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            XWikiDocument xwikiDocument = (XWikiDocument) fields[0];
            xwikiDocument.setDatabase(wikiName);
            Document doc = new Document(xwikiDocument, Utils.getXWikiContext(componentManager));
            BaseObject xwikiObject = (BaseObject) fields[1];
            ObjectSummary objectSummary = DomainObjectFactory.createObjectSummary(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), withPrettyNames);
            objects.getObjectSummaries().add(objectSummary);
        }
        return objects;
    } catch (Exception e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRestException(org.xwiki.rest.XWikiRestException) Objects(org.xwiki.rest.model.jaxb.Objects) BaseObject(com.xpn.xwiki.objects.BaseObject) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) XWikiRestException(org.xwiki.rest.XWikiRestException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 3 with ObjectSummary

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

the class ObjectsResourceTest method testPUTPropertyFormUrlEncoded.

@Test
public void testPUTPropertyFormUrlEncoded() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();
    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);
    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertFalse(objects.getObjectSummaries().isEmpty());
    Object currentObject = null;
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }
    Assert.assertNotNull(currentObject);
    Property tagsProperty = getProperty(currentObject, "tags");
    Assert.assertNotNull(tagsProperty);
    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
    Assert.assertNotNull(tagsPropertyLink);
    NameValuePair[] nameValuePairs = new NameValuePair[1];
    nameValuePairs[0] = new NameValuePair("property#tags", TAG_VALUE);
    PostMethod postMethod = executePostForm(String.format("%s?method=PUT", tagsPropertyLink.getHref()), nameValuePairs, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_ACCEPTED, postMethod.getStatusCode());
    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    tagsProperty = getProperty(currentObject, "tags");
    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 4 with ObjectSummary

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

the class ObjectsResourceTest method testPUTProperty.

@Test
public void testPUTProperty() throws Exception {
    final String TAG_VALUE = UUID.randomUUID().toString();
    /* Make sure that an Object with the TagClass exists. */
    createObjectIfDoesNotExists("XWiki.TagClass", this.spaces, this.pageName);
    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Link link = getFirstLinkByRelation(page, Relations.OBJECTS);
    Assert.assertNotNull(link);
    getMethod = executeGet(link.getHref());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertFalse(objects.getObjectSummaries().isEmpty());
    Object currentObject = null;
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals("XWiki.TagClass")) {
            link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
            currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            break;
        }
    }
    Assert.assertNotNull(currentObject);
    Property tagsProperty = getProperty(currentObject, "tags");
    Assert.assertNotNull(tagsProperty);
    Link tagsPropertyLink = getFirstLinkByRelation(tagsProperty, Relations.SELF);
    Assert.assertNotNull(tagsPropertyLink);
    Property newTags = objectFactory.createProperty();
    newTags.setValue(TAG_VALUE);
    PutMethod putMethod = executePutXml(tagsPropertyLink.getHref(), newTags, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_ACCEPTED, putMethod.getStatusCode());
    getMethod = executeGet(buildURI(ObjectResource.class, getWiki(), this.spaces, this.pageName, currentObject.getClassName(), currentObject.getNumber()).toString());
    Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode());
    currentObject = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    tagsProperty = getProperty(currentObject, "tags");
    Assert.assertEquals(TAG_VALUE, tagsProperty.getValue());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Property(org.xwiki.rest.model.jaxb.Property) Link(org.xwiki.rest.model.jaxb.Link) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 5 with ObjectSummary

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

the class ObjectsResourceTest method createObjectIfDoesNotExists.

private Object createObjectIfDoesNotExists(String className, List<String> spaces, String pageName) throws Exception {
    createPageIfDoesntExist(spaces, pageName, "");
    GetMethod getMethod = executeGet(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Objects objects = (Objects) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    for (ObjectSummary objectSummary : objects.getObjectSummaries()) {
        if (objectSummary.getClassName().equals(className)) {
            Link link = getFirstLinkByRelation(objectSummary, Relations.OBJECT);
            Assert.assertNotNull(link);
            getMethod = executeGet(link.getHref());
            Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
            Object object = (Object) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
            return object;
        }
    }
    /* If no object of that class is found, then create a new one */
    Object object = objectFactory.createObject();
    object.setClassName(className);
    PostMethod postMethod = executePostXml(buildURI(ObjectsResource.class, getWiki(), spaces, pageName).toString(), object, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_CREATED, postMethod.getStatusCode());
    object = (Object) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream());
    return object;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Objects(org.xwiki.rest.model.jaxb.Objects) Object(org.xwiki.rest.model.jaxb.Object) ObjectSummary(org.xwiki.rest.model.jaxb.ObjectSummary) Link(org.xwiki.rest.model.jaxb.Link)

Aggregations

ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)8 Link (org.xwiki.rest.model.jaxb.Link)6 Object (org.xwiki.rest.model.jaxb.Object)6 Objects (org.xwiki.rest.model.jaxb.Objects)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)4 Page (org.xwiki.rest.model.jaxb.Page)4 Property (org.xwiki.rest.model.jaxb.Property)4 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)4 ObjectResource (org.xwiki.rest.resources.objects.ObjectResource)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 Document (com.xpn.xwiki.api.Document)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NameValuePair (org.apache.commons.httpclient.NameValuePair)1 ObjectReference (org.xwiki.model.reference.ObjectReference)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1