Search in sources :

Example 1 with FilterException

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

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class ExtensionInstanceOutputFilterStream method endExtension.

@Override
public void endExtension(String id, String version, FilterEventParameters parameters) throws FilterException {
    // TODO: add support for complete extension
    ExtensionId extensionId = new ExtensionId(id, factory.getVersion(version));
    try {
        LocalExtension localExtension = this.localRepository.getLocalExtension(extensionId);
        if (localExtension == null) {
            Extension extension;
            try {
                // Try to find and download the extension from a repository
                extension = this.extensionRepository.resolve(extensionId);
            } catch (ResolveException e) {
                this.logger.debug("Can't find extension [{}]", extensionId, e);
                // FIXME: Create a dummy extension. Need support for partial/lazy extension.
                return;
            }
            localExtension = this.localRepository.storeExtension(extension);
        }
        String namespace = getCurrentNamespace();
        // TODO: should probably make it configurable
        if (installedRepository.getInstalledExtension(localExtension.getId().getId(), namespace) == null) {
            for (ExtensionId feature : localExtension.getExtensionFeatures()) {
                if (installedRepository.getInstalledExtension(feature.getId(), namespace) != null) {
                    // Already exist so don't register it or it could create a mess
                    return;
                }
            }
        } else {
            return;
        }
        // Register the extension as installed
        installedRepository.installExtension(localExtension, namespace, false);
    } catch (Exception e) {
        this.logger.error("Failed to register extenion [{}] from the XAR", extensionId, e);
    }
}
Also used : Extension(org.xwiki.extension.Extension) LocalExtension(org.xwiki.extension.LocalExtension) ResolveException(org.xwiki.extension.ResolveException) LocalExtension(org.xwiki.extension.LocalExtension) ExtensionId(org.xwiki.extension.ExtensionId) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) FilterException(org.xwiki.filter.FilterException)

Example 3 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class AbstractBeanEntityEventGenerator method write.

@Override
protected void write(E entity, Object filter, F internalFilter, Map<String, Object> properties) throws FilterException {
    P propertiesBean;
    if (this.propertiesType.isInstance(properties)) {
        propertiesBean = (P) properties;
    } else {
        try {
            propertiesBean = this.propertiesType.newInstance();
            this.beanManager.populate(propertiesBean, properties);
        } catch (Exception e) {
            throw new FilterException("Failed to convert properties to Java bean", e);
        }
    }
    write(entity, filter, internalFilter, propertiesBean);
}
Also used : FilterException(org.xwiki.filter.FilterException) FilterException(org.xwiki.filter.FilterException) InitializationException(org.xwiki.component.phase.InitializationException)

Example 4 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class XAROutputFilterStream method endWikiClassProperty.

@Override
public void endWikiClassProperty(String name, String type, FilterEventParameters parameters) throws FilterException {
    try {
        this.writer.writeElement(XARClassPropertyModel.ELEMENT_CLASSTYPE, type);
        this.writer.writeEndElement();
    } catch (Exception e) {
        throw new FilterException(String.format("Failed to write end property [%s] from class [%s] with version [%s]", name, this.currentDocumentReference, this.currentDocumentVersion), e);
    }
}
Also used : FilterException(org.xwiki.filter.FilterException) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException)

Example 5 with FilterException

use of org.xwiki.filter.FilterException in project xwiki-platform by xwiki.

the class XAROutputFilterStream method close.

@Override
public void close() throws IOException {
    if (this.writer != null) {
        try {
            this.writer.close();
            this.writer = null;
        } catch (FilterException e) {
            throw new IOException("Failed to close XML writer", e);
        }
    }
    if (this.wikiWriter != null) {
        this.wikiWriter.close();
        this.wikiWriter = null;
    }
    this.properties.getTarget().close();
}
Also used : FilterException(org.xwiki.filter.FilterException) IOException(java.io.IOException)

Aggregations

FilterException (org.xwiki.filter.FilterException)35 IOException (java.io.IOException)17 XWikiException (com.xpn.xwiki.XWikiException)10 XWikiContext (com.xpn.xwiki.XWikiContext)7 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 Date (java.util.Date)6 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 DocumentReference (org.xwiki.model.reference.DocumentReference)5 ArrayList (java.util.ArrayList)4 InputFilterStream (org.xwiki.filter.input.InputFilterStream)3 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)3 WikiReference (org.xwiki.model.reference.WikiReference)3 QueryException (org.xwiki.query.QueryException)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 ComponentManager (org.xwiki.component.manager.ComponentManager)2