Search in sources :

Example 1 with EntityReferenceSet

use of org.xwiki.model.reference.EntityReferenceSet in project xwiki-platform by xwiki.

the class ExportAction method exportXAR.

private String exportXAR(XWikiContext context) throws XWikiException, IOException, FilterException {
    XWikiRequest request = context.getRequest();
    boolean history = Boolean.valueOf(request.get("history"));
    boolean backup = Boolean.valueOf(request.get("backup"));
    String author = request.get("author");
    String description = request.get("description");
    String licence = request.get("licence");
    String version = request.get("version");
    String name = request.get("name");
    String[] pages = request.getParameterValues("pages");
    boolean all = ArrayUtils.isEmpty(pages);
    if (!context.getWiki().getRightService().hasWikiAdminRights(context)) {
        context.put("message", "needadminrights");
        return "exception";
    }
    if (StringUtils.isEmpty(name)) {
        if (all) {
            name = "backup";
        } else {
            name = "export";
        }
    }
    if (context.getWiki().ParamAsLong("xwiki.action.export.xar.usefilter", 1) == 1) {
        // Create input wiki stream
        DocumentInstanceInputProperties inputProperties = new DocumentInstanceInputProperties();
        // We don't want to log the details
        inputProperties.setVerbose(false);
        inputProperties.setWithJRCSRevisions(history);
        inputProperties.setWithRevisions(false);
        EntityReferenceSet entities = new EntityReferenceSet();
        if (all) {
            entities.includes(new WikiReference(context.getWikiId()));
        } else {
            // Find all page references and add them for processing
            Collection<DocumentReference> pageList = resolvePagesToExport(pages, context);
            for (DocumentReference page : pageList) {
                entities.includes(page);
            }
        }
        inputProperties.setEntities(entities);
        InputFilterStreamFactory inputFilterStreamFactory = Utils.getComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
        InputFilterStream inputFilterStream = inputFilterStreamFactory.createInputFilterStream(inputProperties);
        // Create output wiki stream
        XAROutputProperties xarProperties = new XAROutputProperties();
        // We don't want to log the details
        xarProperties.setVerbose(false);
        XWikiResponse response = context.getResponse();
        xarProperties.setTarget(new DefaultOutputStreamOutputTarget(response.getOutputStream()));
        xarProperties.setPackageName(name);
        if (description != null) {
            xarProperties.setPackageDescription(description);
        }
        if (licence != null) {
            xarProperties.setPackageLicense(licence);
        }
        if (author != null) {
            xarProperties.setPackageAuthor(author);
        }
        if (version != null) {
            xarProperties.setPackageVersion(version);
        }
        xarProperties.setPackageBackupPack(backup);
        xarProperties.setPreserveVersion(backup || history);
        BeanOutputFilterStreamFactory<XAROutputProperties> xarFilterStreamFactory = Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
        OutputFilterStream outputFilterStream = xarFilterStreamFactory.createOutputFilterStream(xarProperties);
        // Export
        response.setContentType("application/zip");
        response.addHeader("Content-disposition", "attachment; filename=" + Util.encodeURI(name, context) + ".xar");
        inputFilterStream.read(outputFilterStream.getFilter());
        inputFilterStream.close();
        outputFilterStream.close();
        // Flush
        response.getOutputStream().flush();
        // Indicate that we are done with the response so no need to add anything
        context.setFinished(true);
    } else {
        PackageAPI export = ((PackageAPI) context.getWiki().getPluginApi("package", context));
        if (export == null) {
            // No Packaging plugin configured
            return "exception";
        }
        export.setWithVersions(history);
        if (author != null) {
            export.setAuthorName(author);
        }
        if (description != null) {
            export.setDescription(description);
        }
        if (licence != null) {
            export.setLicence(licence);
        }
        if (version != null) {
            export.setVersion(version);
        }
        export.setBackupPack(backup);
        export.setName(name);
        if (all) {
            export.backupWiki();
        } else {
            if (pages != null) {
                for (String pageName : pages) {
                    String defaultAction = request.get("action_" + pageName);
                    int iAction;
                    try {
                        iAction = Integer.parseInt(defaultAction);
                    } catch (Exception e) {
                        iAction = 0;
                    }
                    export.add(pageName, iAction);
                }
            }
            export.export();
        }
    }
    return null;
}
Also used : PackageAPI(com.xpn.xwiki.plugin.packaging.PackageAPI) OutputFilterStreamFactory(org.xwiki.filter.output.OutputFilterStreamFactory) BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) InputFilterStreamFactory(org.xwiki.filter.input.InputFilterStreamFactory) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) DefaultOutputStreamOutputTarget(org.xwiki.filter.output.DefaultOutputStreamOutputTarget) InputFilterStream(org.xwiki.filter.input.InputFilterStream) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException) EntityReferenceSet(org.xwiki.model.reference.EntityReferenceSet) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with EntityReferenceSet

