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