Search in sources :

Example 1 with VfsException

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

the class PathConverterTest method convertWhenNoPermission.

@Test
public void convertWhenNoPermission() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c"));
    VfsPermissionChecker permissionChecker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("unauthorized")).when(permissionChecker).checkPermission(reference);
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
        assertEquals("VfsException: unauthorized", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 2 with VfsException

use of org.xwiki.vfs.VfsException 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 3 with VfsException

use of org.xwiki.vfs.VfsException 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 4 with VfsException

use of org.xwiki.vfs.VfsException 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)

Example 5 with VfsException

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

the class VfsResourceReferenceHandlerTest method handleWhenNoGenericPermissionForScheme.

@Test
public void handleWhenNoGenericPermissionForScheme() throws Exception {
    setUp("customscheme", "wiki3", "space3", "page3", "test.zip", Arrays.asList("test.txt"));
    // Don't allow permission for "customscheme"
    VfsPermissionChecker checker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("no permission")).when(checker).checkPermission(this.reference);
    try {
        this.mocker.getComponentUnderTest().handle(this.reference, mock(ResourceReferenceHandlerChain.class));
        fail("Should have thrown exception here");
    } catch (ResourceReferenceHandlerException expected) {
        assertEquals("VfsException: no permission", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) ResourceReferenceHandlerChain(org.xwiki.resource.ResourceReferenceHandlerChain) Test(org.junit.Test)

Aggregations

VfsException (org.xwiki.vfs.VfsException)8 Test (org.junit.Test)6 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)6 VfsPermissionChecker (org.xwiki.vfs.VfsPermissionChecker)3 URI (java.net.URI)2 TPath (net.java.truevfs.access.TPath)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)2 Path (java.nio.file.Path)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 ResourceReferenceHandlerChain (org.xwiki.resource.ResourceReferenceHandlerChain)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)1 ContextualAuthorizationManager (org.xwiki.security.authorization.ContextualAuthorizationManager)1 ExtendedURL (org.xwiki.url.ExtendedURL)1 VfsManager (org.xwiki.vfs.VfsManager)1