Search in sources :

Example 1 with InputFilterStreamFactory

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

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

the class ExportActionTest method exportFullSpaceUsingWildcardsAsXAR.

@Test
public void exportFullSpaceUsingWildcardsAsXAR() throws Exception {
    ExportAction action = new ExportAction();
    XWikiContext context = oldcore.getXWikiContext();
    // Make it a XAR export
    XWikiRequest request = mock(XWikiRequest.class);
    when(request.get("format")).thenReturn("xar");
    context.setRequest(request);
    // Set other request parameters
    when(request.get("name")).thenReturn("myexport");
    // Export all pages in the "Space" space
    when(request.getParameterValues("pages")).thenReturn(new String[] { "Space.%" });
    // Make the current user have programming rights
    when(oldcore.getMockRightService().hasWikiAdminRights(context)).thenReturn(true);
    // Query Manager-related mocking
    QueryManager queryManager = oldcore.getMocker().registerMockComponent(QueryManager.class);
    Query query = mock(Query.class);
    when(queryManager.createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
    when(query.setWiki("xwiki")).thenReturn(query);
    when(query.bindValues(any(List.class))).thenReturn(query);
    when(query.execute()).thenReturn(Arrays.asList("Space.Page1", "Space.Page2"));
    // Register some mock resolver to resolve passed page references
    when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page1", context)).thenReturn(true);
    when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page2", context)).thenReturn(true);
    DocumentReferenceResolver<String> resolver = oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
    when(resolver.resolve("xwiki:Space.Page1")).thenReturn(new DocumentReference("xwiki", "Space", "Page1"));
    when(resolver.resolve("xwiki:Space.Page2")).thenReturn(new DocumentReference("xwiki", "Space", "Page2"));
    // Register some mock filters so that the export does nothing.
    InputFilterStreamFactory inputFilterStreamFactory = oldcore.getMocker().registerMockComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
    when(inputFilterStreamFactory.createInputFilterStream(anyMap())).thenReturn(mock(InputFilterStream.class));
    BeanOutputFilterStreamFactory beanOutputFilterStreamFactory = mock(BeanOutputFilterStreamFactory.class);
    oldcore.getMocker().registerComponent(OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize(), beanOutputFilterStreamFactory);
    when(beanOutputFilterStreamFactory.createOutputFilterStream(any(XAROutputProperties.class))).thenReturn(mock(BeanOutputFilterStream.class));
    // Set response stream
    XWikiResponse response = mock(XWikiResponse.class);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    context.setResponse(response);
    String result = action.render(oldcore.getXWikiContext());
    // The tests are below this line!
    // Verify null is returned (this means the response has been returned)
    assertNull(result);
    // Verify that the parameters passed to the input stream factory are defining the correct pages
    ArgumentCaptor<DocumentInstanceInputProperties> properties = ArgumentCaptor.forClass(DocumentInstanceInputProperties.class);
    verify(inputFilterStreamFactory).createInputFilterStream(properties.capture());
    assertEquals(false, properties.getValue().isVerbose());
    assertEquals(false, properties.getValue().isWithJRCSRevisions());
    assertEquals(false, properties.getValue().isWithRevisions());
    assertEquals(true, properties.getValue().getEntities().matches(new DocumentReference("xwiki", "Space", "Page1")));
    assertEquals(true, properties.getValue().getEntities().matches(new DocumentReference("xwiki", "Space", "Page2")));
}
Also used : BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) BeanOutputFilterStream(org.xwiki.filter.output.BeanOutputFilterStream) Query(org.xwiki.query.Query) ServletOutputStream(javax.servlet.ServletOutputStream) XWikiContext(com.xpn.xwiki.XWikiContext) InputFilterStreamFactory(org.xwiki.filter.input.InputFilterStreamFactory) InputFilterStream(org.xwiki.filter.input.InputFilterStream) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) QueryManager(org.xwiki.query.QueryManager) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

InputFilterStream (org.xwiki.filter.input.InputFilterStream)2 InputFilterStreamFactory (org.xwiki.filter.input.InputFilterStreamFactory)2 DocumentInstanceInputProperties (org.xwiki.filter.instance.input.DocumentInstanceInputProperties)2 BeanOutputFilterStreamFactory (org.xwiki.filter.output.BeanOutputFilterStreamFactory)2 XAROutputProperties (org.xwiki.filter.xar.output.XAROutputProperties)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 PackageAPI (com.xpn.xwiki.plugin.packaging.PackageAPI)1 IOException (java.io.IOException)1 List (java.util.List)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Test (org.junit.Test)1 FilterException (org.xwiki.filter.FilterException)1 BeanOutputFilterStream (org.xwiki.filter.output.BeanOutputFilterStream)1 DefaultOutputStreamOutputTarget (org.xwiki.filter.output.DefaultOutputStreamOutputTarget)1 OutputFilterStream (org.xwiki.filter.output.OutputFilterStream)1 OutputFilterStreamFactory (org.xwiki.filter.output.OutputFilterStreamFactory)1 EntityReferenceSet (org.xwiki.model.reference.EntityReferenceSet)1 WikiReference (org.xwiki.model.reference.WikiReference)1