Search in sources :

Example 1 with XARInputProperties

use of org.xwiki.filter.xar.input.XARInputProperties 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 XARInputProperties

use of org.xwiki.filter.xar.input.XARInputProperties in project xwiki-platform by xwiki.

the class Packager method getXWikiDocument.

public XWikiDocument getXWikiDocument(InputStream source, WikiReference wikiReference) throws FilterException, IOException, ComponentLookupException {
    // Output
    DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties();
    documentProperties.setDefaultReference(wikiReference);
    documentProperties.setVersionPreserved(false);
    // Input
    XARInputProperties xarProperties = new XARInputProperties();
    xarProperties.setWithHistory(false);
    return this.documentImporter.importDocument(new DefaultInputStreamInputSource(source), xarProperties, documentProperties);
}
Also used : DefaultInputStreamInputSource(org.xwiki.filter.input.DefaultInputStreamInputSource) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties)

Example 3 with XARInputProperties

use of org.xwiki.filter.xar.input.XARInputProperties in project xwiki-platform by xwiki.

the class XWikiDocument method fromXML.

public void fromXML(InputSource source, boolean withArchive) throws XWikiException {
    // Output
    DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties();
    XWikiContext xcontext = getXWikiContext();
    if (xcontext != null) {
        documentProperties.setDefaultReference(getXWikiContext().getWikiReference());
    }
    documentProperties.setVersionPreserved(withArchive);
    // Input
    XARInputProperties xarProperties = new XARInputProperties();
    xarProperties.setWithHistory(withArchive);
    try {
        Utils.getComponent(XWikiDocumentFilterUtils.class).importEntity(XWikiDocument.class, this, source, xarProperties, documentProperties);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING, "Error parsing xml", e, null);
    }
    // We have been reading from XML so the document does not need a new version when saved
    setMetaDataDirty(false);
    setContentDirty(false);
}
Also used : XWikiDocumentFilterUtils(com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) ExecutionContextException(org.xwiki.context.ExecutionContextException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MissingParserException(org.xwiki.rendering.parser.MissingParserException) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) QueryException(org.xwiki.query.QueryException) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with XARInputProperties

use of org.xwiki.filter.xar.input.XARInputProperties in project xwiki-platform by xwiki.

the class XWikiDocumentFilterUtils method importEntity.

/**
 * @param entityClass to class used to find the {@link EntityOutputFilterStream} component
 * @param entity the entity to write to or null to create a new entity of the passed class
 * @param source the stream to read
 * @param xarProperties the configuration of the input filter
 * @param documentProperties the configuration of the output filter
 * @return the imported entity, same as {@code entity} if not null
 * @throws FilterException when failing to import
 * @throws IOException when failing to import
 * @throws ComponentLookupException when failing to find a EntityOutputFilterStream corresponding to passed class
 */
public <T> T importEntity(Class<T> entityClass, T entity, InputSource source, XARInputProperties xarProperties, DocumentInstanceOutputProperties documentProperties) throws FilterException, IOException, ComponentLookupException {
    // Output
    EntityOutputFilterStream<T> filterStream = this.componentManager.getInstance(new DefaultParameterizedType(null, EntityOutputFilterStream.class, entityClass));
    filterStream.setProperties(documentProperties);
    filterStream.setEntity(entity);
    if (filterStream instanceof XWikiDocumentOutputFilterStream) {
        ((XWikiDocumentOutputFilterStream) filterStream).disableRenderingEvents();
    }
    // Input
    xarProperties.setSourceType(getSourceType(entityClass));
    xarProperties.setSource(source);
    BeanInputFilterStream<XARInputProperties> xarReader = ((BeanInputFilterStreamFactory<XARInputProperties>) this.xarInputFilterStreamFactory).createInputFilterStream(xarProperties);
    // Convert
    xarReader.read(filterStream.getFilter());
    xarReader.close();
    return filterStream.getEntity();
}
Also used : EntityOutputFilterStream(com.xpn.xwiki.internal.filter.output.EntityOutputFilterStream) XWikiDocumentOutputFilterStream(com.xpn.xwiki.internal.filter.output.XWikiDocumentOutputFilterStream) BeanInputFilterStreamFactory(org.xwiki.filter.input.BeanInputFilterStreamFactory) XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType)

Example 5 with XARInputProperties

use of org.xwiki.filter.xar.input.XARInputProperties in project xwiki-platform by xwiki.

the class XWikiDocumentFilterUtils method importEntity.

/**
 * @param entity the entity to write to or its class to create a new one
 * @param source the stream to read
 * @return the imported entity, same as {@code entity} if not null
 * @throws FilterException when failing to import
 * @throws IOException when failing to import
 * @throws ComponentLookupException when failing to find a EntityOutputFilterStream corresponding to passed class
 */
public <T> T importEntity(Object entity, InputSource source) throws FilterException, IOException, ComponentLookupException {
    // Output
    DocumentInstanceOutputProperties documentProperties = new DocumentInstanceOutputProperties();
    // Input
    XARInputProperties xarProperties = new XARInputProperties();
    return importEntity(getClass(entity), entity instanceof Class ? null : (T) entity, source, xarProperties, documentProperties);
}
Also used : XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Aggregations

XARInputProperties (org.xwiki.filter.xar.input.XARInputProperties)8 DocumentInstanceOutputProperties (org.xwiki.filter.instance.output.DocumentInstanceOutputProperties)4 EntityReferenceSet (org.xwiki.model.reference.EntityReferenceSet)3 Test (org.junit.Test)2 ExtensionId (org.xwiki.extension.ExtensionId)2 BeanInputFilterStreamFactory (org.xwiki.filter.input.BeanInputFilterStreamFactory)2 DefaultFileInputSource (org.xwiki.filter.input.DefaultFileInputSource)2 DefaultInputStreamInputSource (org.xwiki.filter.input.DefaultInputStreamInputSource)2 EntityReference (org.xwiki.model.reference.EntityReference)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XARImportedEvent (com.xpn.xwiki.internal.event.XARImportedEvent)1 XARImportingEvent (com.xpn.xwiki.internal.event.XARImportingEvent)1 XWikiDocumentFilterUtils (com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils)1 EntityOutputFilterStream (com.xpn.xwiki.internal.filter.output.EntityOutputFilterStream)1 XWikiDocumentOutputFilterStream (com.xpn.xwiki.internal.filter.output.XWikiDocumentOutputFilterStream)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)1 Package (com.xpn.xwiki.plugin.packaging.Package)1