Search in sources :

Example 26 with VfsResourceReference

use of org.xwiki.vfs.VfsResourceReference in project xwiki-platform by xwiki.

the class VfsResourceReferenceHandler method handle.

@Override
public void handle(ResourceReference resourceReference, ResourceReferenceHandlerChain chain) throws ResourceReferenceHandlerException {
    // This code only handles VFS Resource References.
    VfsResourceReference vfsResourceReference = (VfsResourceReference) resourceReference;
    try {
        // Verify that the user has the permission for the specified VFS scheme and for the VFS URI
        this.permissionChecker.checkPermission(vfsResourceReference);
        // Extract the asked resource from inside the zip and return its content for display.
        // We need to convert the VFS Resource Reference into a hierarchical URI supported by TrueVFS
        URI trueVFSURI = convertResourceReference(vfsResourceReference);
        // We use TrueVFS. This line will automatically use the VFS Driver that matches the scheme passed in the URI
        Path path = new TPath(trueVFSURI);
        try (InputStream in = Files.newInputStream(path)) {
            List<String> pathSegments = vfsResourceReference.getPathSegments();
            serveResource(pathSegments.get(pathSegments.size() - 1), in);
        }
    } catch (Exception e) {
        throw new ResourceReferenceHandlerException(String.format("Failed to extract resource [%s]", vfsResourceReference), e);
    }
    // Be a good citizen, continue the chain, in case some lower-priority Handler has something to do for this
    // Resource Reference.
    chain.handleNext(vfsResourceReference);
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) InputStream(java.io.InputStream) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) UnsupportedResourceReferenceException(org.xwiki.resource.UnsupportedResourceReferenceException)

Aggregations

VfsResourceReference (org.xwiki.vfs.VfsResourceReference)26 Test (org.junit.Test)19 URI (java.net.URI)11 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)10 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)8 ExtendedURL (org.xwiki.url.ExtendedURL)7 TPath (net.java.truevfs.access.TPath)6 VfsException (org.xwiki.vfs.VfsException)6 Path (java.nio.file.Path)5 DocumentReference (org.xwiki.model.reference.DocumentReference)3 ConversionException (org.xwiki.properties.converter.ConversionException)3 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)3 ContextualAuthorizationManager (org.xwiki.security.authorization.ContextualAuthorizationManager)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)2 URLNormalizer (org.xwiki.url.URLNormalizer)2 VfsManager (org.xwiki.vfs.VfsManager)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1