use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class SkinAction method renderFileFromAttachment.
/**
* Tries to serve the content of an attachment as a skin file.
*
* @param filename The name of the skin file that should be rendered.
* @param doc The skin {@link XWikiDocument document}.
* @param context The current {@link XWikiContext request context}.
* @return <tt>true</tt> if the attachment was found and its content was successfully sent.
* @throws IOException If the response cannot be sent.
* @throws XWikiException If the attachment cannot be loaded.
*/
public boolean renderFileFromAttachment(String filename, XWikiDocument doc, XWikiContext context) throws IOException, XWikiException {
LOGGER.debug("... as attachment");
XWikiAttachment attachment = doc.getAttachment(filename);
if (attachment != null) {
XWiki xwiki = context.getWiki();
XWikiResponse response = context.getResponse();
// Evaluate the file only if it's of a supported type.
String mimetype = xwiki.getEngineContext().getMimeType(filename.toLowerCase());
if (isCssMimeType(mimetype) || isJavascriptMimeType(mimetype)) {
byte[] data = attachment.getContent(context);
// Always force UTF-8, as this is the assumed encoding for text files.
String velocityCode = new String(data, ENCODING);
// Evaluate the content with the rights of the document's author.
String evaluatedContent = evaluateVelocity(velocityCode, attachment.getReference(), doc.getAuthorReference(), context);
// Prepare the response.
response.setCharacterEncoding(ENCODING);
// Write the content to the response's output stream.
data = evaluatedContent.getBytes(ENCODING);
setupHeaders(response, mimetype, attachment.getDate(), data.length);
response.getOutputStream().write(data);
} else {
// Otherwise, return the raw content.
setupHeaders(response, mimetype, attachment.getDate(), attachment.getContentSize(context));
IOUtils.copy(attachment.getContentInputStream(context), response.getOutputStream());
}
return true;
} else {
LOGGER.debug("Attachment not found");
}
return false;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class RemoteSolrInstance method generateAndAttachConfigurationZipIfNotExist.
/**
* Generates the configuration files required to properly configure and initialize the remote Solr server.
* <p>
* The files are available as a zip archive ("solr.zip") attached to the main wiki's XWiki.SolrSearchAdmin document
* and exposed in the user interface of the main wiki.
* <p>
* Note: If the attachment already exists, nothing will be done.
*
* @throws Exception if problems occur.
*/
public void generateAndAttachConfigurationZipIfNotExist() throws Exception {
XWikiContext context = this.xcontextProvider.get();
XWiki xwiki = context.getWiki();
// Get or Create the attachment and dump the content of the zip into the attachment.
DocumentReference configDocumentReference = new DocumentReference(context.getMainXWiki(), "XWiki", "SolrSearchAdmin");
XWikiDocument configurationDocument = xwiki.getDocument(configDocumentReference, context);
XWikiAttachment configurationZipAttachment = configurationDocument.getAttachment(CONFIGURATION_ZIP_FILE_NAME);
if (configurationZipAttachment == null) {
// Create the zip file to attach.
try (InputStream inputStream = this.configuration.getHomeDirectoryConfiguration()) {
// Attach the file.
configurationDocument.setAttachment(CONFIGURATION_ZIP_FILE_NAME, inputStream, context);
xwiki.saveDocument(configurationDocument, "Attach default SOLR configuration", context);
}
}
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class SolrIndexEventListener method onEvent.
@Override
public void onEvent(Event event, Object source, Object data) {
try {
if (event instanceof DocumentUpdatedEvent) {
XWikiDocument document = (XWikiDocument) source;
if (Locale.ROOT.equals(document.getLocale())) {
// Index all the translations of a document when its default translation has been updated because
// the default translation holds meta data shared by all translations (attachments, objects).
indexTranslations(document, (XWikiContext) data);
} else {
// Index only the updated translation.
this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
}
} else if (event instanceof DocumentCreatedEvent) {
XWikiDocument document = (XWikiDocument) source;
if (!Locale.ROOT.equals(document.getLocale())) {
// If a new translation is added to a document reindex the whole document (could be optimized a bit
// by reindexing only the parent locales but that would always include objects and attachments
// anyway)
indexTranslations(document, (XWikiContext) data);
} else {
this.solrIndexer.get().index(document.getDocumentReferenceWithLocale(), false);
}
} else if (event instanceof DocumentDeletedEvent) {
XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
// We must pass the document reference with the REAL locale because when the indexer is going to delete
// the document from the Solr index (later, on a different thread) the real locale won't be accessible
// anymore since the XWiki document has been already deleted from the database. The real locale (taken
// from the XWiki document) is used to compute the id of the Solr document when the document reference
// locale is ROOT (i.e. for default document translations).
// Otherwise the document won't be deleted from the Solr index (because the computed id won't match any
// document from the Solr index) and we're going to have deleted documents that are still in the Solr
// index. These documents will be filtered from the search results but not from the facet counts.
// See XWIKI-10003: Cache problem with Solr facet filter results count
this.solrIndexer.get().delete(new DocumentReference(document.getDocumentReference(), document.getRealLocale()), false);
} else if (event instanceof AttachmentUpdatedEvent || event instanceof AttachmentAddedEvent) {
XWikiDocument document = (XWikiDocument) source;
String fileName = ((AbstractAttachmentEvent) event).getName();
XWikiAttachment attachment = document.getAttachment(fileName);
this.solrIndexer.get().index(attachment.getReference(), false);
} else if (event instanceof AttachmentDeletedEvent) {
XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
String fileName = ((AbstractAttachmentEvent) event).getName();
XWikiAttachment attachment = document.getAttachment(fileName);
this.solrIndexer.get().delete(attachment.getReference(), false);
} else if (event instanceof XObjectUpdatedEvent || event instanceof XObjectAddedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().index(entityEvent.getReference(), false);
} else if (event instanceof XObjectDeletedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().delete(entityEvent.getReference(), false);
} else if (event instanceof XObjectPropertyUpdatedEvent || event instanceof XObjectPropertyAddedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().index(entityEvent.getReference(), false);
} else if (event instanceof XObjectPropertyDeletedEvent) {
EntityEvent entityEvent = (EntityEvent) event;
this.solrIndexer.get().delete(entityEvent.getReference(), false);
} else if (event instanceof WikiDeletedEvent) {
String wikiName = (String) source;
WikiReference wikiReference = new WikiReference(wikiName);
this.solrIndexer.get().delete(wikiReference, false);
}
} catch (Exception e) {
this.logger.error("Failed to handle event [{}] with source [{}]", event, source, e);
}
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testInstallOnWiki.
// Tests
@Test
public void testInstallOnWiki() 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());
MandatoryDocumentInitializer mandatoryInitializer = this.componentManager.registerMockComponent(MandatoryDocumentInitializer.class, "space.mandatory");
when(mandatoryInitializer.updateDocument(any(XWikiDocument.class))).thenReturn(true);
XWikiDocument mandatoryDocument = new XWikiDocument(new DocumentReference("wiki", "space", "mandatory"));
mandatoryDocument.setCreatorReference(new DocumentReference("wiki", "space", "existingcreator"));
mandatoryDocument.setSyntax(Syntax.PLAIN_1_0);
mandatoryDocument.setContent("modified content");
this.oldcore.getSpyXWiki().saveDocument(mandatoryDocument, "", true, getXWikiContext());
// install
XarInstalledExtension xarInstalledExtension = install(this.localXarExtensiontId1, "wiki", this.contextUser);
verifyHasAdminRight(2);
// validate
// 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", this.contextUser, page.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, 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());
Assert.assertFalse("Document is minor edit", page.isMinorEdit());
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", this.contextUser, pagewithattachment.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, pagewithattachment.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, 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(this.contextUser, 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());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReference()));
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(page1.getDocumentReferenceWithLocale()));
assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("wiki", "space1", "page1", Locale.ENGLISH)).size());
assertEquals(0, this.installedExtensionRepository.getXarInstalledExtensions(new DocumentReference("otherwiki", "space1", "page1")).size());
// 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", this.contextUser, defaultTranslated.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, defaultTranslated.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, defaultTranslated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", defaultTranslated.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(defaultTranslated.getDocumentReferenceWithLocale()));
// 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", this.contextUser, translated.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, translated.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, translated.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated.getDocumentReferenceWithLocale()));
// 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", this.contextUser, translated2.getCreatorReference());
Assert.assertEquals("Wrong author", this.contextUser, translated2.getAuthorReference());
Assert.assertEquals("Wrong content author", this.contextUser, translated2.getContentAuthorReference());
Assert.assertEquals("Wrong version", "1.1", translated2.getVersion());
assertEquals(Arrays.asList(xarInstalledExtension), this.installedExtensionRepository.getXarInstalledExtensions(translated2.getDocumentReferenceWithLocale()));
// space.hiddenpage
XWikiDocument hiddenpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "hiddenpage"), getXWikiContext());
Assert.assertFalse("Document wiki:space.hiddenpage has not been saved in the database", hiddenpage.isNew());
Assert.assertTrue("Document is not hidden", hiddenpage.isHidden());
// space.mandatory
XWikiDocument mandatorypage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "mandatory"), getXWikiContext());
Assert.assertEquals("Document wiki:space.mandatory has been overwritten", "1.1", mandatorypage.getVersion());
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class XarExtensionHandlerTest method testUpgradeOnWiki.
@Test
public void testUpgradeOnWiki() throws Throwable {
install(this.localXarExtensiontId1, "wiki", this.contextUser);
verifyHasAdminRight(2);
// Do some local modifications
XWikiDocument deletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "deletedpage"), getXWikiContext());
this.oldcore.getSpyXWiki().deleteDocument(deletedpage, getXWikiContext());
XWikiDocument modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
this.oldcore.getSpyXWiki().deleteDocument(modifieddeletedpage, getXWikiContext());
XWikiDocument pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "pagewithobject"), getXWikiContext());
pagewithobject.removeXObjects(new LocalDocumentReference("XWiki", "XWikiGroups"));
this.oldcore.getSpyXWiki().saveDocument(pagewithobject, getXWikiContext());
XWikiDocument deletedpagewithmodifications = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space1", "modified"), getXWikiContext());
deletedpagewithmodifications.setContent("modified content");
// upgrade
install(this.localXarExtensiontId2, "wiki", this.contextUser);
verifyHasAdminRight(3);
// validate
// samespace.samepage
XWikiDocument samepage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "samespace", "samepage"), getXWikiContext());
Assert.assertFalse("Document samespace has been removed from the database", samepage.isNew());
Assert.assertEquals("Wrong versions", "1.1", samepage.getVersion());
// space.page
XWikiDocument modifiedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "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("wiki", "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("wiki", "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("wiki", "space", "deletedpage"), getXWikiContext());
Assert.assertTrue("Document wiki:space.deleted has been restored", deletedpage.isNew());
// space.modifieddeletedpage
modifieddeletedpage = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "modifieddeletedpage"), getXWikiContext());
Assert.assertTrue("Document wiki:space.modifieddeletedpage has been restored", modifieddeletedpage.isNew());
// space.pagewithobject
pagewithobject = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "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("wiki", "space1", "modified"), getXWikiContext());
Assert.assertFalse("Document wiki:space.modified has been removed from the database", space1modified.isNew());
}
Aggregations