Search in sources :

Example 1 with Package

use of com.xpn.xwiki.plugin.packaging.Package 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 Package

use of com.xpn.xwiki.plugin.packaging.Package in project xwiki-platform by xwiki.

the class Exporter method exportDocuments.

/**
 * Export documents from an existing loaded XWiki database. The database is defined by its passed name and by an
 * Hibernate configuration file.
 *
 * @param exportDirectory the directory where to export the documents
 * @param wikiId the wiki to export
 * @throws Exception if the export failed for any reason
 */
public void exportDocuments(File exportDirectory, String wikiId) throws Exception {
    WikiReference currentWikiReference = this.oldCoreHelper.getXWikiContext().getWikiReference();
    try {
        this.oldCoreHelper.getXWikiContext().setWikiId(wikiId);
        // TODO: Use Filter framework instead
        Package pack = new Package();
        pack.setWithVersions(false);
        pack.addAllWikiDocuments(this.oldCoreHelper.getXWikiContext());
        // See https://jira.xwiki.org/browse/XWIKI-458
        try {
            pack.exportToDir(exportDirectory, this.oldCoreHelper.getXWikiContext());
        } catch (IOException e) {
            throw new PackageException(PackageException.ERROR_PACKAGE_UNKNOWN, "Failed to export documents to [" + exportDirectory + "]", e);
        }
    } finally {
        this.oldCoreHelper.getXWikiContext().setWikiReference(currentWikiReference);
    }
}
Also used : PackageException(com.xpn.xwiki.plugin.packaging.PackageException) Package(com.xpn.xwiki.plugin.packaging.Package) IOException(java.io.IOException) WikiReference(org.xwiki.model.reference.WikiReference)

Example 3 with Package

use of com.xpn.xwiki.plugin.packaging.Package in project xwiki-platform by xwiki.

the class Importer method importXAR.

/**
 * @param file the XAR file to import
 * @param importUser optionally the user under which to perform the import (useful for example when importing pages
 *            that need to have Programming Rights and the page author is not the same as the importing user)
 * @param context the XWiki context
 * @return the number of imported documents
 * @throws XWikiException failed to import the XAR file
 * @throws IOException failed to parse the XAR file
 */
public int importXAR(File file, String importUser, XWikiContext context) throws XWikiException, IOException {
    Package pack = new Package();
    pack.setWithVersions(false);
    // Parse XAR
    FileInputStream fis = new FileInputStream(file);
    try {
        pack.Import(fis, context);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    // Import into the database
    if (!pack.getFiles().isEmpty()) {
        installWithUser(importUser, pack, context);
    }
    return pack.getFiles().size();
}
Also used : Package(com.xpn.xwiki.plugin.packaging.Package) FileInputStream(java.io.FileInputStream)

Example 4 with Package

use of com.xpn.xwiki.plugin.packaging.Package in project xwiki-platform by xwiki.

the class Importer method importDocuments.

/**
 * Import documents defined in an XML file located in the passed document definition directory into a database
 * defined by its passed name and by an Hibernate configuration file.
 * <p>
 * Note: I would have liked to call this method "import" but it's a reserved keyword... Strange that it's not
 * allowed for method names though.
 * </p>
 *
 * @param sourceDirectory the directory where the package.xml file is located and where the documents to import are
 *            located
 * @param wikiId id of the wiki into which to import the documents (e.g. {@code xwiki})
 * @param importUser optionally the user under which to perform the import (useful for example when importing pages
 *            that need to have Programming Rights and the page author is not the same as the importing user)
 * @throws Exception if the import failed for any reason
 */
// TODO: Replace the Hibernate config file with a list of parameters required for the import
public void importDocuments(File sourceDirectory, String wikiId, String importUser) throws Exception {
    Package pack = new Package();
    pack.setWithVersions(false);
    // See https://jira.xwiki.org/browse/XWIKI-458
    try {
        pack.readFromDir(sourceDirectory, this.oldCoreHelper.getXWikiContext());
    } catch (IOException e) {
        throw new PackageException(PackageException.ERROR_PACKAGE_UNKNOWN, "Failed to import documents from [" + sourceDirectory + "]", e);
    }
    installWithUser(importUser, pack, this.oldCoreHelper.getXWikiContext());
}
Also used : PackageException(com.xpn.xwiki.plugin.packaging.PackageException) Package(com.xpn.xwiki.plugin.packaging.Package) IOException(java.io.IOException)

Aggregations

Package (com.xpn.xwiki.plugin.packaging.Package)4 PackageException (com.xpn.xwiki.plugin.packaging.PackageException)2 IOException (java.io.IOException)2 XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)1 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)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 LogEvent (org.xwiki.logging.event.LogEvent)1 LoggerListener (org.xwiki.logging.event.LoggerListener)1