Search in sources :

Example 6 with AttachmentReference

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

the class AttachmentURLStreamHandlerTest method testAttachmentJarURL.

@Test
public void testAttachmentJarURL() throws Exception {
    URL url = new URL(null, "attachmentjar://Space.Page@filename", (URLStreamHandler) this.handler);
    final AttachmentReference attachmentReference = new AttachmentReference("filename", new DocumentReference("wiki", "space", "page"));
    getMockery().checking(new Expectations() {

        {
            oneOf(AttachmentURLStreamHandlerTest.this.arf).resolve("Space.Page@filename");
            will(returnValue(attachmentReference));
            oneOf(AttachmentURLStreamHandlerTest.this.dab).getAttachmentContent(attachmentReference);
            will(returnValue(new ByteArrayInputStream("content".getBytes())));
        }
    });
    URLConnection connection = url.openConnection();
    InputStream input = null;
    try {
        connection.connect();
        input = connection.getInputStream();
        Assert.assertEquals("content", IOUtils.toString(input));
    } finally {
        if (input != null) {
            input.close();
        }
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Expectations(org.jmock.Expectations) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URL(java.net.URL) DocumentReference(org.xwiki.model.reference.DocumentReference) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 7 with AttachmentReference

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

the class AttachmentUnifiedDiffBuilder method addAttachmentDiff.

/**
 * Computes the differences, in unified format, between two versions of an attachment and adds the result to the
 * given {@link DocumentUnifiedDiff}.
 *
 * @param previousAttachment the previous version of the attachment
 * @param nextAttachment the next version of the attachment
 * @param documentDiff where to add the differences
 */
public void addAttachmentDiff(XWikiAttachment previousAttachment, XWikiAttachment nextAttachment, DocumentUnifiedDiff documentDiff) {
    AttachmentReference previousReference = getAttachmentVersionReference(previousAttachment, documentDiff.getPreviousReference());
    AttachmentReference nextReference = getAttachmentVersionReference(nextAttachment, documentDiff.getNextReference());
    EntityUnifiedDiff<AttachmentReference> attachmentDiff = new EntityUnifiedDiff<>(previousReference, nextReference);
    if ((previousAttachment == null || isSmallTextFile(previousAttachment)) && (nextAttachment == null || isSmallTextFile(nextAttachment))) {
        // Compute content differences.
        maybeAddDiff(attachmentDiff, CONTENT, getContentAsString(previousAttachment), getContentAsString(nextAttachment));
    } else {
        // The size difference is useful when the content difference is not shown.
        maybeAddDiff(attachmentDiff, "size", previousAttachment == null ? null : previousAttachment.getLongSize(), nextAttachment == null ? null : nextAttachment.getLongSize());
        if (attachmentDiff.isEmpty() && !contentEquals(previousAttachment, nextAttachment)) {
            // If the files have the same size but the content is different then show an empty list.
            attachmentDiff.put(CONTENT, Collections.<UnifiedDiffBlock<String, Character>>emptyList());
        }
    }
    if (attachmentDiff.size() > 0) {
        documentDiff.getAttachmentDiffs().add(attachmentDiff);
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityUnifiedDiff(org.xwiki.extension.xar.job.diff.EntityUnifiedDiff)

Example 8 with AttachmentReference

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

the class FSPathStringEntityReferenceSerializerTest method serialize.

@Test
public void serialize() throws Exception {
    DocumentReference documentReference = new DocumentReference("wik.i", "spac.e", "pag.e");
    AttachmentReference attachmentReference = new AttachmentReference("image.png", documentReference);
    String result = this.mocker.getComponentUnderTest().serialize(attachmentReference);
    assertEquals("wik%2Ei/spac%2Ee/pag%2Ee/image.png", result);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with AttachmentReference

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

the class DefaultOfficeResourceViewer method createView.

@Override
public XDOM createView(ResourceReference reference, Map<String, ?> parameters) throws Exception {
    OfficeDocumentView view;
    if (reference.getType().equals(ResourceType.ATTACHMENT) || reference.getType().equals(ResourceType.UNKNOWN)) {
        AttachmentReference attachmentReference = this.attachmentResolver.resolve(reference.getReference());
        view = getView(reference, attachmentReference, parameters);
    } else {
        view = getView(reference, parameters);
    }
    // transformations must be executed even when the XDOM is taken from the cache.
    return view.getXDOM().clone();
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference)

Example 10 with AttachmentReference

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

the class DefaultOfficeResourceViewerTest method testViewPresentation.

@Test
public void testViewPresentation() throws Exception {
    AttachmentResourceReference attachResourceRef = new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
    DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
    AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
    AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
    when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(Arrays.asList(attachmentReference));
    when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
    ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
    when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
    ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
    ExpandedMacroBlock galleryMacro = new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
    galleryMacro.addChild(new ImageBlock(imageReference, true));
    XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
    Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
    XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
    PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
    when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)).thenReturn(xdomOfficeDocument);
    Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
    TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
    Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TemporaryResourceReference.class, ExtendedURL.class);
    ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = mocker.getInstance(type, "standard/tmp");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
    when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
    XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
    ImageBlock imageBlock = (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
    assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
    galleryMacro = (ExpandedMacroBlock) output.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
    assertFalse(galleryMacro.getParent() instanceof XDOM);
    assertEquals(Syntax.XWIKI_2_1, ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
    TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
    verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) ImageBlock(org.xwiki.rendering.block.ImageBlock) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) Type(java.lang.reflect.Type) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) PresentationBuilder(org.xwiki.officeimporter.builder.PresentationBuilder) TemporaryResourceStore(org.xwiki.resource.temporary.TemporaryResourceStore) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

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