Search in sources :

Example 1 with XAROutputProperties

use of org.xwiki.filter.xar.output.XAROutputProperties 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 XAROutputProperties

use of org.xwiki.filter.xar.output.XAROutputProperties in project xwiki-platform by xwiki.

the class XWikiDocument method toXML.

/**
 * Serialize the document to an OutputStream.
 *
 * @param out the output where to write the XML
 * @param bWithObjects include XObjects
 * @param bWithRendering include the rendered content
 * @param bWithAttachmentContent include attachments content
 * @param bWithVersions include archived versions
 * @param format true if the XML should be formated
 * @param encoding the encoding to use to write the XML
 * @throws XWikiException when an errors occurs during wiki operations
 * @since 9.0RC1
 */
public void toXML(OutputTarget out, boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent, boolean bWithVersions, boolean format, String encoding) throws XWikiException {
    // Input
    DocumentInstanceInputProperties documentProperties = new DocumentInstanceInputProperties();
    documentProperties.setWithWikiObjects(bWithObjects);
    documentProperties.setWithWikiDocumentContentHTML(bWithRendering);
    documentProperties.setWithWikiAttachmentsContent(bWithAttachmentContent);
    documentProperties.setWithJRCSRevisions(bWithVersions);
    documentProperties.setWithRevisions(false);
    // Output
    XAROutputProperties xarProperties = new XAROutputProperties();
    xarProperties.setPreserveVersion(bWithVersions);
    xarProperties.setEncoding(encoding);
    xarProperties.setFormat(format);
    try {
        Utils.getComponent(XWikiDocumentFilterUtils.class).exportEntity(this, out, xarProperties, documentProperties);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING, "Error parsing xml", e, null);
    }
}
Also used : DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) XWikiDocumentFilterUtils(com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) 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 3 with XAROutputProperties

use of org.xwiki.filter.xar.output.XAROutputProperties in project xwiki-platform by xwiki.

the class XWikiDocumentFilterUtils method exportEntity.

/**
 * @param entity the entity to read
 * @param target the target where to write the result
 * @param xarProperties the configuration of the output filter
 * @param documentProperties the configuration of the input filter
 * @throws ComponentLookupException failed to find an event generator for passed entity
 * @throws FilterException when failing to generate export the passed entity
 * @throws IOException when failing to close the stream
 */
public void exportEntity(Object entity, OutputTarget target, XAROutputProperties xarProperties, DocumentInstanceInputProperties documentProperties) throws ComponentLookupException, FilterException, IOException {
    // Input
    documentProperties.setVerbose(false);
    // Output
    xarProperties.setForceDocument(true);
    if (target != null) {
        xarProperties.setTarget(target);
    }
    xarProperties.setVerbose(false);
    BeanOutputFilterStream<XAROutputProperties> xarFilter = ((BeanOutputFilterStreamFactory<XAROutputProperties>) this.xarOutputFilterStreamFactory).createOutputFilterStream(xarProperties);
    XARFilter filter = (XARFilter) xarFilter.getFilter();
    BeanEntityEventGenerator<Object, DocumentInstanceInputProperties> generator = this.componentManager.getInstance(new DefaultParameterizedType(null, EntityEventGenerator.class, getClass(entity)));
    // Spaces and document events
    FilterEventParameters documentParameters = null;
    DocumentReference documentReference = null;
    if (entity instanceof XWikiDocument) {
        documentReference = ((XWikiDocument) entity).getDocumentReference();
        for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
            filter.beginWikiSpace(spaceReference.getName(), FilterEventParameters.EMPTY);
        }
        documentParameters = new FilterEventParameters();
        documentParameters.put(WikiDocumentFilter.PARAMETER_LOCALE, ((XWikiDocument) entity).getDefaultLocale());
        filter.beginWikiDocument(documentReference.getName(), documentParameters);
    }
    // Document Locale events
    generator.write(entity, xarFilter, documentProperties);
    // Document and spaces events
    if (documentParameters != null) {
        filter.endWikiDocument(documentReference.getName(), documentParameters);
        documentReference = ((XWikiDocument) entity).getDocumentReference();
        for (EntityReference reference = documentReference.getParent(); reference instanceof SpaceReference; reference = reference.getParent()) {
            filter.beginWikiSpace(reference.getName(), FilterEventParameters.EMPTY);
        }
    }
    xarFilter.close();
}
Also used : BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) SpaceReference(org.xwiki.model.reference.SpaceReference) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) FilterEventParameters(org.xwiki.filter.FilterEventParameters) BeanEntityEventGenerator(org.xwiki.filter.instance.input.BeanEntityEventGenerator) EntityEventGenerator(org.xwiki.filter.instance.input.EntityEventGenerator) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) XARFilter(org.xwiki.filter.xar.internal.XARFilter) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with XAROutputProperties

