Search in sources :

Example 26 with AttachmentReference

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

the class TemplateListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;
    // Is this a skin document
    if (document.getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null) {
        if (event instanceof AbstractAttachmentEvent) {
            AttachmentReference attachment = new AttachmentReference(((AbstractAttachmentEvent) event).getName(), document.getDocumentReference());
            String id = this.referenceSerializer.serialize(attachment);
            if (event instanceof AttachmentDeletedEvent) {
                this.observation.notify(new TemplateDeletedEvent(id), this);
            } else if (event instanceof AttachmentUpdatedEvent) {
                this.observation.notify(new TemplateUpdatedEvent(id), this);
            }
        } else if (event instanceof XObjectPropertyEvent) {
            String id = this.referenceSerializer.serialize(((XObjectPropertyEvent) event).getReference());
            if (event instanceof XObjectPropertyDeletedEvent) {
                this.observation.notify(new TemplateDeletedEvent(id), this);
            } else if (event instanceof XObjectPropertyUpdatedEvent) {
                this.observation.notify(new TemplateUpdatedEvent(id), this);
            }
        }
    }
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XObjectPropertyUpdatedEvent(com.xpn.xwiki.internal.event.XObjectPropertyUpdatedEvent) TemplateDeletedEvent(org.xwiki.template.event.TemplateDeletedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) TemplateUpdatedEvent(org.xwiki.template.event.TemplateUpdatedEvent) XObjectPropertyDeletedEvent(com.xpn.xwiki.internal.event.XObjectPropertyDeletedEvent) XObjectPropertyEvent(com.xpn.xwiki.internal.event.XObjectPropertyEvent) AttachmentUpdatedEvent(com.xpn.xwiki.internal.event.AttachmentUpdatedEvent) AbstractAttachmentEvent(com.xpn.xwiki.internal.event.AbstractAttachmentEvent) AttachmentDeletedEvent(com.xpn.xwiki.internal.event.AttachmentDeletedEvent)

Example 27 with AttachmentReference

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

the class PDFResourceResolver method getResource.

@Override
public Resource getResource(URI uri) throws IOException {
    XWikiContext xcontext = xcontextProvider.get();
    Map<String, AttachmentReference> attachmentMap = (Map<String, AttachmentReference>) xcontext.get(PdfURLFactory.PDF_EXPORT_CONTEXT_KEY);
    if (attachmentMap != null) {
        String uriString = uri.toString();
        // implemented for TemporaryResource...
        if (uriString.contains(TEX_ACTION)) {
            // Note: See the comments in FormulaMacro to understand why we do a replace...
            AttachmentReference reference = attachmentMap.get(uriString.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 Resource(new ByteArrayInputStream(image.getData()));
            }
        }
        // TODO: end HACK
        AttachmentReference reference = attachmentMap.get(uriString);
        if (reference != null) {
            try {
                XWikiDocument xdoc = xcontext.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), xcontext);
                // TODO: handle revisions
                XWikiAttachment attachment = xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName());
                return new Resource(attachment.getContentInputStream(xcontext));
            } catch (Exception e) {
                throw new IOException(String.format("Failed to resolve export URI [%s]", uriString), e);
            }
        }
    }
    return this.standardResolver.getResource(uri);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageStorage(org.xwiki.formula.ImageStorage) Resource(org.apache.xmlgraphics.io.Resource) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) IOException(java.io.IOException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageData(org.xwiki.formula.ImageData) Map(java.util.Map)

Example 28 with AttachmentReference

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

the class CompactStringEntityReferenceSerializerTest method testSerializeAttachmentReferenceWhenContextDocument.

@Test
public void testSerializeAttachmentReferenceWhenContextDocument() throws Exception {
    AttachmentReference reference = new AttachmentReference("filename", new DocumentReference("wiki", "space", "page"));
    this.oldcore.getXWikiContext().setWikiId("wiki");
    this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page")));
    Assert.assertEquals("filename", this.mocker.getComponentUnderTest().serialize(reference));
    this.oldcore.getXWikiContext().setWikiId("wiki");
    this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "otherpage")));
    Assert.assertEquals("page@filename", this.mocker.getComponentUnderTest().serialize(reference));
    this.oldcore.getXWikiContext().setWikiId("otherwiki");
    this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("otherwiki", "space", "page")));
    Assert.assertEquals("wiki:space.page@filename", this.mocker.getComponentUnderTest().serialize(reference));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 29 with AttachmentReference

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

the class CompactStringEntityReferenceSerializerTest method testSerializeSpaceReferenceWhenHasChildren.

@Test
public void testSerializeSpaceReferenceWhenHasChildren() throws Exception {
    AttachmentReference reference = new AttachmentReference("filename", new DocumentReference("wiki", "space", "page"));
    this.oldcore.getXWikiContext().setWikiId("wiki");
    this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page")));
    Assert.assertEquals("page", this.mocker.getComponentUnderTest().serialize(reference.getParent()));
    Assert.assertEquals("space", this.mocker.getComponentUnderTest().serialize(reference.getParent().getParent()));
    this.oldcore.getXWikiContext().setWikiId("xwiki");
    this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("xwiki", "xspace", "xpage")));
    Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference.getParent()));
    Assert.assertEquals("wiki:space", this.mocker.getComponentUnderTest().serialize(reference.getParent().getParent()));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 30 with AttachmentReference

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

the class AttachmentResourceReferenceEntityReferenceResolver method resolveTyped.

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference) {
    // If the reference is empty let default resolver deal with it
    if (StringUtils.isEmpty(resourceReference.getReference())) {
        return this.defaultStringAttachmentReferenceResolver.resolve(resourceReference.getReference(), baseReference);
    }
    // Get relative reference
    EntityReference relativeReference = this.relativeReferenceResolver.resolve(resourceReference.getReference(), EntityType.ATTACHMENT);
    // Resolve full reference
    AttachmentReference attachmentReference = this.defaultReferenceAttachmentReferenceResolver.resolve(relativeReference, baseReference);
    // See if the resolved (terminal or WebHome) document exists and, if so, use it.
    DocumentReference documentReference = attachmentReference.getDocumentReference();
    // Take care of fallback if needed
    DocumentReference finalDocumentReference = resolveDocumentReference(relativeReference.getParent(), documentReference, baseReference);
    if (finalDocumentReference != documentReference) {
        attachmentReference = new AttachmentReference(attachmentReference.getName(), finalDocumentReference);
    }
    return attachmentReference;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

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