Search in sources :

Example 6 with Attachments

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

the class SpacesResourceTest method testAttachments.

@Test
public void testAttachments() throws Exception {
    DocumentReference reference = new DocumentReference(getWiki(), getTestClassName(), getTestMethodName());
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().savePage(reference);
    this.testUtils.rest().attachFile(new AttachmentReference("attachment.txt", reference), new ReaderInputStream(new StringReader("content")), true);
    // Matches Sandbox.WebHome@XWikLogo.png
    GetMethod getMethod = executeGet(String.format("%s", buildURI(SpaceAttachmentsResource.class, getWiki(), Arrays.asList(getTestClassName()))));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ReaderInputStream(org.restlet.engine.io.ReaderInputStream) StringReader(java.io.StringReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Attachment(org.xwiki.rest.model.jaxb.Attachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) DocumentReference(org.xwiki.model.reference.DocumentReference) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 7 with Attachments

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

the class WikisResourceTest method testAttachments.

@Test
public void testAttachments() throws Exception {
    this.testUtils.rest().delete(reference);
    this.testUtils.rest().attachFile(new AttachmentReference(getTestClassName() + ".txt", reference), new ReaderInputStream(new StringReader("attachment content")), true);
    // Verify there are attachments in the whole wiki
    GetMethod getMethod = executeGet(buildURI(WikiAttachmentsResource.class, getWiki()).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Attachments attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertTrue(attachments.getAttachments().size() > 0);
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for a specific attachment name in the whole wiki
    getMethod = executeGet(String.format("%s?name=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for all attachments in a given space (sandbox)
    // Also verify that a space can be looked up independtly of its case ("sandbox" will match the "Sandbox" space)
    getMethod = executeGet(String.format("%s?space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
    // Verify we can search for an attachment in a given space (sandbox)
    getMethod = executeGet(String.format("%s?name=" + getTestClassName() + "&space=" + getTestClassName(), buildURI(WikiAttachmentsResource.class, getWiki())));
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    attachments = (Attachments) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(getAttachmentsInfo(attachments), 1, attachments.getAttachments().size());
    for (Attachment attachment : attachments.getAttachments()) {
        checkLinks(attachment);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ReaderInputStream(org.restlet.engine.io.ReaderInputStream) StringReader(java.io.StringReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Attachment(org.xwiki.rest.model.jaxb.Attachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) WikiAttachmentsResource(org.xwiki.rest.resources.wikis.WikiAttachmentsResource) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 8 with Attachments

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

the class AttachmentHistoryResourceImpl method getAttachmentHistory.

@Override
public Attachments getAttachmentHistory(String wikiName, String spaceName, String pageName, String attachmentName, Integer start, Integer number) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        final com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(attachmentName);
        if (xwikiAttachment == null) {
            throw new WebApplicationException(Status.NOT_FOUND);
        }
        Attachments attachments = new Attachments();
        Version[] versions = xwikiAttachment.getVersions();
        List<Version> versionList = new ArrayList<Version>();
        for (Version version : versions) {
            versionList.add(version);
        }
        RangeIterable<Version> ri = new RangeIterable<Version>(versionList, start, number);
        for (Version version : ri) {
            com.xpn.xwiki.api.Attachment xwikiAttachmentAtVersion = xwikiAttachment.getAttachmentRevision(version.toString());
            URL url = Utils.getXWikiContext(componentManager).getURLFactory().createAttachmentRevisionURL(attachmentName, spaceName, doc.getName(), version.toString(), null, wikiName, Utils.getXWikiContext(componentManager));
            String attachmentXWikiAbsoluteUrl = url.toString();
            String attachmentXWikiRelativeUrl = Utils.getXWikiContext(componentManager).getURLFactory().getURL(url, Utils.getXWikiContext(componentManager));
            attachments.getAttachments().add(DomainObjectFactory.createAttachmentAtVersion(objectFactory, uriInfo.getBaseUri(), xwikiAttachmentAtVersion, attachmentXWikiRelativeUrl, attachmentXWikiAbsoluteUrl, Utils.getXWikiApi(componentManager), false));
        }
        return attachments;
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : RangeIterable(org.xwiki.rest.internal.RangeIterable) WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) Attachments(org.xwiki.rest.model.jaxb.Attachments) URL(java.net.URL) Version(org.suigeneris.jrcs.rcs.Version) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Attachments (org.xwiki.rest.model.jaxb.Attachments)8 Attachment (org.xwiki.rest.model.jaxb.Attachment)6 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)5 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)5 URL (java.net.URL)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)2 ReaderInputStream (org.restlet.engine.io.ReaderInputStream)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2 RangeIterable (org.xwiki.rest.internal.RangeIterable)2 XWikiException (com.xpn.xwiki.XWikiException)1 Document (com.xpn.xwiki.api.Document)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Formatter (java.util.Formatter)1