use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class ExportURLFactoryMockitoTest method createURLWhenNotInExportedPages.
@Test
public void createURLWhenNotInExportedPages() throws Exception {
DocumentReference pageReference = new DocumentReference("xwiki", "Main", "WebHome");
when(this.currentDocumentReferenceResolver.resolve("Main.WebHome")).thenReturn(pageReference);
when(this.defaultEntityReferenceSerializer.serialize(pageReference)).thenReturn("xwiki:Main.WebHome");
when(this.legacySpaceResolver.resolve("Main")).thenReturn(Arrays.asList("Main"));
when(this.fsPathEntityReferenceSerializer.serialize(pageReference)).thenReturn("Main/WebHome");
DocumentReference pageReference2 = new DocumentReference("xwiki", "Main", "SomeOtherPage");
when(this.fsPathEntityReferenceSerializer.serialize(pageReference2)).thenReturn("Main/SomeOtherPage");
when(this.relativeEntityReferenceResolver.resolve("Main", EntityType.SPACE)).thenReturn(new EntityReference("Main", EntityType.SPACE));
this.factory.init(Arrays.asList(pageReference), null, new FilesystemExportContext(), this.context);
assertEquals(new URL("http://localhost:8080/xwiki/bin/Main/SomeOtherPage"), this.factory.createURL("Main", "SomeOtherPage", "view", null, null, "xwiki", this.context));
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class ExportURLFactoryMockitoTest method createURLWhenInExportedPages.
@Test
public void createURLWhenInExportedPages() throws Exception {
DocumentReference pageReference = new DocumentReference("xwiki", "Main", "WebHome");
when(this.currentDocumentReferenceResolver.resolve("Main.WebHome")).thenReturn(pageReference);
when(this.defaultEntityReferenceSerializer.serialize(pageReference)).thenReturn("xwiki:Main.WebHome");
when(this.legacySpaceResolver.resolve("Main")).thenReturn(Arrays.asList("Main"));
when(this.fsPathEntityReferenceSerializer.serialize(pageReference)).thenReturn("Main/WebHome");
when(this.relativeEntityReferenceResolver.resolve("Main", EntityType.SPACE)).thenReturn(new EntityReference("Main", EntityType.SPACE));
FilesystemExportContext exportContext = new FilesystemExportContext();
// Simulate locating the doc in pages/Main/WebHome (ie 3 levels deep)
exportContext.setDocParentLevels(3);
this.factory.init(Arrays.asList(pageReference), null, exportContext, this.context);
assertEquals(new URL("file://../../../pages/Main/WebHome.html"), this.factory.createURL("Main", "WebHome", "view", null, null, "xwiki", this.context));
}
use of org.xwiki.url.filesystem.FilesystemExportContext 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);
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceSerializerTest method serializeCSSResourceWithURLsInIt.
@Test
public void serializeCSSResourceWithURLsInIt() throws Exception {
FilesystemExportContext exportContext = new FilesystemExportContext();
exportContext.setExportDir(BASEDIR);
Provider<FilesystemExportContext> exportContextProvider = this.mocker.getInstance(new DefaultParameterizedType(null, Provider.class, FilesystemExportContext.class));
Mockito.when(exportContextProvider.get()).thenReturn(exportContext);
WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("font-awesome", "4.7.0", "css/font-awesome.min.css"));
assertEquals("webjars/font-awesome/4.7.0/css/font-awesome.min.css", this.mocker.getComponentUnderTest().serialize(reference).serialize());
// Also verify that the resources haves been copied!
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/css/font-awesome.min.css").exists());
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/fontawesome-webfont.eot").exists());
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/fontawesome-webfont.woff2").exists());
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/fontawesome-webfont.woff").exists());
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/fontawesome-webfont.svg").exists());
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/fontawesome-webfont.ttf").exists());
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceSerializerTest method serializeWithCSSPathAdjustments.
@Test
public void serializeWithCSSPathAdjustments() throws Exception {
FilesystemExportContext exportContext = new FilesystemExportContext();
exportContext.setExportDir(BASEDIR);
exportContext.pushCSSParentLevels(3);
Provider<FilesystemExportContext> exportContextProvider = this.mocker.getInstance(new DefaultParameterizedType(null, Provider.class, FilesystemExportContext.class));
Mockito.when(exportContextProvider.get()).thenReturn(exportContext);
WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList("font-awesome", "4.7.0", "fonts/FontAwesome.otf"));
// Verify that the returned URL is ok
assertEquals("../../../webjars/font-awesome/4.7.0/fonts/FontAwesome.otf", this.mocker.getComponentUnderTest().serialize(reference).serialize());
// Also verify that the resource has been copied!
assertTrue(new File(BASEDIR, "webjars/font-awesome/4.7.0/fonts/FontAwesome.otf").exists());
}
Aggregations