Search in sources :

Example 1 with Type

use of org.exist.xquery.value.Type in project exist by eXist-db.

the class EXIUtils method getInputStream.

protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
    switch(item.getType()) {
        case Type.ANY_URI:
            LOG.debug("Streaming xs:anyURI");
            // anyURI provided
            String url = item.getStringValue();
            // Fix URL
            if (url.startsWith("/")) {
                url = "xmldb:exist://" + url;
            }
            return new URL(url).openStream();
        case Type.ELEMENT:
        case Type.DOCUMENT:
            LOG.debug("Streaming element or document node");
            /*
            if (item instanceof NodeProxy) {
                NodeProxy np = (NodeProxy) item;
                String url = "xmldb:exist://" + np.getDocument().getBaseURI();
                LOG.debug("Document detected, adding URL " + url);
                streamSource.setSystemId(url);
            }
            */
            // Node provided
            final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
                final Serializer serializer = context.getBroker().borrowSerializer();
                try {
                    fn.accept(serializer);
                } finally {
                    context.getBroker().returnSerializer(serializer);
                }
            };
            NodeValue node = (NodeValue) item;
            return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        default:
            LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
            throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) Logger(org.apache.logging.log4j.Logger) NodeValue(org.exist.xquery.value.NodeValue) Serializer(org.exist.storage.serializers.Serializer) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) URL(java.net.URL) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Example 2 with Type

use of org.exist.xquery.value.Type in project exist by eXist-db.

the class NativeBroker method createCollectionObject.

/**
 * NOTE - When this is called there must be a WRITE_LOCK on collectionUri
 * and at least a READ_LOCK on parentCollection (if it is not null)
 */
private Collection createCollectionObject(final Txn transaction, @Nullable @EnsureLocked(mode = LockMode.READ_LOCK) final Collection parentCollection, @EnsureLocked(mode = LockMode.WRITE_LOCK, type = LockType.COLLECTION) final XmldbURI collectionUri, final Optional<Tuple2<Permission, Long>> creationAttributes) throws ReadOnlyException, PermissionDeniedException, LockException {
    final int collectionId = getNextCollectionId(transaction);
    final Collection collection = creationAttributes.map(attrs -> new MutableCollection(this, collectionId, collectionUri, attrs._1, attrs._2)).orElseGet(() -> new MutableCollection(this, collectionId, collectionUri));
    // inherit the group to collection if parent-collection is setGid
    if (parentCollection != null) {
        final Permission parentPermissions = parentCollection.getPermissionsNoLock();
        if (parentPermissions.isSetGid()) {
            final Permission collectionPermissions = collection.getPermissionsNoLock();
            // inherit group
            collectionPermissions.setGroupFrom(parentPermissions);
            // inherit setGid bit
            collectionPermissions.setSetGid(true);
        }
    }
    return collection;
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) BasePooledObjectFactory(org.apache.commons.pool2.BasePooledObjectFactory) LockMode(org.exist.storage.lock.Lock.LockMode) XmlSerializerPool(org.exist.storage.serializers.XmlSerializerPool) Txn(org.exist.storage.txn.Txn) DEFAULT_TEMPORARY_COLLECTION_PERM(org.exist.security.Permission.DEFAULT_TEMPORARY_COLLECTION_PERM) PooledObject(org.apache.commons.pool2.PooledObject) RawDataBackup(org.exist.backup.RawDataBackup) org.exist.security(org.exist.security) VariableByteOutputStream(org.exist.storage.io.VariableByteOutputStream) IEmbeddedXMLStreamReader(org.exist.stax.IEmbeddedXMLStreamReader) Matcher(java.util.regex.Matcher) TerminatedException(org.exist.xquery.TerminatedException) Document(org.w3c.dom.Document) FunctionE(com.evolvedbinary.j8fu.function.FunctionE) DigestType(org.exist.util.crypto.digest.DigestType) XMLStreamException(javax.xml.stream.XMLStreamException) Collection(org.exist.collections.Collection) LockType(org.exist.storage.lock.Lock.LockType) BFile(org.exist.storage.index.BFile) URI(java.net.URI) Path(java.nio.file.Path) ACEAider(org.exist.security.internal.aider.ACEAider) CollectionStore(org.exist.storage.index.CollectionStore) org.exist.storage.lock(org.exist.storage.lock) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) Type(org.exist.xquery.value.Type) org.exist.collections.triggers(org.exist.collections.triggers) Logger(org.apache.logging.log4j.Logger) StructuralIndex(org.exist.indexing.StructuralIndex) SAXException(org.xml.sax.SAXException) DOMIndexer(org.exist.dom.memtree.DOMIndexer) org.exist.storage.journal(org.exist.storage.journal) Pattern(java.util.regex.Pattern) java.util(java.util) QName(org.exist.dom.QName) SubCollectionEntry(org.exist.collections.Collection.SubCollectionEntry) NodeIterator(org.exist.storage.dom.NodeIterator) DOMFile(org.exist.storage.dom.DOMFile) org.exist.dom.persistent(org.exist.dom.persistent) Function(java.util.function.Function) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) NumberFormat(java.text.NumberFormat) BlobStore(org.exist.storage.blob.BlobStore) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) XMLReader(org.xml.sax.XMLReader) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) NativeSerializer(org.exist.storage.serializers.NativeSerializer) RawNodeIterator(org.exist.storage.dom.RawNodeIterator) VariableByteInput(org.exist.storage.io.VariableByteInput) Node(org.w3c.dom.Node) XmldbURI(org.exist.xmldb.XmldbURI) EXistException(org.exist.EXistException) Indexer(org.exist.Indexer) Nullable(javax.annotation.Nullable) InputSource(org.xml.sax.InputSource) org.exist.collections(org.exist.collections) NodeList(org.w3c.dom.NodeList) ReentrantLock(java.util.concurrent.locks.ReentrantLock) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Sync(org.exist.storage.sync.Sync) TransactionException(org.exist.storage.txn.TransactionException) DOMTransaction(org.exist.storage.dom.DOMTransaction) MessageDigest(org.exist.util.crypto.digest.MessageDigest) DocumentType(org.w3c.dom.DocumentType) BlobId(org.exist.storage.blob.BlobId) TransactionManager(org.exist.storage.txn.TransactionManager) StreamListener(org.exist.indexing.StreamListener) InputStreamUtil.copy(org.exist.util.io.InputStreamUtil.copy) java.io(java.io) NodeId(org.exist.numbering.NodeId) Paths(java.nio.file.Paths) ReindexMode(org.exist.indexing.StreamListener.ReindexMode) EmbeddedXMLStreamReader(org.exist.stax.EmbeddedXMLStreamReader) org.exist.util(org.exist.util) InputStreamUtil(org.exist.util.io.InputStreamUtil) Serializer(org.exist.storage.serializers.Serializer) LogManager(org.apache.logging.log4j.LogManager) org.exist.storage.btree(org.exist.storage.btree) Collection(org.exist.collections.Collection)

