use of com.xpn.xwiki.web.ExportURLFactory in project xwiki-platform by xwiki.
the class HtmlPackager method export.
/**
* Apply export and create the ZIP package.
*
* @param context the XWiki context used to render pages.
* @throws IOException error when creating the package.
* @throws XWikiException error when render the pages.
*/
public void export(XWikiContext context) throws IOException, XWikiException {
context.getResponse().setContentType("application/zip");
context.getResponse().addHeader("Content-disposition", "attachment; filename=" + Util.encodeURI(this.name, context) + ".zip");
context.setFinished(true);
ZipOutputStream zos = new ZipOutputStream(context.getResponse().getOutputStream());
File dir = this.environment.getTemporaryDirectory();
File tempdir = new File(dir, RandomStringUtils.randomAlphanumeric(8));
tempdir.mkdirs();
File attachmentDir = new File(tempdir, "attachment");
attachmentDir.mkdirs();
// Create and initialize a custom URL factory
ExportURLFactory urlf = new ExportURLFactory();
Provider<FilesystemExportContext> exportContextProvider = Utils.getComponent(new DefaultParameterizedType(null, Provider.class, FilesystemExportContext.class));
// Note that the following line will set a FilesystemExportContext instance in the Execution Context
// and this Execution Context will be cloned for each document rendered below.
// TODO: to be cleaner we should set the FilesystemExportContext in the EC used to render each document.
// However we also need to initialize the ExportURLFactory.
FilesystemExportContext exportContext = exportContextProvider.get();
urlf.init(this.pageReferences, tempdir, exportContext, context);
// Render pages to export
renderDocuments(zos, urlf, context);
// Add required skins to ZIP file
for (String skinName : urlf.getFilesystemExportContext().getNeededSkins()) {
addSkinToZip(skinName, zos, urlf.getFilesystemExportContext().getExportedSkinFiles(), context);
}
// Copy generated files in the ZIP file.
addDirToZip(tempdir, TrueFileFilter.TRUE, zos, "", null);
// Generate an index page
generateIndexPage(zos, context);
zos.setComment(this.description);
// Finish ZIP file
zos.finish();
zos.flush();
// Delete temporary directory
deleteDirectory(tempdir);
}
Aggregations