Search in sources :

Example 21 with VfsResourceReference

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

the class CascadingVfsPermissionCheckerTest method checkPermissionWhenNoSpecificSchemeCheckerAndAllowed.

@Test
public void checkPermissionWhenNoSpecificSchemeCheckerAndAllowed() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever");
    AuthorizationManager authorizationManager = this.mocker.registerMockComponent(AuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(true);
    this.mocker.getComponentUnderTest().checkPermission(reference);
}
Also used : VfsResourceReference(org.xwiki.vfs.VfsResourceReference) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) Test(org.junit.Test)

Example 22 with VfsResourceReference

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

the class DefaultVfsManagerTest method getURL.

@Test
public void getURL() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt");
    ResourceReferenceSerializer<VfsResourceReference, ExtendedURL> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, ExtendedURL.class));
    when(serializer.serialize(reference)).thenReturn(new ExtendedURL(Arrays.asList("generated", "url")));
    assertEquals("/generated/url", this.mocker.getComponentUnderTest().getURL(reference));
}
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 23 with VfsResourceReference

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

the class DefaultVfsManagerTest method getURLerror.

@Test
public void getURLerror() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt");
    ResourceReferenceSerializer<VfsResourceReference, ExtendedURL> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, ExtendedURL.class));
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().getURL(reference);
        fail("Should have thrown an exception");
    } catch (VfsException expected) {
        assertEquals("Failed to compute URL for [uri = [attach:xwiki:space.page@attachment], " + "path = [path1/path2/test.txt], parameters = []]", expected.getMessage());
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) 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 24 with VfsResourceReference

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

the class VfsResourceReferenceHandlerTest method setUp.

private void setUp(String scheme, String wikiName, String spaceName, String pageName, String attachmentName, List<String> path) throws Exception {
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    String attachmentReferenceAsString = String.format("%s:%s:%s.%s@%s", scheme, wikiName, spaceName, pageName, attachmentName);
    this.reference = new VfsResourceReference(URI.create(attachmentReferenceAsString), path);
    ResourceReferenceSerializer<VfsResourceReference, URI> trueVfsResourceReferenceSerializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    String truevfsURIFragment = String.format("%s://%s:%s.%s/%s/%s", scheme, wikiName, spaceName, pageName, attachmentName, StringUtils.join(path, '/'));
    when(trueVfsResourceReferenceSerializer.serialize(this.reference)).thenReturn(URI.create(truevfsURIFragment));
    Provider<XWikiContext> xwikiContextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xwikiContextProvider.get()).thenReturn(xcontext);
    XWiki xwiki = mock(XWiki.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    DocumentReferenceResolver<String> documentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
    this.documentReference = new DocumentReference(wikiName, Arrays.asList(spaceName), pageName);
    String documentReferenceAsString = String.format("%s:%s.%s", wikiName, spaceName, pageName);
    when(documentReferenceResolver.resolve(documentReferenceAsString)).thenReturn(this.documentReference);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(this.documentReference, xcontext)).thenReturn(document);
    XWikiAttachment attachment = mock(XWikiAttachment.class);
    when(document.getAttachment(attachmentName)).thenReturn(attachment);
    when(attachment.getDate()).thenReturn(new Date());
    when(attachment.getContentSize(xcontext)).thenReturn(1000);
    when(attachment.getContentInputStream(xcontext)).thenReturn(createZipInputStream(StringUtils.join(path, '/'), "success!"));
    Container container = this.mocker.getInstance(Container.class);
    Response response = mock(Response.class);
    when(container.getResponse()).thenReturn(response);
    this.baos = new ByteArrayOutputStream();
    when(response.getOutputStream()).thenReturn(this.baos);
    // Register our custom Attach Driver in TrueVFS
    TConfig config = TConfig.current();
    // Note: Make sure we add our own Archive Detector to the existing Detector so that all archive formats
    // supported by TrueVFS are handled properly.
    config.setArchiveDetector(new TArchiveDetector(config.getArchiveDetector(), "attach", new AttachDriver(this.mocker)));
}
Also used : AttachDriver(org.xwiki.vfs.internal.attach.AttachDriver) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) Date(java.util.Date) Provider(javax.inject.Provider) Response(org.xwiki.container.Response) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Container(org.xwiki.container.Container) ComponentManager(org.xwiki.component.manager.ComponentManager) TConfig(net.java.truevfs.access.TConfig) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) TArchiveDetector(net.java.truevfs.access.TArchiveDetector) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 25 with VfsResourceReference

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

the class VfsResourceReferenceResolver method resolve.

@Override
public VfsResourceReference resolve(ExtendedURL extendedURL, ResourceType resourceType, Map<String, Object> parameters) throws CreateResourceReferenceException, UnsupportedResourceReferenceException {
    List<String> segments = extendedURL.getSegments();
    // First segment is the url-encoded VFS reference, defined as URI
    URI vfsUri;
    try {
        vfsUri = new URI(segments.get(0));
    } catch (URISyntaxException e) {
        throw new CreateResourceReferenceException(String.format("Invalid VFS URI [%s] for URL [%s]", segments.get(0), extendedURL));
    }
    // Other segments are the path to the archive resource
    List<String> vfsPathSegments = new ArrayList<>(segments);
    vfsPathSegments.remove(0);
    VfsResourceReference vfsReference = new VfsResourceReference(vfsUri, vfsPathSegments);
    copyParameters(extendedURL, vfsReference);
    return vfsReference;
}
Also used : ArrayList(java.util.ArrayList) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) CreateResourceReferenceException(org.xwiki.resource.CreateResourceReferenceException)

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