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