Search in sources :

Example 1 with CachingFilterInputStreamInputSource

use of org.exist.util.CachingFilterInputStreamInputSource in project exist by eXist-db.

the class InMemoryOutputStream method stream.

public void stream(final XmldbURL xmldbURL, final InputStream is, @Deprecated final int length) throws IOException {
    BrokerPool db;
    try {
        db = BrokerPool.getInstance();
    } catch (EXistException e) {
        throw new IOException(e);
    }
    try (final DBBroker broker = db.getBroker()) {
        final XmldbURI collectionUri = XmldbURI.create(xmldbURL.getCollection());
        final XmldbURI documentUri = XmldbURI.create(xmldbURL.getDocumentName());
        final TransactionManager transact = db.getTransactionManager();
        try (final Txn txn = transact.beginTransaction();
            final Collection collection = broker.getOrCreateCollection(txn, collectionUri)) {
            if (collection == null) {
                throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
            }
            final LockManager lockManager = db.getLockManager();
            txn.acquireCollectionLock(() -> lockManager.acquireCollectionWriteLock(collectionUri));
            if (collection.hasChildCollection(broker, documentUri)) {
                throw new IOException("Resource " + documentUri.toString() + " is a collection.");
            }
            try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
                final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
                final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
                try (final ManagedDocumentLock lock = lockManager.acquireDocumentWriteLock(documentUri)) {
                    broker.storeDocument(txn, documentUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
                }
            }
            txn.commit();
        }
    } catch (final IOException ex) {
        LOG.debug(ex);
        throw ex;
    } catch (final Exception ex) {
        LOG.debug(ex);
        throw new IOException(ex.getMessage(), ex);
    }
}
Also used : EXistException(org.exist.EXistException) IOException(java.io.IOException) Txn(org.exist.storage.txn.Txn) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) CachingFilterInputStreamInputSource(org.exist.util.CachingFilterInputStreamInputSource) MimeType(org.exist.util.MimeType) IOException(java.io.IOException) EXistException(org.exist.EXistException) LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

IOException (java.io.IOException)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 BrokerPool (org.exist.storage.BrokerPool)1 DBBroker (org.exist.storage.DBBroker)1 LockManager (org.exist.storage.lock.LockManager)1 ManagedDocumentLock (org.exist.storage.lock.ManagedDocumentLock)1 TransactionManager (org.exist.storage.txn.TransactionManager)1 Txn (org.exist.storage.txn.Txn)1 CachingFilterInputStreamInputSource (org.exist.util.CachingFilterInputStreamInputSource)1 MimeType (org.exist.util.MimeType)1 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)1 FilterInputStreamCache (org.exist.util.io.FilterInputStreamCache)1 XmldbURI (org.exist.xmldb.XmldbURI)1