Search in sources :

Example 1 with PackageAPI

use of com.xpn.xwiki.plugin.packaging.PackageAPI 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 PackageAPI

use of com.xpn.xwiki.plugin.packaging.PackageAPI in project xwiki-platform by xwiki.

the class ImportAction method importPackageOld.

private void importPackageOld(XWikiAttachment packFile, XWikiRequest request, XWikiContext context) throws IOException, XWikiException {
    PackageAPI importer = ((PackageAPI) context.getWiki().getPluginApi("package", context));
    String[] pages = request.getParameterValues("pages");
    importer.Import(packFile.getContentInputStream(context));
    if (pages != null) {
        // Skip document by default
        List<DocumentInfoAPI> filelist = importer.getFiles();
        for (DocumentInfoAPI dia : filelist) {
            dia.setAction(DocumentInfo.ACTION_SKIP);
        }
        // Indicate with documents to import
        for (String pageEntry : pages) {
            String language = getLocale(pageEntry, request);
            int iAction = getAction(pageEntry, language, request);
            String docName = getDocumentReference(pageEntry);
            if (language == null) {
                importer.setDocumentAction(docName, iAction);
            } else {
                importer.setDocumentAction(docName, language, iAction);
            }
        }
    }
    // Set the appropriate strategy to handle versions
    if (StringUtils.equals(request.getParameter("historyStrategy"), "reset")) {
        importer.setPreserveVersion(false);
        importer.setWithVersions(false);
    } else if (StringUtils.equals(request.getParameter("historyStrategy"), "replace")) {
        importer.setPreserveVersion(false);
        importer.setWithVersions(true);
    } else {
        importer.setPreserveVersion(true);
        importer.setWithVersions(false);
    }
    // Set the backup pack option
    if (StringUtils.equals(request.getParameter("importAsBackup"), "true")) {
        importer.setBackupPack(true);
    } else {
        importer.setBackupPack(false);
    }
    // Import files
    importer.install();
}
Also used : PackageAPI(com.xpn.xwiki.plugin.packaging.PackageAPI) DocumentInfoAPI(com.xpn.xwiki.plugin.packaging.DocumentInfoAPI)

Example 3 with PackageAPI

use of com.xpn.xwiki.plugin.packaging.PackageAPI in project xwiki-platform by xwiki.

the class WikiResourceImpl method importXAR.

@Override
public Wiki importXAR(String wikiName, Boolean backup, String history, InputStream is) throws XWikiRestException {
    try {
        if (!wikiExists(wikiName)) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        /* Use the package plugin for importing pages */
        XWikiContext xwikiContext = getXWikiContext();
        PackageAPI importer = ((PackageAPI) xwikiContext.getWiki().getPluginApi("package", xwikiContext));
        if (importer == null) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "Can't access Package plugin API. Generally mean you don't have enough rights.");
        }
        String database = xwikiContext.getWikiId();
        try {
            xwikiContext.setWikiId(wikiName);
            importer.setBackupPack(backup);
            importer.Import(is);
            HistoryOptions historyOption = parseHistoryOption(history, HistoryOptions.ADD);
            switch(historyOption) {
                case RESET:
                    importer.setPreserveVersion(false);
                    importer.setWithVersions(false);
                    break;
                case REPLACE:
                    importer.setPreserveVersion(false);
                    importer.setWithVersions(true);
                    break;
                default:
                case ADD:
                    importer.setPreserveVersion(true);
                    importer.setWithVersions(false);
                    break;
            }
            // Set the backup pack option
            importer.setBackupPack(backup);
            if (importer.install() == com.xpn.xwiki.plugin.packaging.DocumentInfo.INSTALL_IMPOSSIBLE) {
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        } catch (IOException e) {
            throw new WebApplicationException(e);
        } finally {
            xwikiContext.setWikiId(database);
        }
        return DomainObjectFactory.createWiki(objectFactory, uriInfo.getBaseUri(), wikiName);
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) PackageAPI(com.xpn.xwiki.plugin.packaging.PackageAPI) XWikiRestException(org.xwiki.rest.XWikiRestException) XWikiContext(com.xpn.xwiki.XWikiContext) IOException(java.io.IOException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

PackageAPI (com.xpn.xwiki.plugin.packaging.PackageAPI)3 XWikiException (com.xpn.xwiki.XWikiException)2 IOException (java.io.IOException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 DocumentInfoAPI (com.xpn.xwiki.plugin.packaging.DocumentInfoAPI)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 FilterException (org.xwiki.filter.FilterException)1 InputFilterStream (org.xwiki.filter.input.InputFilterStream)1 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)1 DocumentInstanceInputProperties (org.xwiki.filter.instance.input.DocumentInstanceInputProperties)1 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)1 DefaultOutputStreamOutputTarget (org.xwiki.filter.output.DefaultOutputStreamOutputTarget)1 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)1 OutputFilterStreamFactory (org.xwiki.filter.output.OutputFilterStreamFactory)1 XAROutputProperties (org.xwiki.filter.xar.output.XAROutputProperties)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReferenceSet (org.xwiki.model.reference.EntityReferenceSet)1 WikiReference (org.xwiki.model.reference.WikiReference)1 QueryException (org.xwiki.query.QueryException)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1