Search in sources :

Example 1 with ImageStorage

use of org.xwiki.formula.ImageStorage in project xwiki-platform by xwiki.

the class TexAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    String path = request.getRequestURI();
    // Expected /xwiki/bin/tex/Current/Document/image_identifier
    String filename = Util.decodeURI(path.substring(path.lastIndexOf("/") + 1), context);
    ImageStorage storage = Utils.getComponent(ImageStorage.class);
    ImageData image = storage.get(filename);
    if (image == null) {
        return "docdoesnotexist";
    }
    response.setContentLength(image.getData().length);
    response.setContentType(image.getMimeType());
    try {
        response.getOutputStream().write(image.getData());
    } catch (IOException e) {
        LOGGER.info("Failed to send image to the client");
    }
    return null;
}
Also used : ImageStorage(org.xwiki.formula.ImageStorage) ImageData(org.xwiki.formula.ImageData) IOException(java.io.IOException)

Example 2 with ImageStorage

use of org.xwiki.formula.ImageStorage 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 3 with ImageStorage

use of org.xwiki.formula.ImageStorage in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Document Access Bridge Mock
    final DocumentAccessBridge mockDocumentAccessBridge = componentManager.registerMockComponent(mockery, DocumentAccessBridge.class);
    // Image Storage Mock
    final ImageStorage mockImageStorage = componentManager.registerMockComponent(mockery, ImageStorage.class);
    // Configuration Mock
    final FormulaMacroConfiguration mockConfiguration = componentManager.registerMockComponent(mockery, FormulaMacroConfiguration.class);
    mockery.checking(new Expectations() {

        {
            atLeast(1).of(mockDocumentAccessBridge).getCurrentDocumentReference();
            DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
            will(returnValue(documentReference));
            AttachmentReference attachmentReference = new AttachmentReference("06fbba0acf130efd9e147fdfe91a943cc4f3e29972c6cd1d972e9aabf0900966", documentReference);
            atLeast(2).of(mockDocumentAccessBridge).getAttachmentURL(attachmentReference, false);
            will(returnValue("/xwiki/bin/view/space/page/06fbba0acf130efd9e147fdfe91a943cc4f3e29972c6cd1d972e9aabf0900966"));
            atLeast(2).of(mockConfiguration).getRenderer();
            will(returnValue("snuggletex"));
            atLeast(2).of(mockImageStorage).get(with(any(String.class)));
            will(returnValue(null));
            atLeast(2).of(mockImageStorage).put(with(any(String.class)), with(any(ImageData.class)));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) AttachmentReference(org.xwiki.model.reference.AttachmentReference) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ImageStorage(org.xwiki.formula.ImageStorage) ImageData(org.xwiki.formula.ImageData) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with ImageStorage

use of org.xwiki.formula.ImageStorage 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)

Aggregations

ImageData (org.xwiki.formula.ImageData)4 ImageStorage (org.xwiki.formula.ImageStorage)4 AttachmentReference (org.xwiki.model.reference.AttachmentReference)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 Map (java.util.Map)1 TransformerException (javax.xml.transform.TransformerException)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Resource (org.apache.xmlgraphics.io.Resource)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1