Search in sources :

Example 1 with TPath

use of net.java.truevfs.access.TPath in project xwiki-platform by xwiki.

the class DefaultVfsPathFactory method create.

@Override
public Path create(URI uri) throws VfsException {
    try {
        VfsResourceReference reference = this.vfsResourceReferenceConverter.convert(VfsResourceReference.class, uri.toString());
        this.permissionChecker.checkPermission(reference);
        URI trueVFSURI = this.trueVfsResourceReferenceSerializer.serialize(reference);
        return new TPath(trueVFSURI);
    } catch (Exception e) {
        throw new VfsException(String.format("Failed to create Path instance for [%s]", uri.toString()), e);
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) VfsException(org.xwiki.vfs.VfsException)

Example 2 with TPath

use of net.java.truevfs.access.TPath in project xwiki-platform by xwiki.

the class PathConverter method convertToType.

@Override
protected Path convertToType(Type targetType, Object value) {
    if (value == null) {
        return null;
    }
    Path path;
    try {
        VfsResourceReference reference = new VfsResourceReference(new URI(value.toString()));
        // Verify that the user has the permission for the specified VFS scheme. We need to do this at this level
        // since it's possible to do the check in the driver itself since TrueVFS controls whether the driver is
        // called or not and does caching,
        // see https://java.net/projects/truezip/lists/users/archive/2015-12/message/8
        // Since this convert has to be called to use the VFS API from Velocity, we're safe that this will prevent
        // any Velocity script to execute a VFS call if the user is not allowed.
        // 
        // Note: Even though the user needs View access to the attachment, we cannot check this right now because
        // of the caching issue. However we consider that if the user has Programming Rights, he can do anything he
        // wants and thus it's safe that he can access the attachment.
        this.permissionChecker.checkPermission(reference);
        URI trueVfsURI = this.trueVfsResourceReferenceSerializer.serialize(reference);
        path = new TPath(trueVfsURI);
    } catch (Exception e) {
        throw new ConversionException(String.format("Failed to convert [%s] to a Path object", value), e);
    }
    return path;
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) ConversionException(org.xwiki.properties.converter.ConversionException)

Example 3 with TPath

use of net.java.truevfs.access.TPath 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

URI (java.net.URI)3 TPath (net.java.truevfs.access.TPath)3 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)3 Path (java.nio.file.Path)2 InputStream (java.io.InputStream)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1 UnsupportedResourceReferenceException (org.xwiki.resource.UnsupportedResourceReferenceException)1 VfsException (org.xwiki.vfs.VfsException)1