Search in sources :

Example 46 with AttachmentReference

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

the class ImageFilterTest method filterAddsImageMarkers.

@Test
public void filterAddsImageMarkers() {
    AttachmentReference attachmentReference = new AttachmentReference("-foo--bar.png-", this.documentReference);
    when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
    ResourceReference resourceReference = new ResourceReference("-foo--bar.png-", ResourceType.ATTACHMENT);
    resourceReference.setTyped(false);
    when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|-foo--bar.png-");
    filterAndAssertOutput("<img src=\"../../some/path/-foo--b%61r.png-\"/>", Collections.singletonMap("targetDocument", "Path.To.Page"), "<!--startimage:false|-|attach|-|-foo-\\-bar.png-\\--><img src=\"/path/to/foo.png\"/><!--stopimage-->");
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) Test(org.junit.Test)

Example 47 with AttachmentReference

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

the class DefaultOfficeViewerScriptServiceTest method view.

@Test
public void view() throws Exception {
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    AttachmentReference attachmentReference = new AttachmentReference("file.odt", new DocumentReference("wiki", "Space", "Page"));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentAccessBridge documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    when(documentAccessBridge.isDocumentViewable(attachmentReference.getDocumentReference())).thenReturn(true);
    when(documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference())).thenReturn(document);
    when(document.getSyntax()).thenReturn(Syntax.TEX_1_0);
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    OfficeViewer viewer = mocker.getInstance(OfficeViewer.class);
    when(viewer.createView(attachmentReference, Collections.<String, String>emptyMap())).thenReturn(xdom);
    BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0");
    Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference));
    TransformationManager transformationManager = mocker.getInstance(TransformationManager.class);
    verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class));
    verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) OfficeViewer(org.xwiki.office.viewer.OfficeViewer) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 48 with AttachmentReference

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

the class PdfURLFactory method saveAttachmentReference.

/**
 * @param url the URL to save in the attachment map
 * @param wiki the wiki where the attachment is located
 * @param spaces a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If
 *        a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
 * @param page the page where the attachment is located
 * @param fileName the name of the attachment file
 * @param context the XWiki Context where to find the attachment map that we add to
 * @see PDFResourceResolver
 */
private void saveAttachmentReference(URL url, String wiki, String spaces, String page, String fileName, XWikiContext context) {
    // Save Entity Reference pointed to by this URL in the Context so that it can be used at export time in the
    // PDF URIResolver code.
    Map<String, AttachmentReference> attachmentMap = (Map<String, AttachmentReference>) context.get(PDF_EXPORT_CONTEXT_KEY);
    if (attachmentMap == null) {
        attachmentMap = new HashMap<>();
        context.put(PDF_EXPORT_CONTEXT_KEY, attachmentMap);
    }
    attachmentMap.put(url.toString(), new AttachmentReference(fileName, new DocumentReference(wiki, this.legacySpaceResolver.resolve(spaces), page)));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Map(java.util.Map) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 49 with AttachmentReference

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

the class PDFURIResolver method resolve.

@Override
public Source resolve(String href, String base) throws TransformerException {
    if (this.attachmentMap != null) {
        // implemented for TemporaryResource...
        if (href.contains(TEX_ACTION)) {
            // Note: See the comments in FormulaMacro to understand why we do a replace...
            AttachmentReference reference = this.attachmentMap.get(href.replace(TEX_ACTION, "/download/"));
            if (reference != null) {
                // Get the generated image's input stream
                ImageStorage storage = Utils.getComponent(ImageStorage.class);
                ImageData image = storage.get(reference.getName());
                return new StreamSource(new ByteArrayInputStream(image.getData()));
            }
        }
        // TODO: end HACK
        AttachmentReference reference = this.attachmentMap.get(href);
        if (reference != null) {
            try {
                XWikiDocument xdoc = this.context.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), this.context);
                // TODO: handle revisions
                XWikiAttachment attachment = xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName());
                return new StreamSource(attachment.getContentInputStream(this.context));
            } catch (Exception e) {
                throw new TransformerException(String.format("Failed to resolve export URI [%s]", href), e);
            }
        }
    }
    // Defaults to the default URI Resolver in FO
    return null;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ImageStorage(org.xwiki.formula.ImageStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageData(org.xwiki.formula.ImageData) StreamSource(javax.xml.transform.stream.StreamSource) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException)

Example 50 with AttachmentReference

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

the class DefaultResourceReferenceEntityReferenceResolverTest method resolveTypeAttachment.

@Test
public void resolveTypeAttachment() throws ComponentLookupException {
    // When the page does not exist
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
    // When the page exist
    this.existingDocuments.add(new DocumentReference(WIKI, SPACE, PAGE));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
    this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, SPACE, PAGE));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
    this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
    // When page is current page
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
    this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE));
    assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)), this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) 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