Example 3 with Type

use of org.exist.xquery.value.Type in project exist by eXist-db.

the class Shared method getStreamSource.

public static StreamSource getStreamSource(Item item, XQueryContext context) throws XPathException, IOException {
    final StreamSource streamSource = new StreamSource();
    if (item.getType() == Type.JAVA_OBJECT) {
        LOG.debug("Streaming Java object");
        final Object obj = ((JavaObjectValue) item).getObject();
        if (!(obj instanceof File)) {
            throw new XPathException("Passed java object should be a File");
        }
        final File inputFile = (File) obj;
        final InputStream is = new FileInputStream(inputFile);
        streamSource.setInputStream(is);
        streamSource.setSystemId(inputFile.toURI().toURL().toString());
    } else if (item.getType() == Type.ANY_URI) {
        LOG.debug("Streaming xs:anyURI");
        // anyURI provided
        String url = item.getStringValue();
        // Fix URL
        if (url.startsWith("/")) {
            url = "xmldb:exist://" + url;
        }
        final InputStream is = new URL(url).openStream();
        streamSource.setInputStream(is);
        streamSource.setSystemId(url);
    } else if (item.getType() == Type.ELEMENT || item.getType() == Type.DOCUMENT) {
        LOG.debug("Streaming element or document node");
        if (item instanceof NodeProxy) {
            final NodeProxy np = (NodeProxy) item;
            final String url = "xmldb:exist://" + np.getOwnerDocument().getBaseURI();
            LOG.debug("Document detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
        // Node provided
        final DBBroker broker = context.getBroker();
        final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
            final Serializer serializer = broker.borrowSerializer();
            try {
                fn.accept(serializer);
            } finally {
                broker.returnSerializer(serializer);
            }
        };
        final NodeValue node = (NodeValue) item;
        final InputStream is = new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        streamSource.setInputStream(is);
    } else if (item.getType() == Type.BASE64_BINARY || item.getType() == Type.HEX_BINARY) {
        LOG.debug("Streaming base64 binary");
        final BinaryValue binary = (BinaryValue) item;
        final byte[] data = binary.toJavaObject(byte[].class);
        final InputStream is = new UnsynchronizedByteArrayInputStream(data);
        streamSource.setInputStream(is);
        if (item instanceof Base64BinaryDocument) {
            final Base64BinaryDocument b64doc = (Base64BinaryDocument) item;
            final String url = "xmldb:exist://" + b64doc.getUrl();
            LOG.debug("Base64BinaryDocument detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
    } else {
        LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
        throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
    return streamSource;
}
Also used : ValidationReport(org.exist.validation.ValidationReport) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeProxy(org.exist.dom.persistent.NodeProxy) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ArrayList(java.util.ArrayList) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeValue(org.exist.xquery.value.NodeValue) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) NodeImpl(org.exist.dom.memtree.NodeImpl) XQueryContext(org.exist.xquery.XQueryContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) InputSource(org.xml.sax.InputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) ValidationReportItem(org.exist.validation.ValidationReportItem) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Logger(org.apache.logging.log4j.Logger) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) DBBroker(org.exist.storage.DBBroker) Serializer(org.exist.storage.serializers.Serializer) Sequence(org.exist.xquery.value.Sequence) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) FileInputStream(java.io.FileInputStream) URL(java.net.URL) DBBroker(org.exist.storage.DBBroker) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) File(java.io.File) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 Serializer (org.exist.storage.serializers.Serializer)3 Type (org.exist.xquery.value.Type)3 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)2 InputSource (org.xml.sax.InputSource)2 FunctionE (com.evolvedbinary.j8fu.function.FunctionE)1 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)1 java.io (java.io)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1