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