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