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));
}
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));
}
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;
}
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);
}
}
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);
}
Aggregations