Search in sources :

Example 1 with CachingFilterInputStream

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

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

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

the class CachingFilterInputStreamInputSource method getByteStreamLength.

/**
 * @see EXistInputSource#getByteStreamLength()
 *
 * @throws IllegalStateException if the InputSource was previously closed
 */
@Override
public long getByteStreamLength() {
    assertOpen();
    if (length == -1) {
        try (final CachingFilterInputStream is = new CachingFilterInputStream(cachingFilterInputStream);
            final CountingOutputStream cos = new CountingOutputStream(NullOutputStream.NULL_OUTPUT_STREAM)) {
            InputStreamUtil.copy(is, cos);
            length = cos.getByteCount();
        } catch (final InstantiationException | IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    return -1;
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) IOException(java.io.IOException)

Example 4 with CachingFilterInputStream

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

the class BinaryValueFromInputStream method convertTo.

@Override
public BinaryValue convertTo(final BinaryValueType binaryValueType) throws XPathException {
    try {
        final BinaryValueFromInputStream binaryInputStream = new BinaryValueFromInputStream(getManager(), binaryValueType, new CachingFilterInputStream(is));
        getManager().registerBinaryValueInstance(binaryInputStream);
        return binaryInputStream;
    } catch (InstantiationException ex) {
        LOG.error(ex.getMessage(), ex);
    }
    return null;
}
Also used : CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream)

Example 5 with CachingFilterInputStream

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

CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)8 FilterInputStreamCache (org.exist.util.io.FilterInputStreamCache)6 IOException (java.io.IOException)5 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 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 CountingOutputStream (org.apache.commons.io.output.CountingOutputStream)1 TriggerException (org.exist.collections.triggers.TriggerException)1