Search in sources :

Example 1 with MCRByteContent

use of org.mycore.common.content.MCRByteContent in project mycore by MyCoRe-Org.

the class MCRXMLMetadataManagerTest method retrieve.

@Test
public void retrieve() throws JDOMException, IOException, SAXException {
    assertEquals("Stored document ID do not match:", MyCoRe_document_00000001.id.toString(), SAX_BUILDER.build(new ByteArrayInputStream(MyCoRe_document_00000001.blob)).getRootElement().getAttributeValue("id"));
    getStore().create(MyCoRe_document_00000001.id, new MCRByteContent(MyCoRe_document_00000001.blob, MCR_document_00000001.lastModified.getTime()), MyCoRe_document_00000001.lastModified);
    MCRVersionedMetadata data = getStore().getVersionedMetaData(MyCoRe_document_00000001.id);
    assertFalse(data.isDeleted());
    assertFalse(data.isDeletedInRepository());
    Document doc = getStore().retrieveXML(MyCoRe_document_00000001.id);
    assertEquals("Stored document ID do not match:", MyCoRe_document_00000001.id.toString(), doc.getRootElement().getAttributeValue("id"));
    try {
        doc = getStore().retrieveXML(MCR_document_00000001.id);
        if (doc != null) {
            fail("Requested " + MCR_document_00000001.id + ", retrieved wrong document:\n" + new XMLOutputter(Format.getPrettyFormat()).outputString(doc));
        }
    } catch (Exception e) {
    // this is ok
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) MCRByteContent(org.mycore.common.content.MCRByteContent) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 2 with MCRByteContent

use of org.mycore.common.content.MCRByteContent in project mycore by MyCoRe-Org.

the class MCRFileStoreTest method basicFunctionality.

@Test
public void basicFunctionality() throws Exception {
    Date first = new Date();
    synchronized (this) {
        wait(1000);
    }
    MCRFileCollection col = getStore().create();
    assertNotNull(col);
    assertTrue(col.getID() > 0);
    Date created = col.getLastModified();
    assertFalse(first.after(created));
    bzzz();
    MCRFile build = col.createFile("build.xml");
    assertNotNull(build);
    Date modified = col.getLastModified();
    assertTrue(modified.after(created));
    assertEquals(1, col.getNumChildren());
    assertEquals(1, col.getChildren().size());
    assertEquals(0, build.getSize());
    assertTrue(created.before(build.getLastModified()));
    build.setContent(new MCRJDOMContent(new Element("project")));
    assertTrue(build.getSize() > 0);
    assertNotNull(build.getContent().asByteArray());
    bzzz();
    MCRDirectory dir = col.createDir("documentation");
    assertEquals(2, col.getNumChildren());
    assertTrue(modified.before(col.getLastModified()));
    byte[] content = "Hello World!".getBytes("UTF-8");
    dir.createFile("readme.txt").setContent(new MCRByteContent(content, System.currentTimeMillis()));
    MCRFile child = (MCRFile) dir.getChild("readme.txt");
    assertNotNull(child);
    assertEquals(content.length, child.getSize());
}
Also used : Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRByteContent(org.mycore.common.content.MCRByteContent) Date(java.util.Date) Test(org.junit.Test)

Example 3 with MCRByteContent

use of org.mycore.common.content.MCRByteContent in project mycore by MyCoRe-Org.

the class MCRFileTest method randomAccessContent.

@Test
public void randomAccessContent() throws Exception {
    MCRFile file = col.createFile("foo.txt");
    byte[] content = "Hello World".getBytes("UTF-8");
    file.setContent(new MCRByteContent(content, System.currentTimeMillis()));
    RandomAccessContent rac = file.getRandomAccessContent();
    rac.skipBytes(6);
    InputStream in = rac.getInputStream();
    char c = (char) in.read();
    assertEquals('W', c);
    in.close();
    rac.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MCRByteContent(org.mycore.common.content.MCRByteContent) RandomAccessContent(org.apache.commons.vfs2.RandomAccessContent) Test(org.junit.Test)

Example 4 with MCRByteContent

use of org.mycore.common.content.MCRByteContent in project mycore by MyCoRe-Org.

the class MCRXSLTransformer method getTransformedContent.

protected MCRContent getTransformedContent(MCRContent source, XMLReader reader, TransformerHandler transformerHandler) throws IOException, SAXException {
    MCRByteArrayOutputStream baos = new MCRByteArrayOutputStream(INITIAL_BUFFER_SIZE);
    StreamResult serializer = new StreamResult(baos);
    transformerHandler.setResult(serializer);
    // Parse the source XML, and send the parse events to the
    // TransformerHandler.
    LOGGER.info("Start transforming: {}", source.getSystemId() == null ? source.getName() : source.getSystemId());
    reader.parse(source.getInputSource());
    return new MCRByteContent(baos.getBuffer(), 0, baos.size());
}
Also used : MCRByteArrayOutputStream(org.mycore.common.content.streams.MCRByteArrayOutputStream) StreamResult(javax.xml.transform.stream.StreamResult) MCRByteContent(org.mycore.common.content.MCRByteContent)

Example 5 with MCRByteContent

use of org.mycore.common.content.MCRByteContent in project mycore by MyCoRe-Org.

the class MCRPNGTools method toPNGContent.

public MCRContent toPNGContent(BufferedImage thumbnail) throws IOException {
    if (thumbnail != null) {
        ImageWriter imageWriter = getImageWriter();
        try (MCRByteArrayOutputStream bout = new MCRByteArrayOutputStream(maxPngSize.get());
            ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(bout)) {
            imageWriter.setOutput(imageOutputStream);
            IIOImage iioImage = new IIOImage(thumbnail, null, null);
            imageWriter.write(null, iioImage, imageWriteParam);
            int contentLength = bout.size();
            maxPngSize.set(Math.max(maxPngSize.get(), contentLength));
            MCRByteContent imageContent = new MCRByteContent(bout.getBuffer(), 0, bout.size());
            imageContent.setMimeType("image/png");
            return imageContent;
        } finally {
            imageWriter.reset();
            imageWriters.add(imageWriter);
        }
    } else {
        return null;
    }
}
Also used : MCRByteArrayOutputStream(org.mycore.common.content.streams.MCRByteArrayOutputStream) ImageWriter(javax.imageio.ImageWriter) MCRByteContent(org.mycore.common.content.MCRByteContent) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

MCRByteContent (org.mycore.common.content.MCRByteContent)10 MCRByteArrayOutputStream (org.mycore.common.content.streams.MCRByteArrayOutputStream)5 Test (org.junit.Test)4 Document (org.jdom2.Document)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Element (org.jdom2.Element)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 SVNRepository (org.tmatesoft.svn.core.io.SVNRepository)2 JsonElement (com.google.gson.JsonElement)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 IIOImage (javax.imageio.IIOImage)1 ImageWriter (javax.imageio.ImageWriter)1 ImageOutputStream (javax.imageio.stream.ImageOutputStream)1 StreamResult (javax.xml.transform.stream.StreamResult)1 RandomAccessContent (org.apache.commons.vfs2.RandomAccessContent)1 JDOMException (org.jdom2.JDOMException)1 MCRUsageException (org.mycore.common.MCRUsageException)1 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)1