Search in sources :

Example 1 with FilesystemExportContext

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));
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) DocumentReference(org.xwiki.model.reference.DocumentReference) URL(java.net.URL) Test(org.junit.Test)

Example 2 with FilesystemExportContext

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));
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) DocumentReference(org.xwiki.model.reference.DocumentReference) URL(java.net.URL) Test(org.junit.Test)

Example 3 with FilesystemExportContext

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);
}
Also used : ExportURLFactory(com.xpn.xwiki.web.ExportURLFactory) ZipOutputStream(java.util.zip.ZipOutputStream) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) File(java.io.File) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) Provider(javax.inject.Provider)

Example 4 with FilesystemExportContext

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());
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) File(java.io.File) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 5 with FilesystemExportContext

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());
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) File(java.io.File) Provider(javax.inject.Provider) Test(org.junit.Test)

Aggregations

FilesystemExportContext (org.xwiki.url.filesystem.FilesystemExportContext)14 Test (org.junit.Test)9 Provider (javax.inject.Provider)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 File (java.io.File)6 URL (java.net.URL)3 ExecutionContext (org.xwiki.context.ExecutionContext)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ArrayList (java.util.ArrayList)2 Execution (org.xwiki.context.Execution)2 EntityReference (org.xwiki.model.reference.EntityReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 ExportURLFactory (com.xpn.xwiki.web.ExportURLFactory)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Before (org.junit.Before)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1 UnsupportedResourceReferenceException (org.xwiki.resource.UnsupportedResourceReferenceException)1 RelativeExtendedURL (org.xwiki.url.internal.RelativeExtendedURL)1