Search in sources :

Example 1 with XARImportedEvent

use of com.xpn.xwiki.internal.event.XARImportedEvent in project xwiki-platform by xwiki.

the class ImportAction method importPackageFilterStream.

private void importPackageFilterStream(XWikiAttachment packFile, XWikiRequest request, XWikiContext context) throws IOException, XWikiException, FilterException {
    String[] pages = request.getParameterValues("pages");
    XARInputProperties xarProperties = new XARInputProperties();
    DocumentInstanceOutputProperties instanceProperties = new DocumentInstanceOutputProperties();
    instanceProperties.setSaveComment("Imported from XAR");
    if (pages != null) {
        EntityReferenceSet entities = new EntityReferenceSet();
        EntityReferenceResolver<String> resolver = Utils.getComponent(EntityReferenceResolver.TYPE_STRING, "relative");
        for (String pageEntry : pages) {
            if (StringUtils.isNotEmpty(pageEntry)) {
                String locale = getLocale(pageEntry, request);
                int iAction = getAction(pageEntry, locale, request);
                String documentReference = getDocumentReference(pageEntry);
                if (iAction == DocumentInfo.ACTION_OVERWRITE) {
                    entities.includes(new LocalDocumentReference(resolver.resolve(documentReference, EntityType.DOCUMENT), LocaleUtils.toLocale(locale)));
                }
            }
        }
        xarProperties.setEntities(entities);
    }
    // Set the appropriate strategy to handle versions
    if (StringUtils.equals(request.getParameter("historyStrategy"), "reset")) {
        instanceProperties.setPreviousDeleted(true);
        instanceProperties.setVersionPreserved(false);
        xarProperties.setWithHistory(false);
    } else if (StringUtils.equals(request.getParameter("historyStrategy"), "replace")) {
        instanceProperties.setPreviousDeleted(true);
        instanceProperties.setVersionPreserved(true);
        xarProperties.setWithHistory(true);
    } else {
        instanceProperties.setPreviousDeleted(false);
        instanceProperties.setVersionPreserved(false);
        xarProperties.setWithHistory(false);
    }
    // Set the backup pack option
    if (StringUtils.equals(request.getParameter("importAsBackup"), "true")) {
        instanceProperties.setAuthorPreserved(true);
    } else {
        instanceProperties.setAuthorPreserved(false);
    }
    BeanInputFilterStreamFactory<XARInputProperties> xarFilterStreamFactory = Utils.getComponent((Type) InputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
    BeanInputFilterStream<XARInputProperties> xarFilterStream = xarFilterStreamFactory.createInputFilterStream(xarProperties);
    BeanOutputFilterStreamFactory<InstanceOutputProperties> instanceFilterStreamFactory = Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
    BeanOutputFilterStream<InstanceOutputProperties> instanceFilterStream = instanceFilterStreamFactory.createOutputFilterStream(instanceProperties);
    // Notify all the listeners about import
    ObservationManager observation = Utils.getComponent(ObservationManager.class);
    InputStream source = packFile.getContentInputStream(context);
    xarProperties.setSource(new DefaultInputStreamInputSource(source));
    // Setup log
    xarProperties.setVerbose(true);
    instanceProperties.setVerbose(true);
    instanceProperties.setStoppedWhenSaveFail(false);
    LoggerManager loggerManager = Utils.getComponent(LoggerManager.class);
    LogQueue logger = new LogQueue();
    if (loggerManager != null) {
        // Isolate log
        loggerManager.pushLogListener(new LoggerListener(UUID.randomUUID().toString(), logger));
    }
    observation.notify(new XARImportingEvent(), null, context);
    try {
        xarFilterStream.read(instanceFilterStream.getFilter());
        xarFilterStream.close();
        instanceFilterStream.close();
    } finally {
        if (loggerManager != null) {
            // Stop isolating log
            loggerManager.popLogListener();
        }
        // Print the import log
        if (LOGGER.isDebugEnabled()) {
            logger.log(LOGGER);
        } else {
            // TODO: remove when the UI show the log properly
            for (LogEvent logEvent : logger.getLogsFrom(LogLevel.ERROR)) {
                logEvent.log(LOGGER);
            }
        }
        // Close the input source
        source.close();
        observation.notify(new XARImportedEvent(), null, context);
    }
    // Generate import report
    // Emulate old packager report (for retro compatibility)
    Package oldImporter = new Package();
    if (logger.containLogsFrom(LogLevel.ERROR)) {
        context.put("install_status", DocumentInfo.INSTALL_ERROR);
    } else {
        context.put("install_status", DocumentInfo.INSTALL_OK);
    }
    EntityReferenceSerializer<String> serializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, "local");
    for (LogEvent log : logger) {
        Marker marker = log.getMarker();
        if (marker != null) {
            if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_CREATED.getName()) || marker.contains(WikiDocumentFilter.LOG_DOCUMENT_UPDATED.getName())) {
                oldImporter.getInstalled(context).add(serializer.serialize((EntityReference) log.getArgumentArray()[0]));
            } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED.getName())) {
                oldImporter.getSkipped(context).add(serializer.serialize((EntityReference) log.getArgumentArray()[0]));
            } else if (marker.contains(WikiDocumentFilter.LOG_DOCUMENT_ERROR.getName())) {
                Object entity = log.getArgumentArray()[0];
                if (entity != null) {
                    oldImporter.getErrors(context).add(entity instanceof EntityReference ? serializer.serialize((EntityReference) log.getArgumentArray()[0]) : entity.toString());
                }
            }
        }
    }
}
Also used : InstanceOutputProperties(org.xwiki.filter.instance.output.InstanceOutputProperties) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) XARImportingEvent(com.xpn.xwiki.internal.event.XARImportingEvent) DefaultInputStreamInputSource(org.xwiki.filter.input.DefaultInputStreamInputSource) BeanInputFilterStreamFactory(org.xwiki.filter.input.BeanInputFilterStreamFactory) InputFilterStreamFactory(org.xwiki.filter.input.InputFilterStreamFactory) ObservationManager(org.xwiki.observation.ObservationManager) EntityReferenceSet(org.xwiki.model.reference.EntityReferenceSet) LogQueue(org.xwiki.logging.LogQueue) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) EntityReference(org.xwiki.model.reference.EntityReference) LoggerManager(org.xwiki.logging.LoggerManager) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) LogEvent(org.xwiki.logging.event.LogEvent) OutputFilterStreamFactory(org.xwiki.filter.output.OutputFilterStreamFactory) BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) InputStream(java.io.InputStream) XARImportedEvent(com.xpn.xwiki.internal.event.XARImportedEvent) LoggerListener(org.xwiki.logging.event.LoggerListener) Marker(org.slf4j.Marker) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) XarPackage(org.xwiki.xar.XarPackage) Package(com.xpn.xwiki.plugin.packaging.Package)

