Search in sources :

Example 1 with ResourceReferenceSerializer

use of org.xwiki.resource.ResourceReferenceSerializer in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenError.

@Test
public void convertWhenError() 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)).thenThrow(new SerializeResourceReferenceException("error"));
    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());
    }
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ResourceReferenceSerializer

use of org.xwiki.resource.ResourceReferenceSerializer 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 3 with ResourceReferenceSerializer

use of org.xwiki.resource.ResourceReferenceSerializer 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 4 with ResourceReferenceSerializer

use of org.xwiki.resource.ResourceReferenceSerializer in project xwiki-platform by xwiki.

the class VfsResourceReferenceSerializerTest method serializeWhenSpecificSchemeSerializer.

@Test
public void serializeWhenSpecificSchemeSerializer() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:attachment"), "path1/path2/test.txt");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("vfs", "attach:wiki:space.page@attachment", "path1", "path2", "test.txt"));
    ResourceReferenceSerializer<VfsResourceReference, ExtendedURL> serializer = this.mocker.registerMockComponent(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, ExtendedURL.class), "attach");
    when(serializer.serialize(reference)).thenReturn(extendedURL);
    assertEquals("/vfs/attach%3Awiki%3Aspace.page%40attachment/path1/path2/test.txt", this.mocker.getComponentUnderTest().serialize(reference).toString());
}
Also used : ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 5 with ResourceReferenceSerializer

use of org.xwiki.resource.ResourceReferenceSerializer in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenOk.

@Test
public void convertWhenOk() 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"));
    Path path = this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
    assertEquals("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c", path.toString());
    assertEquals(TPath.class.getName(), path.getClass().getName());
}
Also used : TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) TPath(net.java.truevfs.access.TPath) URI(java.net.URI) Test(org.junit.Test)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)9 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)9 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)8 Test (org.junit.Test)7 URI (java.net.URI)5 ExtendedURL (org.xwiki.url.ExtendedURL)4 Path (java.nio.file.Path)3 TPath (net.java.truevfs.access.TPath)3 ComponentManager (org.xwiki.component.manager.ComponentManager)2 ConversionException (org.xwiki.properties.converter.ConversionException)2 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)2 VfsException (org.xwiki.vfs.VfsException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Date (java.util.Date)1 Provider (javax.inject.Provider)1 TArchiveDetector (net.java.truevfs.access.TArchiveDetector)1