use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DefaultDocumentRestorerFromAttachedXARTest method errorZipInvalid.
@Test
public void errorZipInvalid() throws Exception {
// Mocks
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install");
when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
XWikiAttachment xeXar = mock(XWikiAttachment.class);
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(getClass().getResourceAsStream("/invalid-xar.xar"));
// Run
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
// Verify
verify(mocker.getMockedLogger()).error(eq("Error during the decompression of [{}]."), eq("workspace-template.xar"), any(IOException.class));
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testInstallOnWikiWithoutAuthor.
@Test
public void testInstallOnWikiWithoutAuthor() throws Throwable {
XWikiDocument existingDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
BaseObject object = new BaseObject();
object.setXClassReference(new DocumentReference("wiki", "space", "class"));
existingDocument.addXObject(object);
existingDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
this.oldcore.getSpyXWiki().saveDocument(existingDocument, "", true, getXWikiContext());
// install
install(this.localXarExtensiontId1, "wiki", null);
// validate
DocumentReference xarAuthorReference = new DocumentReference("wiki", "XWiki", "author");
DocumentReference xarCreatorReference = new DocumentReference("wiki", "XWiki", "creator");
DocumentReference xarContentAuthorReference = new DocumentReference("wiki", "XWiki", "contentAuthor");
// space.page
XWikiDocument page = this.oldcore.getSpyXWiki().getDocument(existingDocument.getDocumentReference(), getXWikiContext());
Assert.assertFalse("Document wiki:space.page has not been saved in the database", page.isNew());
Assert.assertNull(page.getXObject(object.getXClassReference()));
Assert.assertEquals("Wrong content", "content", page.getContent());
Assert.assertEquals("Wrong creator", new DocumentReference("wiki", "space", "existingcreator"), page.getCreatorReference());
Assert.assertEquals("Wrong author", xarAuthorReference, page.getAuthorReference());
Assert.assertEquals("Wrong content author", xarContentAuthorReference, page.getContentAuthorReference());
Assert.assertEquals("Wrong version", "2.1", page.getVersion());
Assert.assertEquals("Wrong version", Locale.ROOT, page.getLocale());
Assert.assertFalse("Document is hidden", page.isHidden());
BaseClass baseClass = page.getXClass();
Assert.assertNotNull(baseClass.getField("property"));
Assert.assertEquals("property", baseClass.getField("property").getName());
Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
// space.pagewithattachment
XWikiDocument pagewithattachment = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithattachment"), getXWikiContext());
Assert.assertFalse(pagewithattachment.isNew());
Assert.assertEquals("Wrong version", "1.1", pagewithattachment.getVersion());
Assert.assertEquals("Wrong creator", xarCreatorReference, pagewithattachment.getCreatorReference());
Assert.assertEquals("Wrong author", xarAuthorReference, pagewithattachment.getAuthorReference());
Assert.assertEquals("Wrong content author", xarContentAuthorReference, pagewithattachment.getContentAuthorReference());
XWikiAttachment attachment = pagewithattachment.getAttachment("attachment.txt");
Assert.assertNotNull(attachment);
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
Assert.assertEquals(new DocumentReference("wiki", "XWiki", "attachmentauthor"), attachment.getAuthorReference());
// space1.page1
XWikiDocument page1 = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "page1"), getXWikiContext());
Assert.assertFalse("Document wiki:space1.page1 has not been saved in the database", page1.isNew());
// translated.translated
DocumentReference translatedReference = new DocumentReference("wiki", "translated", "translated");
XWikiDocument defaultTranslated = this.oldcore.getSpyXWiki().getDocument(translatedReference, getXWikiContext());
Assert.assertNotNull("Document wiki:translated.translated has not been saved in the database", defaultTranslated);
Assert.assertFalse("Document wiki:translated.translated has not been saved in the database", defaultTranslated.isNew());
Assert.assertEquals("Wrong content", "default content", defaultTranslated.getContent());
Assert.assertEquals("Wrong creator", xarCreatorReference, defaultTranslated.getCreatorReference());
Assert.assertEquals("Wrong author", xarAuthorReference, defaultTranslated.getAuthorReference());
Assert.assertEquals("Wrong content author", xarContentAuthorReference, defaultTranslated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
// translated.translated.tr
XWikiDocument translated = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("tr")));
Assert.assertNotNull("Document wiki:translated.translated in langauge tr has not been saved in the database", translated);
Assert.assertFalse("Document wiki:translated.translated in langauge tr has not been saved in the database", translated.isNew());
Assert.assertEquals("Wrong content", "tr content", translated.getContent());
Assert.assertEquals("Wrong creator", xarCreatorReference, translated.getCreatorReference());
Assert.assertEquals("Wrong author", xarAuthorReference, translated.getAuthorReference());
Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
// translated.translated.fr
XWikiDocument translated2 = this.oldcore.getDocuments().get(new DocumentReference(translatedReference, new Locale("fr")));
Assert.assertNotNull("Document wiki:translated.translated in language fr has not been saved in the database", translated2);
Assert.assertFalse("Document wiki:translated.translated in langauge fr has not been saved in the database", translated2.isNew());
Assert.assertEquals("Wrong content", "fr content", translated2.getContent());
Assert.assertEquals("Wrong creator", xarCreatorReference, translated2.getCreatorReference());
Assert.assertEquals("Wrong author", xarAuthorReference, translated2.getAuthorReference());
Assert.assertEquals("Wrong content author", xarContentAuthorReference, translated2.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
// space.hiddenpage
XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
Assert.assertNotNull("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage);
Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testUpgradeOnRoot.
@Test
public void testUpgradeOnRoot() throws Throwable {
doReturn(Arrays.asList("wiki1", "wiki2")).when(this.oldcore.getWikiDescriptorManager()).getAllIds();
install(this.localXarExtensiontId1, null, this.contextUser);
verifyHasAdminRight(2);
// Do some local modifications
XWikiDocument deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "deletedpage"), getXWikiContext());
this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
XWikiDocument pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
deletedpagewithmodifications.setContent("modified content");
// upgrade
install(this.localXarExtensiontId2, null, this.contextUser);
verifyHasAdminRight(3);
// validate
// samespace.samepage
XWikiDocument samepage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "samespace", "samepage"), getXWikiContext());
Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
// space.page
XWikiDocument modifiedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "page"), getXWikiContext());
Assert.assertFalse("Document wiki.space.page has not been saved in the database", modifiedpage.isNew());
Assert.assertEquals("Wrong content", "content 2", modifiedpage.getContent());
Assert.assertEquals("Wrong author", this.contextUser, modifiedpage.getAuthorReference());
Assert.assertEquals("Wrong versions", "2.1", modifiedpage.getVersion());
Assert.assertEquals("Wrong version", Locale.ROOT, modifiedpage.getLocale());
Assert.assertEquals("Wrong customclass", "customclass2", modifiedpage.getCustomClass());
Assert.assertEquals("Wrong defaultTemplate", "defaultTemplate2", modifiedpage.getDefaultTemplate());
Assert.assertEquals("Wrong hidden", true, modifiedpage.isHidden());
Assert.assertEquals("Wrong ValidationScript", "validationScript2", modifiedpage.getValidationScript());
BaseClass baseClass = modifiedpage.getXClass();
Assert.assertNotNull(baseClass.getField("property"));
Assert.assertEquals("property", baseClass.getField("property").getName());
Assert.assertSame(NumberClass.class, baseClass.getField("property").getClass());
XWikiAttachment attachment = modifiedpage.getAttachment("attachment.txt");
Assert.assertNotNull(attachment);
Assert.assertEquals("attachment.txt", attachment.getFilename());
Assert.assertEquals(18, attachment.getContentLongSize(getXWikiContext()));
Assert.assertEquals("attachment content", IOUtils.toString(attachment.getContentInputStream(getXWikiContext()), StandardCharsets.UTF_8));
// space2.page2
XWikiDocument newPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space2", "page2"), getXWikiContext());
Assert.assertFalse("Document wiki:space2.page2 has not been saved in the database", newPage.isNew());
// space1.page1
XWikiDocument removedPage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "page1"), getXWikiContext());
Assert.assertTrue("Document wiki:space1.page1 has not been removed from the database", removedPage.isNew());
// space.deletedpage
deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "deletedpage"), getXWikiContext());
Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
// space.modifieddeletedpage
modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "modifieddeletedpage"), getXWikiContext());
Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
// space.pagewithobject
pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space", "pagewithobject"), getXWikiContext());
Assert.assertNull("Document wiki:space.pagewithobject does not contain an XWiki.XWikiGroups object", pagewithobject.getXObject(new LocalDocumentReference("XWiki", "XWikiGroups")));
// space1.modified
XWikiDocument space1modified = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki1", "space1", "modified"), getXWikiContext());
Assert.assertFalse("Document wiki:space1.modified has been removed from the database", space1modified.isNew());
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWiki method searchAttachments.
/**
* Search attachments by passing HQL where clause values as parameters. You can specify properties of the "attach"
* (the attachment) or "doc" (the document it is attached to)
*
* @param parametrizedSqlClause The HQL where clause. For example {@code where doc.fullName
* <> ? and (attach.author = ? or (attach.filename = ? and doc.space = ?))}
* @param checkRight if true, only return attachments in documents which the "current user" has permission to view.
* @param nb The number of rows to return. If 0 then all rows are returned
* @param start The number of rows to skip at the beginning.
* @param parameterValues A {@link java.util.List} of the where clause values that replace the question marks (?)
* @param context see {@link XWikiContext}
* @return A List of {@link XWikiAttachment} objects.
* @throws XWikiException in case of error while performing the query
* @see com.xpn.xwiki.store.XWikiStoreInterface#searchDocuments(String, int, int, java.util.List, XWikiContext)
* @since 5.0M2
* @deprecated since 9.7RC1, use the QueryManager instead along with the "attachment" query filter
*/
@Deprecated
public List<XWikiAttachment> searchAttachments(String parametrizedSqlClause, boolean checkRight, int nb, int start, List<?> parameterValues, XWikiContext context) throws XWikiException {
parametrizedSqlClause = parametrizedSqlClause.trim().replaceFirst("^and ", "").replaceFirst("^where ", "");
// Get the attachment filenames and document fullNames
List<java.lang.Object[]> results = this.getStore().search("select attach.filename, doc.fullName from XWikiAttachment attach, XWikiDocument doc where doc.id = attach.docId and " + parametrizedSqlClause, nb, start, parameterValues, context);
HashMap<String, List<String>> filenamesByDocFullName = new HashMap<String, List<String>>();
// Put each attachment name with the document name it belongs to
for (int i = 0; i < results.size(); i++) {
String filename = (String) results.get(i)[0];
String docFullName = (String) results.get(i)[1];
if (!filenamesByDocFullName.containsKey(docFullName)) {
filenamesByDocFullName.put(docFullName, new ArrayList<String>());
}
filenamesByDocFullName.get(docFullName).add(filename);
}
List<XWikiAttachment> out = new ArrayList<XWikiAttachment>();
// Index through the document names, get relivent attachments
for (Map.Entry<String, List<String>> entry : filenamesByDocFullName.entrySet()) {
String fullName = entry.getKey();
XWikiDocument doc = getDocument(fullName, context);
if (checkRight) {
if (!context.getWiki().getRightService().hasAccessLevel("view", context.getUser(), doc.getFullName(), context)) {
continue;
}
}
List<String> returnedAttachmentNames = entry.getValue();
for (XWikiAttachment attach : doc.getAttachmentList()) {
if (returnedAttachmentNames.contains(attach.getFilename())) {
out.add(attach);
}
}
}
return out;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XWikiHibernateAttachmentStore method deleteXWikiAttachment.
@Override
public void deleteXWikiAttachment(XWikiAttachment attachment, boolean parentUpdate, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
String currentWiki = context.getWikiId();
try {
// Switch context wiki to attachment wiki
String attachmentWiki = (attachment.getReference() == null) ? null : attachment.getReference().getDocumentReference().getWikiReference().getName();
if (attachmentWiki != null) {
context.setWikiId(attachmentWiki);
}
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
}
Session session = getSession(context);
// Delete the three attachment entries
try {
session.delete(new XWikiAttachmentContent(attachment));
} catch (Exception e) {
this.logger.warn("Error deleting attachment content [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
}
AttachmentVersioningStore store = resolveAttachmentVersioningStore(attachment, context);
store.deleteArchive(attachment, context, false);
try {
session.delete(attachment);
} catch (Exception e) {
this.logger.warn("Error deleting attachment meta data [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
}
try {
if (parentUpdate) {
List<XWikiAttachment> list = attachment.getDoc().getAttachmentList();
for (int i = 0; i < list.size(); i++) {
XWikiAttachment attach = list.get(i);
if (attachment.getFilename().equals(attach.getFilename())) {
list.remove(i);
break;
}
}
context.getWiki().getStore().saveXWikiDoc(attachment.getDoc(), context, false);
}
} catch (Exception e) {
this.logger.warn("Error updating document when deleting attachment [{}] of document [{}]", attachment.getFilename(), attachment.getDoc().getDocumentReference());
}
if (bTransaction) {
endTransaction(context, true);
}
} catch (Exception e) {
Object[] args = { attachment.getReference() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_ATTACHMENT, "Exception while deleting attachment {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false);
}
} catch (Exception e) {
}
// Restore context wiki
context.setWikiId(currentWiki);
restoreExecutionXContext();
}
}
Aggregations