Search in sources :

Example 1 with OutputFilterStream

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

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

the class AbstractInstanceFilterStreamTest method importFromXML.

protected void importFromXML(String resource, InstanceOutputProperties instanceProperties) throws FilterException {
    if (instanceProperties == null) {
        instanceProperties = new InstanceOutputProperties();
        instanceProperties.setVerbose(false);
    }
    OutputFilterStream outputFilterStream = this.outputFilterStreamFactory.createOutputFilterStream(instanceProperties);
    URL url = getClass().getResource("/filter/" + resource + ".xml");
    FilterXMLInputProperties properties = new FilterXMLInputProperties();
    properties.setSource(new DefaultURLInputSource(url));
    InputFilterStream inputFilterStream = this.xmlInputFilterStreamFactory.createInputFilterStream(properties);
    inputFilterStream.read(outputFilterStream.getFilter());
    try {
        inputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close input wiki stream", e);
    }
    try {
        outputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close output wiki stream", e);
    }
}
Also used : InstanceOutputProperties(org.xwiki.filter.instance.output.InstanceOutputProperties) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) DefaultURLInputSource(org.xwiki.filter.input.DefaultURLInputSource) FilterException(org.xwiki.filter.FilterException) FilterXMLInputProperties(org.xwiki.filter.filterxml.input.FilterXMLInputProperties) IOException(java.io.IOException) InputFilterStream(org.xwiki.filter.input.InputFilterStream) URL(java.net.URL)

Example 3 with OutputFilterStream

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

the class AbstractInstanceInputFilterStreamTest method assertXML.

protected void assertXML(String resource, InstanceInputProperties instanceProperties) throws FilterException, IOException {
    if (instanceProperties == null) {
        instanceProperties = new InstanceInputProperties();
        instanceProperties.setVerbose(false);
    }
    URL url = getClass().getResource("/filter/" + resource + ".xml");
    String expected = IOUtils.toString(url, "UTF-8");
    expected = StringUtils.removeStart(expected, "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n\n");
    InputFilterStream inputFilterStream = this.inputFilterStreamFactory.createInputFilterStream(instanceProperties);
    StringWriterOutputTarget writer = new StringWriterOutputTarget();
    FilterXMLOutputProperties properties = new FilterXMLOutputProperties();
    properties.setTarget(writer);
    OutputFilterStream outputFilterStream = this.xmlOutputFilterStreamFactory.createOutputFilterStream(properties);
    inputFilterStream.read(outputFilterStream.getFilter());
    inputFilterStream.close();
    outputFilterStream.close();
    Assert.assertEquals(expected, writer.getBuffer().toString());
}
Also used : FilterXMLOutputProperties(org.xwiki.filter.filterxml.output.FilterXMLOutputProperties) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) InstanceInputProperties(org.xwiki.filter.instance.input.InstanceInputProperties) StringWriterOutputTarget(org.xwiki.filter.output.StringWriterOutputTarget) InputFilterStream(org.xwiki.filter.input.InputFilterStream) URL(java.net.URL)

Example 4 with OutputFilterStream

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

the class XARInputFilterStreamTest method assertXML.

