Search in sources :

Example 51 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method testImageURL.

private void testImageURL(final ResourceReference imageReference, Map<String, String> parameters, final boolean expectedIsImageDimensionsIncludedInImageURL, final String expectedQueryString) throws Exception {
    AttachmentReference attachmentReference = new AttachmentReference("image", new DocumentReference("wiki", "space", "page"));
    ExtendedRenderingConfiguration configuration = this.mocker.getInstance(ExtendedRenderingConfiguration.class);
    when(configuration.isImageDimensionsIncludedInImageURL()).thenReturn(expectedIsImageDimensionsIncludedInImageURL);
    when(this.referenceResolver.resolve(imageReference, EntityType.ATTACHMENT)).thenReturn(attachmentReference);
    when(this.documentAccessBridge.getAttachmentURL(attachmentReference, expectedQueryString, false)).thenReturn("?" + expectedQueryString);
    assertEquals("?" + expectedQueryString, this.mocker.getComponentUnderTest().getImageURL(imageReference, parameters));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ExtendedRenderingConfiguration(org.xwiki.rendering.configuration.ExtendedRenderingConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 52 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class RepositoryManager method isVersionValid.

private boolean isVersionValid(XWikiDocument document, BaseObject extensionVersionObject, XWikiContext context) {
    // Has a version
    String extensionVersion = getValue(extensionVersionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
    if (StringUtils.isBlank(extensionVersion)) {
        this.logger.debug("No actual version provided for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
        return false;
    }
    boolean valid;
    ResourceReference resourceReference = null;
    try {
        resourceReference = getDownloadReference(document, extensionVersion);
    } catch (ResolveException e) {
        logger.debug("Cannot obtain download source reference for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
        return false;
    }
    if (resourceReference != null) {
        if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
            AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), document.getDocumentReference());
            XWikiDocument attachmentDocument;
            try {
                attachmentDocument = context.getWiki().getDocument(attachmentReference.getDocumentReference(), context);
                valid = attachmentDocument.getAttachment(attachmentReference.getName()) != null;
            } catch (XWikiException e) {
                this.logger.error("Failed to get document [{}]", attachmentReference.getDocumentReference(), e);
                valid = false;
            }
            if (!valid) {
                this.logger.debug("Attachment [{}] does not exists", attachmentReference);
            }
        } else if (ResourceType.URL.equals(resourceReference.getType()) || ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
            valid = true;
        } else {
            valid = false;
            this.logger.debug("Unknown resource type [{}]", resourceReference.getType());
        }
    } else {
        valid = false;
        this.logger.debug("No actual download provided for object [{}({})]", XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE, extensionVersionObject.getNumber());
    }
    return valid;
}
Also used : ResolveException(org.xwiki.extension.ResolveException) AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 53 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AttachVfsResourceReferenceSerializerTest method serialize.

@Test
public void serialize() 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"));
    URLNormalizer<ExtendedURL> normalizer = this.mocker.getInstance(new DefaultParameterizedType(null, URLNormalizer.class, ExtendedURL.class), "contextpath");
    when(normalizer.normalize(extendedURL)).thenReturn(extendedURL);
    AttachmentReferenceResolver<String> attachmentResolver = this.mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    AttachmentReference attachmentReference = new AttachmentReference("attachment", new DocumentReference("wiki", Arrays.asList("space"), "page"));
    when(attachmentResolver.resolve("attachment")).thenReturn(attachmentReference);
    EntityReferenceSerializer<String> entitySerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    when(entitySerializer.serialize(attachmentReference)).thenReturn("wiki:space.page@attachment");
    assertEquals("/vfs/attach%3Awiki%3Aspace.page%40attachment/path1/path2/test.txt", this.mocker.getComponentUnderTest().serialize(reference).toString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) URLNormalizer(org.xwiki.url.URLNormalizer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 54 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class CascadingVfsPermissionCheckerTest method checkPermissionWithAttachSchemeChecker.

@Test
public void checkPermissionWithAttachSchemeChecker() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:whatever"), "whatever");
    AttachmentReferenceResolver<String> resolver = this.mocker.registerMockComponent(AttachmentReferenceResolver.TYPE_STRING);
    DocumentReference attachmentDocumentReference = new DocumentReference("wiki", "space", "page");
    AttachmentReference attachmentReference = new AttachmentReference("file", attachmentDocumentReference);
    when(resolver.resolve("whatever")).thenReturn(attachmentReference);
    ContextualAuthorizationManager authorizationManager = this.mocker.registerMockComponent(ContextualAuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.VIEW, attachmentReference)).thenReturn(true);
    this.mocker.getComponentUnderTest().checkPermission(reference);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 55 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AttachURIVfsResourceReferenceSerializer method serialize.

@Override
public URI serialize(VfsResourceReference reference) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
    AttachmentReference attachmentReference = this.attachmentResolver.resolve(reference.getURI().getSchemeSpecificPart());
    String scheme = reference.getURI().getScheme();
    String documentRefefenceString = this.documentSerializer.serialize(attachmentReference.getDocumentReference());
    return URI.create(String.format("%s://%s/%s/%s", scheme, documentRefefenceString, attachmentReference.getName(), reference.getPath()));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3