Search in sources :

Example 81 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class BinaryValueFromInputStreamTest method filter_withIncrementReferenceCount.

@Test
public void filter_withIncrementReferenceCount() throws IOException, XPathException {
    final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
    final byte[] testData = "test data".getBytes();
    try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
        final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
        final InputStream bvis = binaryValue.getInputStream();
        // create a filter over the first BinaryValue, and reference count increment
        final InputStream fis = new BinaryValueFilteringInputStream(bvis, true);
        final BinaryValue filteredBinaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis);
        // we now destroy the filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
        // It should not close the original binary value, as BinaryValueFilteringInputStream increased the reference count.
        filteredBinaryValue.close();
        assertTrue(filteredBinaryValue.isClosed());
        assertFalse(binaryValue.isClosed());
        // we should still be able to read from the origin binary value!
        try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
            binaryValue.streamBinaryTo(baos);
            assertArrayEquals(testData, baos.toByteArray());
        }
        // finally close the original binary value
        fis.close();
        bvis.close();
        binaryValue.close();
        assertTrue(binaryValue.isClosed());
    } finally {
        binaryValueManager.runCleanupTasks();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 82 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class BinaryValueFromInputStreamTest method filterFilter_withoutIncrementReferenceCountFails.

@Test(expected = IOException.class)
public void filterFilter_withoutIncrementReferenceCountFails() throws IOException, XPathException {
    final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
    final byte[] testData = "test data".getBytes();
    try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
        final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
        final InputStream bvis = binaryValue.getInputStream();
        // create a filter over the first BinaryValue, with no reference count increment
        final InputStream fis1 = new BinaryValueFilteringInputStream(bvis, false);
        final BinaryValue filteredBinaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis1);
        // create a second filter over the first filter, with no reference count increment
        final InputStream fbvis = filteredBinaryValue1.getInputStream();
        final InputStream fis2 = new BinaryValueFilteringInputStream(fbvis, false);
        final BinaryValue filteredBinaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis2);
        // we now destroy the second filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
        // It should close the first filtered binary value and original binary value, as we have not incremented the reference counts!
        filteredBinaryValue2.close();
        fis2.close();
        fbvis.close();
        assertTrue(filteredBinaryValue2.isClosed());
        assertTrue(filteredBinaryValue1.isClosed());
        fis1.close();
        bvis.close();
        assertTrue(binaryValue.isClosed());
        // we should not be able to read from the first filtered binary value!
        try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
            // this should throw an IOException
            filteredBinaryValue1.streamBinaryTo(baos);
        }
    } finally {
        binaryValueManager.runCleanupTasks();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 83 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class BinaryValueFromInputStreamTest method filter_withoutIncrementReferenceCountFails.

@Test(expected = IOException.class)
public void filter_withoutIncrementReferenceCountFails() throws IOException, XPathException {
    final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
    final byte[] testData = "test data".getBytes();
    try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
        final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
        final InputStream bvis = binaryValue.getInputStream();
        // create a filter over the first BinaryValue, with no reference count increment
        final InputStream fis = new BinaryValueFilteringInputStream(bvis, false);
        final BinaryValue filteredBinaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis);
        // we now destroy the filtered binary value, just as it would be if it went out of scope from popLocalVariables#popLocalVariables.
        // It should close the original binary value, as we have not incremented the reference count!
        filteredBinaryValue.close();
        assertTrue(filteredBinaryValue.isClosed());
        fis.close();
        bvis.close();
        assertTrue(binaryValue.isClosed());
        // we should not be able to read from the origin binary value!
        try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
            // this should throw an IOException
            binaryValue.streamBinaryTo(baos);
        }
    } finally {
        binaryValueManager.runCleanupTasks();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 84 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class ActiveDirectoryRealmTest method setUpBeforeClass.

/**
 * @throws java.lang.Exception
 */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    InputStream is = new UnsynchronizedByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8));
    Configuration config = Configurator.parse(is);
    realm = new ActiveDirectoryRealm(null, config);
}
Also used : Configuration(org.exist.config.Configuration) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BeforeClass(org.junit.BeforeClass)

Example 85 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class ExistCollection method createFile.

public XmldbURI createFile(String newName, InputStream is, Long length, String contentType) throws IOException, PermissionDeniedException, CollectionDoesNotExistException {
    if (LOG.isDebugEnabled())
        LOG.debug("Create '{}' in '{}'", newName, xmldbUri);
    XmldbURI newNameUri = XmldbURI.create(newName);
    // Get mime, or NULL when not available
    MimeType mime = MimeTable.getInstance().getContentTypeFor(newName);
    if (mime == null) {
        mime = MimeType.BINARY_TYPE;
    }
    // XML documents are not supported a small file will be created.
    if (mime.isXMLType() && length == 0) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating dummy XML file for null resource lock '{}'", newNameUri);
        }
        is = new UnsynchronizedByteArrayInputStream("<null_resource/>".getBytes(StandardCharsets.UTF_8));
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
        // by ResourceFactory
        if (collection == null) {
            LOG.debug("Collection {} does not exist", xmldbUri);
            txnManager.abort(txn);
            throw new CollectionDoesNotExistException(xmldbUri + "");
        }
        if (LOG.isDebugEnabled()) {
            if (mime.isXMLType()) {
                LOG.debug("Inserting XML document '{}'", mime.getName());
            } else {
                LOG.debug("Inserting BINARY document '{}'", mime.getName());
            }
        }
        // Stream into database
        try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
            final CachingFilterInputStream cfis = new CachingFilterInputStream(cache);
            final EXistInputSource eis = new CachingFilterInputStreamInputSource(cfis)) {
            broker.storeDocument(txn, newNameUri, eis, mime, collection);
        }
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Document created successfully");
        }
    } catch (EXistException | SAXException e) {
        LOG.error(e);
        throw new IOException(e);
    } catch (LockException e) {
        LOG.error(e);
        throw new PermissionDeniedException(xmldbUri + "");
    } catch (IOException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation");
        }
    }
    // Send the result back to the client
    XmldbURI newResource = xmldbUri.append(newName);
    return newResource;
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) CollectionDoesNotExistException(org.exist.webdav.exceptions.CollectionDoesNotExistException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)114 InputStream (java.io.InputStream)102 Test (org.junit.Test)93 MarkShieldInputStream (org.apache.commons.io.input.MarkShieldInputStream)31 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)10 IOException (java.io.IOException)8 FilterInputStream (java.io.FilterInputStream)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)7 XMLResource (org.xmldb.api.modules.XMLResource)6 DBBroker (org.exist.storage.DBBroker)5 Txn (org.exist.storage.txn.Txn)5 Element (org.w3c.dom.Element)4 Collection (org.xmldb.api.base.Collection)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3