Search in sources :

Example 6 with AttachmentResourceReference

use of org.xwiki.rendering.listener.reference.AttachmentResourceReference 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)

Example 7 with AttachmentResourceReference

use of org.xwiki.rendering.listener.reference.AttachmentResourceReference in project xwiki-platform by xwiki.

the class OfficeMacro method getResourceReference.

private ResourceReference getResourceReference(MacroBlock block, OfficeMacroParameters parameters) throws MacroExecutionException {
    ResourceReference resourceReference = parameters.getReference();
    // Make sure to provide full reference for attachment
    if (resourceReference == null || resourceReference.getType().equals(ResourceType.ATTACHMENT) || !resourceReference.isTyped()) {
        AttachmentReference attachmentReference;
        if (resourceReference == null) {
            // Support former way of indicating the file to view
            String reference = parameters.getAttachment();
            if (StringUtils.isEmpty(reference)) {
                throw new MacroExecutionException("You must specify the 'reference' parameter pointing to the office file to display.");
            }
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(reference, block);
        } else {
            attachmentReference = this.macroAttachmentReferenceResolver.resolve(resourceReference.getReference(), block);
        }
        resourceReference = new AttachmentResourceReference(this.serializer.serialize(attachmentReference));
    }
    return resourceReference;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 8 with AttachmentResourceReference

use of org.xwiki.rendering.listener.reference.AttachmentResourceReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method getImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsNotLimited.

/**
 * Tests that the proper image URL is generated when both the width and the height are unspecified and the image
 * size is not limited in the configuration.
 *
 * @throws Exception if an exception occurs while running the test
 */
@Test
public void getImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsNotLimited() throws Exception {
    final ExtendedRenderingConfiguration configuration = this.mocker.getInstance(ExtendedRenderingConfiguration.class);
    when(configuration.getImageWidthLimit()).thenReturn(-1);
    when(configuration.getImageHeightLimit()).thenReturn(-1);
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("style", "bad CSS declaration");
    testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, null);
}
Also used : ExtendedRenderingConfiguration(org.xwiki.rendering.configuration.ExtendedRenderingConfiguration) HashMap(java.util.HashMap) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) Test(org.junit.Test)

Example 9 with AttachmentResourceReference

use of org.xwiki.rendering.listener.reference.AttachmentResourceReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method getImageURLWhenBothWidthAndHeightCSSPropertiesAreSpecified.

/**
 * Tests that the proper image URL is generated when both the width and the height CSS properties are specified.
 *
 * @throws Exception if an exception occurs while running the test
 */
@Test
public void getImageURLWhenBothWidthAndHeightCSSPropertiesAreSpecified() throws Exception {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("style", "border: 1px; height: 30px; margin-top: 2em; width: 70px");
    testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=70&height=30");
}
Also used : HashMap(java.util.HashMap) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) Test(org.junit.Test)

Example 10 with AttachmentResourceReference

use of org.xwiki.rendering.listener.reference.AttachmentResourceReference in project xwiki-platform by xwiki.

the class XWikiWikiModelTest method getImageURLWhenBothWidthAndHeightAreUnspecifiedAndOnlyImageWidthIsLimited.

/**
 * Tests that the proper image URL is generated when both the width and the height are unspecified and only the
 * image width is limited in the configuration.
 *
 * @throws Exception if an exception occurs while running the test
 */
@Test
public void getImageURLWhenBothWidthAndHeightAreUnspecifiedAndOnlyImageWidthIsLimited() throws Exception {
    final ExtendedRenderingConfiguration configuration = this.mocker.getInstance(ExtendedRenderingConfiguration.class);
    when(configuration.getImageWidthLimit()).thenReturn(25);
    when(configuration.getImageHeightLimit()).thenReturn(-1);
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("width", "45%");
    parameters.put("style", "height:10em");
    testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=25");
}
Also used : ExtendedRenderingConfiguration(org.xwiki.rendering.configuration.ExtendedRenderingConfiguration) HashMap(java.util.HashMap) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) Test(org.junit.Test)

Aggregations

AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)14 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 ExtendedRenderingConfiguration (org.xwiki.rendering.configuration.ExtendedRenderingConfiguration)3 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)3 AttachmentReference (org.xwiki.model.reference.AttachmentReference)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 DefaultVersionConstraint (org.xwiki.extension.version.internal.DefaultVersionConstraint)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 PresentationBuilder (org.xwiki.officeimporter.builder.PresentationBuilder)1 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)1 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)1 ImageBlock (org.xwiki.rendering.block.ImageBlock)1 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1