Search in sources :

Example 1 with Link

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

the class ModelFactory method toRestClass.

public Class toRestClass(URI baseUri, com.xpn.xwiki.api.Class xwikiClass) {
    Class clazz = this.objectFactory.createClass();
    clazz.setId(xwikiClass.getName());
    clazz.setName(xwikiClass.getName());
    DocumentReference reference = xwikiClass.getReference();
    String wikiName = reference.getWikiReference().getName();
    for (java.lang.Object xwikiPropertyClassObject : xwikiClass.getProperties()) {
        PropertyClass xwikiPropertyClass = (PropertyClass) xwikiPropertyClassObject;
        Property property = this.objectFactory.createProperty();
        property.setName(xwikiPropertyClass.getName());
        property.setType(xwikiPropertyClass.getxWikiClass().getName());
        for (java.lang.Object xwikiPropertyObject : xwikiPropertyClass.getProperties()) {
            com.xpn.xwiki.api.Property xwikiProperty = (com.xpn.xwiki.api.Property) xwikiPropertyObject;
            java.lang.Object value = xwikiProperty.getValue();
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(xwikiProperty.getName());
            if (value != null) {
                attribute.setValue(value.toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        String propertyUri = Utils.createURI(baseUri, ClassPropertyResource.class, wikiName, xwikiClass.getName(), xwikiPropertyClass.getName()).toString();
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        clazz.getProperties().add(property);
    }
    String classUri = Utils.createURI(baseUri, ClassResource.class, wikiName, xwikiClass.getName()).toString();
    Link classLink = this.objectFactory.createLink();
    classLink.setHref(classUri);
    classLink.setRel(Relations.SELF);
    clazz.getLinks().add(classLink);
    String propertiesUri = Utils.createURI(baseUri, ClassPropertiesResource.class, wikiName, xwikiClass.getName()).toString();
    Link propertyLink = this.objectFactory.createLink();
    propertyLink.setHref(propertiesUri);
    propertyLink.setRel(Relations.PROPERTIES);
    clazz.getLinks().add(propertyLink);
    String objectsUri = Utils.createURI(baseUri, AllObjectsForClassNameResource.class, wikiName, xwikiClass.getName()).toString();
    Link objectsLink = this.objectFactory.createLink();
    objectsLink.setHref(objectsUri);
    objectsLink.setRel(Relations.OBJECTS);
    clazz.getLinks().add(objectsLink);
    return clazz;
}
Also used : ClassResource(org.xwiki.rest.resources.classes.ClassResource) Attribute(org.xwiki.rest.model.jaxb.Attribute) ClassPropertyResource(org.xwiki.rest.resources.classes.ClassPropertyResource) PropertyClass(com.xpn.xwiki.api.PropertyClass) ClassPropertiesResource(org.xwiki.rest.resources.classes.ClassPropertiesResource) AllObjectsForClassNameResource(org.xwiki.rest.resources.objects.AllObjectsForClassNameResource) ListClass(com.xpn.xwiki.objects.classes.ListClass) PropertyClass(com.xpn.xwiki.api.PropertyClass) Class(org.xwiki.rest.model.jaxb.Class) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) Link(org.xwiki.rest.model.jaxb.Link)

Example 2 with Link

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

the class ModelFactory method getObjectLink.

private static Link getObjectLink(ObjectFactory objectFactory, URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, String relation) {
    String objectUri;
    if (useVersion) {
        objectUri = Utils.createURI(baseUri, ObjectAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
    } else {
        objectUri = Utils.createURI(baseUri, ObjectResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber()).toString();
    }
    Link objectLink = objectFactory.createLink();
    objectLink.setHref(objectUri);
    objectLink.setRel(relation);
    return objectLink;
}
Also used : Link(org.xwiki.rest.model.jaxb.Link)

Example 3 with Link

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

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

the class ModelFactory method toRestAttachment.

public Attachment toRestAttachment(URI baseUri, com.xpn.xwiki.api.Attachment xwikiAttachment, String xwikiRelativeUrl, String xwikiAbsoluteUrl, Boolean withPrettyNames, boolean versionURL) {
    Attachment attachment = this.objectFactory.createAttachment();
    Document doc = xwikiAttachment.getDocument();
    attachment.setId(String.format("%s@%s", doc.getPrefixedFullName(), xwikiAttachment.getFilename()));
    attachment.setName(xwikiAttachment.getFilename());
    attachment.setSize(xwikiAttachment.getFilesize());
    attachment.setVersion(xwikiAttachment.getVersion());
    attachment.setPageId(doc.getPrefixedFullName());
    attachment.setPageVersion(doc.getVersion());
    attachment.setMimeType(xwikiAttachment.getMimeType());
    attachment.setAuthor(xwikiAttachment.getAuthor());
    if (withPrettyNames) {
        XWikiContext xcontext = xcontextProvider.get();
        attachment.setAuthorName(xcontext.getWiki().getUserName(xwikiAttachment.getAuthor(), null, false, xcontext));
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(xwikiAttachment.getDate());
    attachment.setDate(calendar);
    attachment.setXwikiRelativeUrl(xwikiRelativeUrl);
    attachment.setXwikiAbsoluteUrl(xwikiAbsoluteUrl);
    String pageUri = Utils.createURI(baseUri, PageResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName()).toString();
    Link pageLink = objectFactory.createLink();
    pageLink.setHref(pageUri);
    pageLink.setRel(Relations.PAGE);
    attachment.getLinks().add(pageLink);
    String attachmentUri;
    if (versionURL) {
        attachmentUri = Utils.createURI(baseUri, AttachmentVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename(), xwikiAttachment.getVersion()).toString();
    } else {
        attachmentUri = Utils.createURI(baseUri, AttachmentResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiAttachment.getFilename()).toString();
    }
    Link attachmentLink = objectFactory.createLink();
    attachmentLink.setHref(attachmentUri);
    attachmentLink.setRel(Relations.ATTACHMENT_DATA);
    attachment.getLinks().add(attachmentLink);
    return attachment;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Calendar(java.util.Calendar) XWikiContext(com.xpn.xwiki.XWikiContext) Attachment(org.xwiki.rest.model.jaxb.Attachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Link(org.xwiki.rest.model.jaxb.Link)

Example 5 with Link

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

the class ModelFactory method toRestWiki.

// To REST
public Wiki toRestWiki(URI baseUri, String wikiName) {
    Wiki wiki = this.objectFactory.createWiki().withId(wikiName).withName(wikiName);
    String spacesUri = Utils.createURI(baseUri, SpacesResource.class, wikiName).toString();
    Link spacesLink = this.objectFactory.createLink();
    spacesLink.setHref(spacesUri);
    spacesLink.setRel(Relations.SPACES);
    wiki.getLinks().add(spacesLink);
    String classesUri = Utils.createURI(baseUri, ClassesResource.class, wikiName).toString();
    Link classesLink = this.objectFactory.createLink();
    classesLink.setHref(classesUri);
    classesLink.setRel(Relations.CLASSES);
    wiki.getLinks().add(classesLink);
    String modificationsUri = Utils.createURI(baseUri, ModificationsResource.class, wikiName).toString();
    Link modificationsLink = this.objectFactory.createLink();
    modificationsLink.setHref(modificationsUri);
    modificationsLink.setRel(Relations.MODIFICATIONS);
    wiki.getLinks().add(modificationsLink);
    String searchUri = Utils.createURI(baseUri, WikiSearchResource.class, wikiName).toString();
    Link searchLink = this.objectFactory.createLink();
    searchLink.setHref(searchUri);
    searchLink.setRel(Relations.SEARCH);
    wiki.getLinks().add(searchLink);
    String queryUri = Utils.createURI(baseUri, WikiSearchQueryResource.class, wikiName).toString();
    Link queryLink = this.objectFactory.createLink();
    queryLink.setHref(queryUri);
    queryLink.setRel(Relations.QUERY);
    wiki.getLinks().add(queryLink);
    return wiki;
}
Also used : SpacesResource(org.xwiki.rest.resources.spaces.SpacesResource) WikiSearchResource(org.xwiki.rest.resources.wikis.WikiSearchResource) WikiSearchQueryResource(org.xwiki.rest.resources.wikis.WikiSearchQueryResource) Wiki(org.xwiki.rest.model.jaxb.Wiki) ModificationsResource(org.xwiki.rest.resources.ModificationsResource) Link(org.xwiki.rest.model.jaxb.Link) ClassesResource(org.xwiki.rest.resources.classes.ClassesResource)

Aggregations

Link (org.xwiki.rest.model.jaxb.Link)55 Test (org.junit.Test)17 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)16 Page (org.xwiki.rest.model.jaxb.Page)15 GetMethod (org.apache.commons.httpclient.methods.GetMethod)13 XWikiRestException (org.xwiki.rest.XWikiRestException)12 Calendar (java.util.Calendar)11 Object (org.xwiki.rest.model.jaxb.Object)11 Document (com.xpn.xwiki.api.Document)10 XWikiException (com.xpn.xwiki.XWikiException)9 Property (org.xwiki.rest.model.jaxb.Property)9 PageResource (org.xwiki.rest.resources.pages.PageResource)8 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 PutMethod (org.apache.commons.httpclient.methods.PutMethod)7 DocumentReference (org.xwiki.model.reference.DocumentReference)7 Wiki (org.xwiki.rest.model.jaxb.Wiki)7 ArrayList (java.util.ArrayList)6 ObjectSummary (org.xwiki.rest.model.jaxb.ObjectSummary)6