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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations