Search in sources :

Example 6 with CachingFilterInputStream

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

Example 7 with CachingFilterInputStream

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

the class RESTServer method doPut.

/**
 * Handles PUT requests. The request content is stored as a new resource at
 * the specified location. If the resource already exists, it is overwritten
 * if the user has write permissions.
 *
 * The resource type depends on the content type specified in the HTTP
 * header. The content type will be looked up in the global mime table. If
 * the corresponding mime type is not a know XML mime type, the resource
 * will be stored as a binary resource.
 *
 * @param broker the database broker
 * @param transaction the database transaction
 * @param request the request
 * @param response the response
 * @param path the path of the request
 *
 * @throws BadRequestException if a bad request is made
 * @throws PermissionDeniedException if the request has insufficient permissions
 * @throws NotFoundException if the request resource cannot be found
 * @throws IOException if an I/O error occurs
 */
public void doPut(final DBBroker broker, final Txn transaction, final XmldbURI path, final HttpServletRequest request, final HttpServletResponse response) throws BadRequestException, PermissionDeniedException, IOException, NotFoundException {
    if (checkForXQueryTarget(broker, transaction, path, request, response)) {
        return;
    }
    // fourth, process the request
    final XmldbURI docUri = path.lastSegment();
    final XmldbURI collUri = path.removeLastSegment();
    if (docUri == null || collUri == null) {
        throw new BadRequestException("Bad path: " + path);
    }
    // TODO : use getOrCreateCollection() right now ?
    try (final ManagedCollectionLock managedCollectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collUri)) {
        final Collection collection = broker.getOrCreateCollection(transaction, collUri);
        final MimeType mime;
        String contentType = request.getContentType();
        if (contentType != null) {
            final int semicolon = contentType.indexOf(';');
            if (semicolon > 0) {
                contentType = contentType.substring(0, semicolon).trim();
            }
            mime = MimeTable.getInstance().getContentType(contentType);
        } else {
            mime = MimeTable.getInstance().getContentTypeFor(docUri);
        }
        // TODO(AR) in storeDocument, if the input source has an InputStream (but is not a subclass: FileInputSource or ByteArrayInputSource), need to handle caching and reusing the input stream between validate and store
        try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), request.getInputStream());
            final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
            broker.storeDocument(transaction, docUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
        }
        response.setStatus(HttpServletResponse.SC_CREATED);
    // try(final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), request.getInputStream());
    // final InputStream cfis = new CachingFilterInputStream(cache)) {
    // 
    // if (mime.isXMLType()) {
    // cfis.mark(Integer.MAX_VALUE);
    // final IndexInfo info = collection.validateXMLResource(transaction, broker, docUri, new InputSource(cfis));
    // info.getDocument().setMimeType(contentType);
    // cfis.reset();
    // collection.store(transaction, broker, info, new InputSource(cfis));
    // response.setStatus(HttpServletResponse.SC_CREATED);
    // } else {
    // collection.addBinaryResource(transaction, broker, docUri, cfis, contentType, request.getContentLength());
    // response.setStatus(HttpServletResponse.SC_CREATED);
    // }
    // }
    } catch (final SAXParseException e) {
        throw new BadRequestException("Parsing exception at " + e.getLineNumber() + "/" + e.getColumnNumber() + ": " + e.toString());
    } catch (final TriggerException | LockException e) {
        throw new PermissionDeniedException(e.getMessage());
    } catch (final SAXException e) {
        Exception o = e.getException();
        if (o == null) {
            o = e;
        }
        throw new BadRequestException("Parsing exception: " + o.getMessage());
    } catch (final EXistException e) {
        throw new BadRequestException("Internal error: " + e.getMessage());
    }
}
Also used : EXistException(org.exist.EXistException) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) PermissionDeniedException(org.exist.security.PermissionDeniedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) TriggerException(org.exist.collections.triggers.TriggerException) EXistException(org.exist.EXistException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 8 with CachingFilterInputStream

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

the class RestXqServiceImpl method extractRequestBody.

@Override
protected Sequence extractRequestBody(final HttpRequest request) throws RestXqServiceException {
    // TODO don't use close shield input stream and move parsing of form parameters from HttpServletRequestAdapter into RequestBodyParser
    InputStream is;
    FilterInputStreamCache cache = null;
    try {
        // first, get the content of the request
        is = new CloseShieldInputStream(request.getInputStream());
        if (is.available() <= 0) {
            return null;
        }
        // if marking is not supported, 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)
        if (!is.markSupported()) {
            cache = FilterInputStreamCacheFactory.getCacheInstance(() -> {
                final Configuration configuration = getBrokerPool().getConfiguration();
                return (String) configuration.getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
            }, is);
            is = new CachingFilterInputStream(cache);
        }
        is.mark(Integer.MAX_VALUE);
    } catch (final IOException ioe) {
        throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
    }
    Sequence result = null;
    try {
        // was there any POST content?
        if (is != null && is.available() > 0) {
            String contentType = request.getContentType();
            // 1) determine if exists mime database considers this binary data
            if (contentType != null) {
                // strip off any charset encoding info
                if (contentType.contains(";")) {
                    contentType = contentType.substring(0, contentType.indexOf(";"));
                }
                MimeType mimeType = MimeTable.getInstance().getContentType(contentType);
                if (mimeType != null && !mimeType.isXMLType()) {
                    // binary data
                    try {
                        final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), is);
                        if (binaryValue != null) {
                            result = new SequenceImpl<>(new BinaryTypedValue(binaryValue));
                        }
                    } catch (final XPathException xpe) {
                        throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, xpe);
                    }
                }
            }
            if (result == null) {
                // 2) not binary, try and parse as an XML document
                final DocumentImpl doc = parseAsXml(is);
                if (doc != null) {
                    result = new SequenceImpl<>(new DocumentTypedValue(doc));
                }
            }
            if (result == null) {
                String encoding = request.getCharacterEncoding();
                // 3) not a valid XML document, return a string representation of the document
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                try {
                    // reset the stream, as we need to reuse for string parsing
                    is.reset();
                    final StringValue str = parseAsString(is, encoding);
                    if (str != null) {
                        result = new SequenceImpl<>(new StringTypedValue(str));
                    }
                } catch (final IOException ioe) {
                    throw new RestXqServiceException(RestXqErrorCodes.RQDY0014, ioe);
                }
            }
        }
    } catch (IOException e) {
        throw new RestXqServiceException(e.getMessage());
    } finally {
        if (cache != null) {
            try {
                cache.invalidate();
            } catch (final IOException ioe) {
                LOG.error(ioe.getMessage(), ioe);
            }
        }
        if (is != null) {
            /*
                 * Do NOT close the stream if its a binary value,
                 * because we will need it later for serialization
                 */
            boolean isBinaryType = false;
            if (result != null) {
                try {
                    final Type type = result.head().getType();
                    isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
                } catch (final IndexOutOfBoundsException ioe) {
                    LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
                }
            }
            if (!isBinaryType) {
                try {
                    is.close();
                } catch (final IOException ioe) {
                    LOG.error(ioe.getMessage(), ioe);
                }
            }
        }
    }
    if (result != null) {
        return result;
    } else {
        return Sequence.EMPTY_SEQUENCE;
    }
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) Configuration(org.exist.util.Configuration) DocumentTypedValue(org.exist.extensions.exquery.xdm.type.impl.DocumentTypedValue) XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) Sequence(org.exquery.xquery.Sequence) FilterInputStreamCache(org.exist.util.io.FilterInputStreamCache) DocumentImpl(org.exist.dom.memtree.DocumentImpl) MimeType(org.exist.util.MimeType) BinaryTypedValue(org.exist.extensions.exquery.xdm.type.impl.BinaryTypedValue) StringTypedValue(org.exist.extensions.exquery.xdm.type.impl.StringTypedValue) MimeType(org.exist.util.MimeType) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Type(org.exquery.xquery.Type) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) StringValue(org.exist.xquery.value.StringValue) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

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