Search in sources :

Example 6 with VfsResourceReference

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

the class VfsScriptServiceTest method urlError.

@Test
public void urlError() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt");
    VfsManager manager = this.mocker.getInstance(VfsManager.class);
    when(manager.getURL(reference)).thenThrow(new VfsException("error"));
    assertNull(this.mocker.getComponentUnderTest().url(new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt")));
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsManager(org.xwiki.vfs.VfsManager) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) Test(org.junit.Test)

Example 7 with VfsResourceReference

use of org.xwiki.vfs.VfsResourceReference 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 8 with VfsResourceReference

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

the class URIVfsResourceReferenceSerializer method serialize.

@Override
public URI serialize(VfsResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    URI resultURI;
    try {
        ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.componentManagerProvider.get().getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), String.format("truevfs/%s", reference.getURI().getScheme()));
        resultURI = serializer.serialize(reference);
    } catch (ComponentLookupException e) {
        // No serializer exist, we just don't perform any conversion!
        resultURI = reference.toURI();
    }
    return resultURI;
}
Also used : ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI)

Example 9 with VfsResourceReference

use of org.xwiki.vfs.VfsResourceReference 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 10 with VfsResourceReference

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

the class CascadingVfsPermissionCheckerTest method checkPermissionWhenReservedScheme.

@Test
public void checkPermissionWhenReservedScheme() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("cascading:whatever"), "whatever");
    try {
        this.mocker.getComponentUnderTest().checkPermission(reference);
        fail("Should have raised exception");
    } catch (VfsException expected) {
        assertEquals("[cascading] is a reserved VFS URI scheme and cannot be used.", expected.getMessage());
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) Test(org.junit.Test)

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