use of javax.inject.Provider 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 javax.inject.Provider in project xwiki-platform by xwiki.
the class DefaultLinkRefactoringTest method configure.
@Before
public void configure() throws Exception {
XWiki xwiki = mock(XWiki.class);
when(this.xcontext.getWiki()).thenReturn(xwiki);
Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
when(xcontextProvider.get()).thenReturn(this.xcontext);
this.resourceReferenceResolver = this.mocker.getInstance(DefaultResourceReferenceEntityReferenceResolver.TYPE_RESOURCEREFERENCE);
this.defaultReferenceDocumentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_REFERENCE);
this.compactEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
Provider<ComponentManager> contextComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
when(contextComponentManagerProvider.get()).thenReturn(this.mocker);
}
use of javax.inject.Provider 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 javax.inject.Provider 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());
}
use of javax.inject.Provider in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceSerializerTest method serialize.
@Test
public void serialize() 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", "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