use of org.xwiki.model.reference.EntityReferenceSet 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 3 with EntityReferenceSet

use of org.xwiki.model.reference.EntityReferenceSet in project xwiki-platform by xwiki.

the class XARInputFilterStreamTest method testSkipFirstDocument.

@Test
public void testSkipFirstDocument() throws FilterException, IOException, ComponentLookupException {
    XARInputProperties xarProperties = new XARInputProperties();
    xarProperties.setSource(new DefaultFileInputSource(extensionPackager.getExtensionFile(new ExtensionId("xar1", "1.0"))));
    EntityReferenceSet entities = new EntityReferenceSet();
    entities.includes(new LocalDocumentReference("space2", "page2"));
    xarProperties.setEntities(entities);
    assertXML("testSkipFirstDocument", xarProperties);
    assertTrue(this.allLogRule.getMarker(0).contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED));
}
Also used : EntityReferenceSet(org.xwiki.model.reference.EntityReferenceSet) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) DefaultFileInputSource(org.xwiki.filter.input.DefaultFileInputSource) ExtensionId(org.xwiki.extension.ExtensionId) Test(org.junit.Test)

Example 4 with EntityReferenceSet

use of org.xwiki.model.reference.EntityReferenceSet in project xwiki-platform by xwiki.

the class XARInputFilterStreamTest method testSkipLastSpace.

@Test
public void testSkipLastSpace() throws FilterException, IOException, ComponentLookupException {
    XARInputProperties xarProperties = new XARInputProperties();
    xarProperties.setSource(new DefaultFileInputSource(extensionPackager.getExtensionFile(new ExtensionId("xar1", "1.0"))));
    EntityReferenceSet entities = new EntityReferenceSet();
    entities.excludes(new EntityReference("space2", EntityType.SPACE));
    xarProperties.setEntities(entities);
    assertXML("testSkipLastSpace", xarProperties);
    assertTrue(this.allLogRule.getMarker(0).contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED));
}
Also used : EntityReferenceSet(org.xwiki.model.reference.EntityReferenceSet) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) EntityReference(org.xwiki.model.reference.EntityReference) DefaultFileInputSource(org.xwiki.filter.input.DefaultFileInputSource) ExtensionId(org.xwiki.extension.ExtensionId) Test(org.junit.Test)

Aggregations

EntityReferenceSet (org.xwiki.model.reference.EntityReferenceSet)4 XARInputProperties (org.xwiki.filter.xar.input.XARInputProperties)3 Test (org.junit.Test)2 ExtensionId (org.xwiki.extension.ExtensionId)2 DefaultFileInputSource (org.xwiki.filter.input.DefaultFileInputSource)2 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)2 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)2 OutputFilterStreamFactory (org.xwiki.filter.output.OutputFilterStreamFactory)2 EntityReference (org.xwiki.model.reference.EntityReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XWikiException (com.xpn.xwiki.XWikiException)1 XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)1 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)1 Package (com.xpn.xwiki.plugin.packaging.Package)1 PackageAPI (com.xpn.xwiki.plugin.packaging.PackageAPI)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Marker (org.slf4j.Marker)1 FilterException (org.xwiki.filter.FilterException)1 BeanInputFilterStreamFactory (org.xwiki.filter.input.BeanInputFilterStreamFactory)1