private void assertXML(String resource, XARInputProperties xarProperties) throws FilterException, IOException, ComponentLookupException {
    URL url = getClass().getResource("/xar/" + resource + ".output.xml");
    String expected = IOUtils.toString(url, "UTF-8");
    expected = StringUtils.removeStart(expected, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
    BeanInputFilterStreamFactory<XARInputProperties> inputFilterStreamFactory = this.mocker.getInstance(InputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
    InputFilterStream inputFilterStream = inputFilterStreamFactory.createInputFilterStream(xarProperties);
    StringWriterOutputTarget writer = new StringWriterOutputTarget();
    FilterXMLOutputProperties properties = new FilterXMLOutputProperties();
    properties.setTarget(writer);
    BeanOutputFilterStreamFactory<FilterXMLOutputProperties> xmlOutputFilterStreamFactory = this.mocker.getInstance(OutputFilterStreamFactory.class, FilterStreamType.FILTER_XML.serialize());
    OutputFilterStream outputFilterStream = xmlOutputFilterStreamFactory.createOutputFilterStream(properties);
    inputFilterStream.read(outputFilterStream.getFilter());
    inputFilterStream.close();
    outputFilterStream.close();
    Assert.assertEquals(expected, writer.getBuffer().toString());
}
Also used : XARInputProperties(org.xwiki.filter.xar.input.XARInputProperties) FilterXMLOutputProperties(org.xwiki.filter.filterxml.output.FilterXMLOutputProperties) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) StringWriterOutputTarget(org.xwiki.filter.output.StringWriterOutputTarget) InputFilterStream(org.xwiki.filter.input.InputFilterStream) URL(java.net.URL)

Example 5 with OutputFilterStream

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

the class ExtensionInstanceOutputFilterStreamTest method importFromXML.

protected void importFromXML(String resource, InstanceOutputProperties instanceProperties) throws FilterException {
    if (instanceProperties == null) {
        instanceProperties = new InstanceOutputProperties();
        instanceProperties.setVerbose(false);
    }
    OutputFilterStream outputFilterStream = this.outputFilterStreamFactory.createOutputFilterStream(instanceProperties);
    URL url = getClass().getResource("/" + resource + ".xml");
    FilterXMLInputProperties properties = new FilterXMLInputProperties();
    properties.setSource(new DefaultURLInputSource(url));
    InputFilterStream inputFilterStream = this.xmlInputFilterStreamFactory.createInputFilterStream(properties);
    inputFilterStream.read(outputFilterStream.getFilter());
    try {
        inputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close input wiki stream", e);
    }
    try {
        outputFilterStream.close();
    } catch (IOException e) {
        throw new FilterException("Failed to close output wiki stream", e);
    }
}
Also used : ExtensionInstanceOutputProperties(org.xwiki.filter.instance.output.ExtensionInstanceOutputProperties) InstanceOutputProperties(org.xwiki.filter.instance.output.InstanceOutputProperties) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) DefaultURLInputSource(org.xwiki.filter.input.DefaultURLInputSource) FilterException(org.xwiki.filter.FilterException) FilterXMLInputProperties(org.xwiki.filter.filterxml.input.FilterXMLInputProperties) IOException(java.io.IOException) InputFilterStream(org.xwiki.filter.input.InputFilterStream) URL(java.net.URL)

Aggregations

InputFilterStream (org.xwiki.filter.input.InputFilterStream)5 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)5 URL (java.net.URL)4 IOException (java.io.IOException)3 FilterException (org.xwiki.filter.FilterException)3 FilterXMLInputProperties (org.xwiki.filter.filterxml.input.FilterXMLInputProperties)2 FilterXMLOutputProperties (org.xwiki.filter.filterxml.output.FilterXMLOutputProperties)2 DefaultURLInputSource (org.xwiki.filter.input.DefaultURLInputSource)2 InstanceOutputProperties (org.xwiki.filter.instance.output.InstanceOutputProperties)2 StringWriterOutputTarget (org.xwiki.filter.output.StringWriterOutputTarget)2 XWikiException (com.xpn.xwiki.XWikiException)1 PackageAPI (com.xpn.xwiki.plugin.packaging.PackageAPI)1 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)1 DocumentInstanceInputProperties (org.xwiki.filter.instance.input.DocumentInstanceInputProperties)1 InstanceInputProperties (org.xwiki.filter.instance.input.InstanceInputProperties)1 ExtensionInstanceOutputProperties (org.xwiki.filter.instance.output.ExtensionInstanceOutputProperties)1 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)1 DefaultOutputStreamOutputTarget (org.xwiki.filter.output.DefaultOutputStreamOutputTarget)1 OutputFilterStreamFactory (org.xwiki.filter.output.OutputFilterStreamFactory)1 XARInputProperties (org.xwiki.filter.xar.input.XARInputProperties)1