use of org.xwiki.filter.xar.output.XAROutputProperties in project xwiki-platform by xwiki.

the class BaseElement method toXMLString.

/**
 * @param format true if the XML should be formated
 * @return the XML as a String
 * @since 9.0RC1
 */
public String toXMLString(boolean format) {
    XAROutputProperties xarProperties = new XAROutputProperties();
    xarProperties.setFormat(false);
    try {
        return Utils.getComponent(XWikiDocumentFilterUtils.class).exportEntity(this, xarProperties);
    } catch (Exception e) {
        LOGGER.error("Failed to serialize collection to XML", e);
        return "";
    }
}
Also used : XWikiDocumentFilterUtils(com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) XWikiException(com.xpn.xwiki.XWikiException) IOException(java.io.IOException)

Example 5 with XAROutputProperties

use of org.xwiki.filter.xar.output.XAROutputProperties in project xwiki-platform by xwiki.

the class XWikiAttachment method toXML.

/**
 * Write an XML representation of the attachment into an {@link com.xpn.xwiki.internal.xml.XMLWriter}
 *
 * @param out the output where to write the XML
 * @param bWithAttachmentContent if true, binary content of the attachment is included (base64 encoded)
 * @param bWithVersions if true, all archive version is also included
 * @param format true if the XML should be formated
 * @param encoding the encoding to use when serializing XML
 * @throws XWikiException when an error occurs during xwiki operation
 * @since 9.10RC1
 */
public void toXML(OutputTarget out, boolean bWithAttachmentContent, boolean bWithVersions, boolean format, String encoding) throws XWikiException {
    // Input
    DocumentInstanceInputProperties documentProperties = new DocumentInstanceInputProperties();
    documentProperties.setWithWikiAttachmentsContent(bWithAttachmentContent);
    documentProperties.setWithJRCSRevisions(bWithVersions);
    documentProperties.setWithRevisions(false);
    // Output
    XAROutputProperties xarProperties = new XAROutputProperties();
    xarProperties.setPreserveVersion(bWithVersions);
    xarProperties.setEncoding(encoding);
    xarProperties.setFormat(format);
    try {
        Utils.getComponent(XWikiDocumentFilterUtils.class).exportEntity(this, out, xarProperties, documentProperties);
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING, "Error parsing xml", e, null);
    }
}
Also used : DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) XWikiDocumentFilterUtils(com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XAROutputProperties (org.xwiki.filter.xar.output.XAROutputProperties)5 XWikiException (com.xpn.xwiki.XWikiException)4 IOException (java.io.IOException)4 DocumentInstanceInputProperties (org.xwiki.filter.instance.input.DocumentInstanceInputProperties)4 XWikiDocumentFilterUtils (com.xpn.xwiki.internal.filter.XWikiDocumentFilterUtils)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 QueryException (org.xwiki.query.QueryException)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 PackageAPI (com.xpn.xwiki.plugin.packaging.PackageAPI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 ExecutionContextException (org.xwiki.context.ExecutionContextException)1 FilterEventParameters (org.xwiki.filter.FilterEventParameters)1 FilterException (org.xwiki.filter.FilterException)1 InputFilterStream (org.xwiki.filter.input.InputFilterStream)1 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)1