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());
}
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());
}
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);
}
}
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);
}
}
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);
}
}
Aggregations