Search in sources :

Example 21 with Object

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

the class ObjectsResourceTest method testPOSTInvalidObject.

@Test
public void testPOSTInvalidObject() throws Exception {
    final String TAG_VALUE = "TAG";
    Property property = new Property();
    property.setName("tags");
    property.setValue(TAG_VALUE);
    Object object = objectFactory.createObject();
    object.getProperties().add(property);
    PostMethod postMethod = executePostXml(buildURI(ObjectsResource.class, getWiki(), this.spaces, this.pageName).toString(), object, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    Assert.assertEquals(getHttpMethodInfo(postMethod), HttpStatus.SC_BAD_REQUEST, postMethod.getStatusCode());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) Object(org.xwiki.rest.model.jaxb.Object) Property(org.xwiki.rest.model.jaxb.Property) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 22 with Object

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

the class PageResourceTest method testPUTGETWithObject.

@Test
public void testPUTGETWithObject() throws Exception {
    String pageURI = buildURI(PageResource.class, getWiki(), Arrays.asList("RESTTest"), "PageWithObject");
    final String title = String.format("Title (%s)", UUID.randomUUID().toString());
    final String content = String.format("This is a content (%d)", System.currentTimeMillis());
    final String comment = String.format("Updated title and content (%d)", System.currentTimeMillis());
    Page newPage = this.objectFactory.createPage();
    newPage.setTitle(title);
    newPage.setContent(content);
    newPage.setComment(comment);
    // Add object
    final String TAG_VALUE = "TAG";
    Property property = new Property();
    property.setName("tags");
    property.setValue(TAG_VALUE);
    Object object = objectFactory.createObject();
    object.setClassName("XWiki.TagClass");
    object.getProperties().add(property);
    newPage.setObjects(objectFactory.createObjects());
    newPage.getObjects().getObjectSummaries().add(object);
    // PUT
    PutMethod putMethod = executePutXml(pageURI, newPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED, HttpStatus.SC_CREATED)));
    Page modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    // GET
    GetMethod getMethod = executeGet(pageURI + "?objects=true");
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    Assert.assertEquals(TAG_VALUE, getProperty((Object) modifiedPage.getObjects().getObjectSummaries().get(0), "tags").getValue());
    // Send again but with empty object list
    modifiedPage.getObjects().getObjectSummaries().clear();
    // PUT
    putMethod = executePutXml(pageURI, modifiedPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword());
    assertThat(getHttpMethodInfo(putMethod), putMethod.getStatusCode(), isIn(Arrays.asList(HttpStatus.SC_ACCEPTED)));
    modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    // GET
    getMethod = executeGet(pageURI + "?objects=true");
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(title, modifiedPage.getTitle());
    Assert.assertEquals(content, modifiedPage.getContent());
    Assert.assertEquals(comment, modifiedPage.getComment());
    Assert.assertTrue(modifiedPage.getObjects().getObjectSummaries().isEmpty());
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) Object(org.xwiki.rest.model.jaxb.Object) Property(org.xwiki.rest.model.jaxb.Property) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 23 with Object

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

the class ObjectPropertiesAtPageVersionResourceImpl method getObjectProperties.

@Override
public Properties getObjectProperties(String wikiName, String spaceName, String pageName, String version, String className, Integer objectNumber, 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);
        Properties properties = objectFactory.createProperties();
        properties.getProperties().addAll(object.getProperties());
        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);
        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) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Properties(org.xwiki.rest.model.jaxb.Properties) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Object(org.xwiki.rest.model.jaxb.Object) ObjectAtPageVersionResource(org.xwiki.rest.resources.objects.ObjectAtPageVersionResource) Link(org.xwiki.rest.model.jaxb.Link) XWikiException(com.xpn.xwiki.XWikiException)

Example 24 with Object

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

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

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