Example 2 with XARImportedEvent

use of com.xpn.xwiki.internal.event.XARImportedEvent in project xwiki-platform by xwiki.

the class Packager method importXARToWiki.

private XarMergeResult importXARToWiki(String comment, InputStream xarInputStream, WikiReference wikiReference, PackageConfiguration configuration) throws IOException, XarException, XWikiException {
    XarMergeResult mergeResult = new XarMergeResult();
    ZipArchiveInputStream zis = new ZipArchiveInputStream(xarInputStream);
    XWikiContext xcontext = this.xcontextProvider.get();
    String currentWiki = xcontext.getWikiId();
    try {
        xcontext.setWikiId(wikiReference.getName());
        this.observation.notify(new XARImportingEvent(), null, xcontext);
        for (ArchiveEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (!entry.isDirectory()) {
                // Only import what should be imported
                if (!entry.getName().equals(XarModel.PATH_PACKAGE) && (configuration.getEntriesToImport() == null || configuration.getEntriesToImport().contains(entry.getName()))) {
                    XarEntryMergeResult entityMergeResult = importDocumentToWiki(comment, wikiReference, zis, configuration);
                    if (entityMergeResult != null) {
                        mergeResult.addMergeResult(entityMergeResult);
                    }
                }
            }
        }
    } finally {
        this.observation.notify(new XARImportedEvent(), null, xcontext);
        xcontext.setWikiId(currentWiki);
    }
    return mergeResult;
}
Also used : XARImportingEvent(com.xpn.xwiki.internal.event.XARImportingEvent) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) XARImportedEvent(com.xpn.xwiki.internal.event.XARImportedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 3 with XARImportedEvent

use of com.xpn.xwiki.internal.event.XARImportedEvent in project xwiki-platform by xwiki.

the class Package method install.

public int install(XWikiContext context) throws XWikiException {
    boolean isAdmin = context.getWiki().getRightService().hasWikiAdminRights(context);
    if (testInstall(isAdmin, context) == DocumentInfo.INSTALL_IMPOSSIBLE) {
        setStatus(DocumentInfo.INSTALL_IMPOSSIBLE, context);
        return DocumentInfo.INSTALL_IMPOSSIBLE;
    }
    boolean hasCustomMappings = false;
    for (DocumentInfo docinfo : this.customMappingFiles) {
        BaseClass bclass = docinfo.getDoc().getXClass();
        hasCustomMappings |= context.getWiki().getStore().injectCustomMapping(bclass, context);
    }
    if (hasCustomMappings) {
        context.getWiki().getStore().injectUpdatedCustomMappings(context);
    }
    int status = DocumentInfo.INSTALL_OK;
    // Determine if the user performing the installation is a farm admin.
    // We allow author preservation from the package only to farm admins.
    // In order to prevent sub-wiki admins to take control of a farm with forged packages.
    // We test it once for the whole import in case one of the document break user during the import process.
    boolean backup = this.backupPack && isFarmAdmin(context);
    // Notify all the listeners about import
    ObservationManager om = Utils.getComponent(ObservationManager.class);
    // FIXME: should be able to pass some sort of source here, the name of the attachment or the list of
    // imported documents. But for the moment it's fine
    om.notify(new XARImportingEvent(), null, context);
    try {
        // definitions are available when installing documents using them.
        for (DocumentInfo classFile : this.classFiles) {
            if (installDocument(classFile, isAdmin, backup, context) == DocumentInfo.INSTALL_ERROR) {
                status = DocumentInfo.INSTALL_ERROR;
            }
        }
        // Install the remaining documents (without class definitions).
        for (DocumentInfo docInfo : this.files) {
            if (!this.classFiles.contains(docInfo)) {
                if (installDocument(docInfo, isAdmin, backup, context) == DocumentInfo.INSTALL_ERROR) {
                    status = DocumentInfo.INSTALL_ERROR;
                }
            }
        }
        setStatus(status, context);
    } finally {
        // FIXME: should be able to pass some sort of source here, the name of the attachment or the list of
        // imported documents. But for the moment it's fine
        om.notify(new XARImportedEvent(), null, context);
        registerExtension(context);
    }
    return status;
}
Also used : XARImportingEvent(com.xpn.xwiki.internal.event.XARImportingEvent) XARImportedEvent(com.xpn.xwiki.internal.event.XARImportedEvent) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ObservationManager(org.xwiki.observation.ObservationManager)

Aggregations

XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)3 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)3 ObservationManager (org.xwiki.observation.ObservationManager)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 Package (com.xpn.xwiki.plugin.packaging.Package)1 InputStream (java.io.InputStream)1 ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)1 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)1 Marker (org.slf4j.Marker)1 BeanInputFilterStreamFactory (org.xwiki.filter.input.BeanInputFilterStreamFactory)1 DefaultInputStreamInputSource (org.xwiki.filter.input.DefaultInputStreamInputSource)1 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)1 DocumentInstanceOutputProperties (org.xwiki.filter.instance.output.DocumentInstanceOutputProperties)1 InstanceOutputProperties (org.xwiki.filter.instance.output.InstanceOutputProperties)1 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)1 OutputFilterStreamFactory (org.xwiki.filter.output.OutputFilterStreamFactory)1 XARInputProperties (org.xwiki.filter.xar.input.XARInputProperties)1 LogQueue (org.xwiki.logging.LogQueue)1 LoggerManager (org.xwiki.logging.LoggerManager)1