use of org.xwiki.url.filesystem.FilesystemExportContext 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());
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceSerializer method serialize.
@Override
public ExtendedURL serialize(WebJarsResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
// Copy the resource from the webjar to the filesystem
FilesystemExportContext exportContext = this.exportContextProvider.get();
try {
FilesystemResourceReferenceCopier copier = new FilesystemResourceReferenceCopier();
copier.copyResourceFromJAR(WEBJARS_RESOURCE_PREFIX, reference.getResourceName(), WEBJAR_PATH, exportContext);
// filesystem.
if (reference.getResourceName().toLowerCase().endsWith("css")) {
copier.processCSS(WEBJARS_RESOURCE_PREFIX, reference.getResourceName(), WEBJAR_PATH, exportContext);
}
} catch (Exception e) {
throw new SerializeResourceReferenceException(String.format("Failed to extract and copy WebJAR resource [%s]", reference.getResourceName()), e);
}
List<String> pathSegments = new ArrayList<>();
// Adjust path depending on where the current doc is stored
if (exportContext.getCSSParentLevel() == 0) {
for (int i = 0; i < exportContext.getDocParentLevel(); i++) {
pathSegments.add(PARENT);
}
} else {
// Adjust path for links inside CSS files (since they need to be relative to the CSS file they're in).
for (int i = 0; i < exportContext.getCSSParentLevel(); i++) {
pathSegments.add(PARENT);
}
}
pathSegments.add(WEBJAR_PATH);
for (String resourceSegment : StringUtils.split(reference.getResourceName(), '/')) {
pathSegments.add(resourceSegment);
}
return new RelativeExtendedURL(pathSegments);
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class FilesystemResourceReferenceSerializerTest method serializeWithCSSPathAdjustmentsWithDocParentLevels.
@Test
public void serializeWithCSSPathAdjustmentsWithDocParentLevels() throws Exception {
FilesystemExportContext exportContext = new FilesystemExportContext();
exportContext.setExportDir(BASEDIR);
exportContext.setDocParentLevels(2);
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 org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class ExportURLFactory method init.
/**
* Init the url factory.
*
* @param exportedPages the pages that will be exported.
* @param exportDir the directory where to copy exported objects (attachments).
* @param context the XWiki context.
* @deprecated starting with 8.4.5/9.0, use {@link #init(Collection, File, FilesystemExportContext, XWikiContext)}
*/
@Deprecated
public void init(Collection<String> exportedPages, File exportDir, XWikiContext context) {
Provider<FilesystemExportContext> exportContextProvider = Utils.getComponent(new DefaultParameterizedType(null, Provider.class, FilesystemExportContext.class));
DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "current");
List<DocumentReference> references = new ArrayList<>();
for (String exportedPage : exportedPages) {
references.add(resolver.resolve(exportedPage));
}
init(references, exportDir, exportContextProvider.get(), context);
}
use of org.xwiki.url.filesystem.FilesystemExportContext in project xwiki-platform by xwiki.
the class FilesystemExportContextProvider method get.
@Override
public FilesystemExportContext get() {
ExecutionContext ec = this.execution.getContext();
FilesystemExportContext exportContext = (FilesystemExportContext) ec.getProperty(CONTEXT_KEY);
if (exportContext == null) {
exportContext = new FilesystemExportContext();
ec.newProperty(CONTEXT_KEY).inherited().initial(exportContext).declare();
}
return exportContext;
}
Aggregations