Search in sources :

Example 6 with FilesystemExportContext

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());
}
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 7 with FilesystemExportContext

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);
}
Also used : SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) RelativeExtendedURL(org.xwiki.url.internal.RelativeExtendedURL) ArrayList(java.util.ArrayList) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException)

Example 8 with FilesystemExportContext

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());
}
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 9 with FilesystemExportContext

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);
}
Also used : ArrayList(java.util.ArrayList) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext) DocumentReference(org.xwiki.model.reference.DocumentReference) Provider(javax.inject.Provider)

Example 10 with FilesystemExportContext

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;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) FilesystemExportContext(org.xwiki.url.filesystem.FilesystemExportContext)

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