Search in sources :

Example 1 with XWikiAttachmentArchive

use of com.xpn.xwiki.doc.XWikiAttachmentArchive in project xwiki-platform by xwiki.

the class VoidAttachmentVersioningStore method loadArchive.

@Override
public XWikiAttachmentArchive loadArchive(XWikiAttachment attachment, XWikiContext context, boolean transaction) throws XWikiException {
    XWikiAttachmentArchive archive = attachment.getAttachment_archive();
    if (!(archive instanceof VoidAttachmentArchive)) {
        archive = new VoidAttachmentArchive(attachment);
    }
    attachment.setAttachment_archive(archive);
    return archive;
}
Also used : XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive)

Example 2 with XWikiAttachmentArchive

use of com.xpn.xwiki.doc.XWikiAttachmentArchive in project xwiki-platform by xwiki.

the class HibernateAttachmentVersioningStore method loadArchive.

@Override
public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, XWikiContext context, boolean bTransaction) throws XWikiException {
    try {
        final XWikiAttachmentArchive archive = new XWikiAttachmentArchive();
        archive.setAttachment(attachment);
        executeRead(context, bTransaction, new HibernateCallback<Object>() {

            @Override
            public Object doInHibernate(Session session) throws HibernateException {
                try {
                    session.load(archive, archive.getId());
                } catch (ObjectNotFoundException e) {
                // if none found then return empty created archive
                }
                return null;
            }
        });
        attachment.setAttachment_archive(archive);
        return archive;
    } catch (Exception e) {
        Object[] args = { attachment.getFilename(), attachment.getDoc() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT, "Exception while loading attachment archive {0} of document {1}", e, args);
    }
}
Also used : XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) HibernateException(org.hibernate.HibernateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) XWikiException(com.xpn.xwiki.XWikiException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 3 with XWikiAttachmentArchive

use of com.xpn.xwiki.doc.XWikiAttachmentArchive in project xwiki-platform by xwiki.

the class VoidAttachmentVersioningStoreTest method testStore.

public void testStore() throws XWikiException {
    // is store correctly inited?
    assertEquals(VoidAttachmentVersioningStore.class, this.store.getClass());
    // create doc, attachment & attachment archive
    XWikiDocument doc = new XWikiDocument(new DocumentReference("Wiki", "Main", "Test"));
    XWikiAttachment attachment = new XWikiAttachment(doc, "filename");
    attachment.setContent(new byte[] { 1 });
    attachment.updateContentArchive(getContext());
    // is archive correctly inited and cloneable?
    this.store.saveArchive(attachment.getAttachment_archive(), this.getContext(), true);
    XWikiAttachmentArchive archive = this.store.loadArchive(attachment, this.getContext(), false);
    assertEquals(VoidAttachmentArchive.class, archive.getClass());
    assertEquals(VoidAttachmentArchive.class, archive.clone().getClass());
    assertEquals(archive, this.store.loadArchive(attachment, this.getContext(), true));
    this.store.deleteArchive(attachment, getContext(), true);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with XWikiAttachmentArchive

use of com.xpn.xwiki.doc.XWikiAttachmentArchive in project xwiki-platform by xwiki.

the class XWikiAttachmentEventGenerator method write.

@Override
public void write(XWikiAttachment attachment, Object filter, XWikiAttachmentFilter attachmentFilter, DocumentInstanceInputProperties properties) throws FilterException {
    XWikiContext xcontext = this.xcontextProvider.get();
    FilterEventParameters attachmentParameters = new FilterEventParameters();
    if (attachment.getAuthor() != null) {
        attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_AUTHOR, attachment.getAuthor());
    }
    attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_COMMENT, attachment.getComment());
    attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION_DATE, attachment.getDate());
    attachmentParameters.put(WikiAttachmentFilter.PARAMETER_REVISION, attachment.getVersion());
    if (StringUtils.isNotEmpty(attachment.getMimeType())) {
        attachmentParameters.put(WikiAttachmentFilter.PARAMETER_MIMETYPE, attachment.getMimeType());
    }
    if (properties.isWithJRCSRevisions()) {
        try {
            // We need to make sure content is loaded
            XWikiAttachmentArchive archive;
            archive = attachment.loadArchive(xcontext);
            if (archive != null) {
                attachmentParameters.put(XWikiWikiAttachmentFilter.PARAMETER_JRCSREVISIONS, archive.getArchiveAsString());
            }
        } catch (XWikiException e) {
            this.logger.error("Attachment [{}] has malformed history", attachment.getReference(), e);
        }
    }
    InputStream content;
    Long size;
    if (properties.isWithWikiAttachmentsContent()) {
        try {
            content = attachment.getContentInputStream(xcontext);
            size = Long.valueOf(attachment.getLongSize());
        } catch (XWikiException e) {
            this.logger.error("Failed to get content of attachment [{}]", attachment.getReference(), e);
            content = new ByteArrayInputStream(new byte[0]);
            size = 0L;
        }
    } else {
        content = null;
        size = null;
    }
    // WikiAttachment
    attachmentFilter.onWikiAttachment(attachment.getFilename(), content, size, attachmentParameters);
}
Also used : FilterEventParameters(org.xwiki.filter.FilterEventParameters) XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with XWikiAttachmentArchive

use of com.xpn.xwiki.doc.XWikiAttachmentArchive in project xwiki-platform by xwiki.

the class FilesystemAttachmentVersioningStoreTest method loadArchiveTest.

@Test
public void loadArchiveTest() throws Exception {
    this.versionStore.saveArchive(this.archive, null, false);
    final XWikiAttachmentArchive newArch = this.versionStore.loadArchive(archive.getAttachment(), null, false);
    Assert.assertTrue(newArch.getVersions().length == 3);
    final XWikiAttachment version1 = newArch.getRevision(archive.getAttachment(), "1.1", null);
    final XWikiAttachment version2 = newArch.getRevision(archive.getAttachment(), "1.2", null);
    final XWikiAttachment version3 = newArch.getRevision(archive.getAttachment(), "1.3", null);
    Assert.assertTrue(version1.getVersion().equals("1.1"));
    Assert.assertTrue(version1.getFilename().equals("attachment.txt"));
    Assert.assertEquals("I am version 1.1", IOUtils.toString(version1.getContentInputStream(null)));
    Assert.assertSame(version1.getDoc(), this.archive.getAttachment().getDoc());
    Assert.assertTrue(version2.getVersion().equals("1.2"));
    Assert.assertTrue(version2.getFilename().equals("attachment.txt"));
    Assert.assertEquals("I am version 1.2", IOUtils.toString(version2.getContentInputStream(null)));
    Assert.assertSame(version2.getDoc(), this.archive.getAttachment().getDoc());
    Assert.assertTrue(version3.getVersion().equals("1.3"));
    Assert.assertTrue(version3.getFilename().equals("attachment.txt"));
    Assert.assertEquals("I am version 1.3", IOUtils.toString(version3.getContentInputStream(null)));
    Assert.assertSame(version3.getDoc(), this.archive.getAttachment().getDoc());
}
Also used : XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Aggregations

XWikiAttachmentArchive (com.xpn.xwiki.doc.XWikiAttachmentArchive)6 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HibernateException (org.hibernate.HibernateException)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 Session (org.hibernate.Session)1 Test (org.junit.Test)1 FilterEventParameters (org.xwiki.filter.FilterEventParameters)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1