Search in sources :

Example 11 with ResourceReference

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

the class DefaultOfficeResourceViewerTest method testViewExistingOfficeFileWithCacheHit.

/**
 * Tests creating a view for an external office file which has already been viewed and cached.
 *
 * @throws Exception if an error occurs.
 */
@Test
public void testViewExistingOfficeFileWithCacheHit() throws Exception {
    ResourceReference resourceReference = new ResourceReference("http://resource", ResourceType.URL);
    when(this.resourceReferenceSerializer.serialize(resourceReference)).thenReturn("url:" + resourceReference.getReference());
    OfficeDocumentView officeDocumentView = new OfficeDocumentView(resourceReference, new XDOM(new ArrayList<Block>()), new HashSet<File>());
    when(externalCache.get(STRING_DOCUMENT_REFERENCE + "/url:http://resource/" + DEFAULT_VIEW_PARAMETERS.hashCode())).thenReturn(officeDocumentView);
    Assert.assertNotNull(mocker.getComponentUnderTest().createView(resourceReference, DEFAULT_VIEW_PARAMETERS));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) ArrayList(java.util.ArrayList) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) File(java.io.File) Test(org.junit.Test)

Example 12 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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 13 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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 14 with ResourceReference

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

the class OfficeMacro method execute.

@Override
public List<Block> execute(OfficeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Check if the office server is started and if not, generate an error.
    if (!this.officeServer.getState().equals(OfficeServer.ServerState.CONNECTED)) {
        throw new MacroExecutionException("The wiki needs to be connected to an office server in order to view " + "office files. Ask your administrator to configure such a server.");
    }
    ResourceReference resourceReference = getResourceReference(context.getCurrentMacroBlock(), parameters);
    Map<String, Object> viewParameters = new HashMap<String, Object>();
    viewParameters.put("filterStyles", parameters.isFilterStyles());
    viewParameters.put("ownerDocument", getOwnerDocument(context.getCurrentMacroBlock()));
    try {
        return this.officeViewer.createView(resourceReference, viewParameters).getChildren();
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to view office attachment.", e);
    }
}
Also used : HashMap(java.util.HashMap) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException)

Example 15 with ResourceReference

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

the class DefaultGadgetSource method getDashboardSourceMetadata.

@Override
public List<Block> getDashboardSourceMetadata(String source, MacroTransformationContext context) {
    DocumentReference sourceDoc = getSourceDocumentReference(source);
    String classParameterName = "class";
    GroupBlock metadataContainer = new GroupBlock();
    metadataContainer.setParameter(classParameterName, DashboardMacro.METADATA);
    // generate anchors for the urls
    XWikiContext xContext = getXWikiContext();
    String editURL = xContext.getWiki().getURL(sourceDoc, "save", "", "", xContext);
    LinkBlock editURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(editURL, ResourceType.URL), false);
    editURLBlock.setParameter(classParameterName, DashboardMacro.EDIT_URL);
    metadataContainer.addChild(editURLBlock);
    String removeURL = xContext.getWiki().getURL(sourceDoc, "objectremove", "", "", xContext);
    LinkBlock removeURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(removeURL, ResourceType.URL), false);
    removeURLBlock.setParameter(classParameterName, DashboardMacro.REMOVE_URL);
    metadataContainer.addChild(removeURLBlock);
    String addURL = xContext.getWiki().getURL(sourceDoc, "objectadd", "", "", xContext);
    LinkBlock addURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(addURL, ResourceType.URL), false);
    addURLBlock.setParameter(classParameterName, DashboardMacro.ADD_URL);
    metadataContainer.addChild(addURLBlock);
    // and create divs for the source metadata
    GroupBlock sourcePageBlock = new GroupBlock();
    sourcePageBlock.addChild(new WordBlock(sourceDoc.getName()));
    sourcePageBlock.setParameter(classParameterName, DashboardMacro.SOURCE_PAGE);
    metadataContainer.addChild(sourcePageBlock);
    GroupBlock sourceSpaceBlock = new GroupBlock();
    // Extract the full Space Reference (in order to support Nested Spaces) and set it in the XDOM
    sourceSpaceBlock.addChild(new WordBlock(this.localReferenceSerializer.serialize(sourceDoc.getLastSpaceReference())));
    sourceSpaceBlock.setParameter(classParameterName, DashboardMacro.SOURCE_SPACE);
    metadataContainer.addChild(sourceSpaceBlock);
    GroupBlock sourceWikiBlock = new GroupBlock();
    sourceWikiBlock.addChild(new WordBlock(sourceDoc.getWikiReference().getName()));
    sourceWikiBlock.setParameter(classParameterName, DashboardMacro.SOURCE_WIKI);
    metadataContainer.addChild(sourceWikiBlock);
    String sourceURL = xContext.getWiki().getURL(sourceDoc, "view", "", "", xContext);
    LinkBlock sourceURLBlock = new LinkBlock(Collections.<Block>emptyList(), new ResourceReference(sourceURL, ResourceType.URL), false);
    sourceURLBlock.setParameter(classParameterName, DashboardMacro.SOURCE_URL);
    metadataContainer.addChild(sourceURLBlock);
    return Collections.<Block>singletonList(metadataContainer);
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) XWikiContext(com.xpn.xwiki.XWikiContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) WordBlock(org.xwiki.rendering.block.WordBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)51 DocumentReference (org.xwiki.model.reference.DocumentReference)27 Test (org.junit.Test)26 LinkBlock (org.xwiki.rendering.block.LinkBlock)20 XDOM (org.xwiki.rendering.block.XDOM)16 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)15 AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)13 Block (org.xwiki.rendering.block.Block)12 SpaceReference (org.xwiki.model.reference.SpaceReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 AttachmentReference (org.xwiki.model.reference.AttachmentReference)9 ImageBlock (org.xwiki.rendering.block.ImageBlock)8 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReference (org.xwiki.model.reference.EntityReference)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 GroupBlock (org.xwiki.rendering.block.GroupBlock)5