Search in sources :

Example 1 with FilterInputStreamCache

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

the class HttpServletRequestAdapter method getInputStream.

@Override
public InputStream getInputStream() throws IOException {
    if (is == null) {
        final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(cacheConfiguration, request.getInputStream());
        is = new CachingFilterInputStream(cache);
        is.mark(Integer.MAX_VALUE);
    } else {
        is.reset();
    }
    return is;
}
Also used : CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache)

Example 2 with FilterInputStreamCache

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

the class GetData method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    // if the content length is unknown or 0, return
    if (request.getContentLength() <= 0) {
        return Sequence.EMPTY_SEQUENCE;
    }
    InputStream isRequest = null;
    Sequence result = Sequence.EMPTY_SEQUENCE;
    try {
        isRequest = request.getInputStream();
        // was there any POST content?
        if (isRequest != null && isRequest.available() > 0) {
            // 1) determine if exists mime database considers this binary data
            String contentType = request.getContentType();
            if (contentType != null) {
                // strip off any charset encoding info
                if (contentType.indexOf(';') > -1) {
                    contentType = contentType.substring(0, contentType.indexOf(';'));
                }
                final MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
                if (mimeType != null && !mimeType.isXMLType()) {
                    // binary data
                    result = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), isRequest);
                }
            }
            if (result == Sequence.EMPTY_SEQUENCE) {
                // 2) not binary, try and parse as an XML document, otherwise 3) return a string representation
                // parsing will consume the stream so we must cache!
                InputStream is = null;
                FilterInputStreamCache cache = null;
                try {
                    // we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt)
                    cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) context.getBroker().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), isRequest);
                    is = new CachingFilterInputStream(cache);
                    // mark the start of the stream
                    is.mark(Integer.MAX_VALUE);
                    // 2) try and  parse as XML
                    result = parseAsXml(is);
                    if (result == Sequence.EMPTY_SEQUENCE) {
                        // 3) not a valid XML document, return a string representation of the document
                        String encoding = request.getCharacterEncoding();
                        if (encoding == null) {
                            encoding = "UTF-8";
                        }
                        try {
                            // reset the stream, as we need to reuse for string parsing after the XML parsing happened
                            is.reset();
                            result = parseAsString(is, encoding);
                        } catch (final IOException ioe) {
                            throw new XPathException(this, "An IO exception occurred: " + ioe.getMessage(), ioe);
                        }
                    }
                } finally {
                    if (cache != null) {
                        try {
                            cache.invalidate();
                        } catch (final IOException ioe) {
                            LOG.error(ioe.getMessage(), ioe);
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (final IOException ioe) {
                            LOG.error(ioe.getMessage(), ioe);
                        }
                    }
                }
            }
        // NOTE we do not close isRequest, because it may be needed further by the caching input stream wrapper
        }
    } catch (final IOException ioe) {
        throw new XPathException(this, "An IO exception occurred: " + ioe.getMessage(), ioe);
    }
    return result;
}
Also used : CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) InputStream(java.io.InputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) IOException(java.io.IOException) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) MimeType(org.exist.util.MimeType)

Example 3 with FilterInputStreamCache

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

the class BinaryValues method getCacheInstances.

@Override
public List<BinaryInputStreamCacheInfo> getCacheInstances() {
    final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
    final Collection<FilterInputStreamCacheInfo> cacheInstances = monitor.getActive();
    final List<BinaryInputStreamCacheInfo> results = new ArrayList<>();
    for (final FilterInputStreamCacheInfo cacheInstance : cacheInstances) {
        final BinaryInputStreamCacheInfo result;
        final FilterInputStreamCache cache = cacheInstance.getCache();
        if (cache instanceof FileFilterInputStreamCache) {
            result = new BinaryInputStreamCacheInfo(CacheType.FILE, cacheInstance.getRegistered(), Optional.of(((FileFilterInputStreamCache) cache).getFilePath()), cache.getLength());
        } else if (cache instanceof MemoryMappedFileFilterInputStreamCache) {
            result = new BinaryInputStreamCacheInfo(CacheType.MEMORY_MAPPED_FILE, cacheInstance.getRegistered(), Optional.of(((MemoryMappedFileFilterInputStreamCache) cache).getFilePath()), cache.getLength());
        } else {
            result = new BinaryInputStreamCacheInfo(CacheType.MEMORY, cacheInstance.getRegistered(), Optional.empty(), cache.getLength());
        }
        results.add(result);
    }
    return results;
}
Also used : FilterInputStreamCacheMonitor(org.exist.util.io.FilterInputStreamCacheMonitor) ArrayList(java.util.ArrayList) FilterInputStreamCacheInfo(org.exist.util.io.FilterInputStreamCacheMonitor.FilterInputStreamCacheInfo) FileFilterInputStreamCache(org.exist.util.io.FileFilterInputStreamCache) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) MemoryMappedFileFilterInputStreamCache(org.exist.util.io.MemoryMappedFileFilterInputStreamCache) MemoryMappedFileFilterInputStreamCache(org.exist.util.io.MemoryMappedFileFilterInputStreamCache) FileFilterInputStreamCache(org.exist.util.io.FileFilterInputStreamCache) MemoryMappedFileFilterInputStreamCache(org.exist.util.io.MemoryMappedFileFilterInputStreamCache)

Example 4 with FilterInputStreamCache

use of org.exist.util.io.FilterInputStreamCache 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)

Example 5 with FilterInputStreamCache

use of org.exist.util.io.FilterInputStreamCache 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

FilterInputStreamCache (org.exist.util.io.FilterInputStreamCache)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)6 IOException (java.io.IOException)4 EXistException (org.exist.EXistException)3 Collection (org.exist.collections.Collection)3 MimeType (org.exist.util.MimeType)3 XmldbURI (org.exist.xmldb.XmldbURI)3 InputStream (java.io.InputStream)2 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 DBBroker (org.exist.storage.DBBroker)2 TransactionManager (org.exist.storage.txn.TransactionManager)2 Txn (org.exist.storage.txn.Txn)2 SAXException (org.xml.sax.SAXException)2 ArrayList (java.util.ArrayList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)1 TriggerException (org.exist.collections.triggers.TriggerException)1