Search in sources :

Example 1 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class ResourceDeferredPermission method apply.

@Override
public void apply(final DBBroker broker, final Txn transaction) {
    try (final LockedDocument lockedDoc = broker.getXMLResource(getTarget(), Lock.LockMode.WRITE_LOCK)) {
        final DocumentImpl doc = lockedDoc.getDocument();
        final Permission permission = doc.getPermissions();
        PermissionFactory.chown(broker, permission, Optional.ofNullable(getOwner()), Optional.ofNullable(getGroup()));
        PermissionFactory.chmod(broker, permission, Optional.of(getMode()), Optional.ofNullable(permission instanceof ACLPermission ? getAces() : null));
        broker.storeXMLResource(transaction, doc);
    } catch (final PermissionDeniedException e) {
        final String msg = "ERROR: Failed to set permissions on Document '" + getTarget() + "'.";
        LOG.error(msg, e);
        getListener().warn(msg);
    }
}
Also used : ACLPermission(org.exist.security.ACLPermission) LockedDocument(org.exist.dom.persistent.LockedDocument) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 2 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class Configurator method parse.

public static Configuration parse(final Configurable instance, final DBBroker broker, final Collection collection, final XmldbURI fileURL) throws ConfigurationException {
    final FullXmldbURI key = getFullURI(broker.getBrokerPool(), collection.getURI().append(fileURL));
    Configuration conf = hotConfigs.get(key);
    if (conf != null) {
        return conf;
    }
    // XXX: locking required
    DocumentImpl document;
    try {
        document = collection.getDocument(broker, fileURL);
    } catch (final PermissionDeniedException pde) {
        throw new ConfigurationException(pde.getMessage(), pde);
    }
    if (document == null) {
        if (broker.isReadOnly()) {
            // create in memory document & configuration
            try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
                final Writer writer = new OutputStreamWriter(os)) {
                final SAXSerializer serializer = new SAXSerializer(writer, null);
                serializer.startDocument();
                serialize(instance, serializer);
                serializer.endDocument();
                if (os.size() == 0) {
                    return null;
                }
                return parse(os.toInputStream());
            } catch (final IOException | SAXException e) {
                throw new ConfigurationException(e.getMessage(), e);
            }
        }
        try {
            document = save(instance, broker, collection, fileURL);
        } catch (final IOException e) {
            LOG.error(e.getMessage(), e);
            // TODO : throw exception ? -pb
            return null;
        }
    }
    if (document == null) {
        // possibly on corrupted database, find better solution (recovery flag?)
        return null;
    }
    final Element confElement = document.getDocumentElement();
    if (confElement == null) {
        // possibly on corrupted database, find better solution (recovery flag?)
        return null;
    }
    conf = new ConfigurationImpl(confElement);
    hotConfigs.put(key, conf);
    return conf;
}
Also used : Element(org.w3c.dom.Element) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FullXmldbURI(org.exist.xmldb.FullXmldbURI) SAXSerializer(org.exist.util.serializer.SAXSerializer)

Example 3 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class XQueryURLRewrite method getSource.

private SourceInfo getSource(final DBBroker broker, final String moduleLoadPath) throws ServletException {
    final SourceInfo sourceInfo;
    if (query.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        // Is the module source stored in the database?
        try {
            final XmldbURI locationUri = XmldbURI.xmldbUriFor(query);
            try (final LockedDocument lockedSourceDoc = broker.getXMLResource(locationUri.toCollectionPathURI(), LockMode.READ_LOCK)) {
                if (lockedSourceDoc == null) {
                    throw new ServletException("XQuery resource: " + query + " not found in database");
                }
                final DocumentImpl sourceDoc = lockedSourceDoc.getDocument();
                if (sourceDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(sourceDoc.getMimeType())) {
                    throw new ServletException("XQuery resource: " + query + " is not an XQuery or " + "declares a wrong mime-type");
                }
                sourceInfo = new SourceInfo(new DBSource(broker, (BinaryDocument) sourceDoc, true), locationUri.toString());
            } catch (final PermissionDeniedException e) {
                throw new ServletException("permission denied to read module source from " + query);
            }
        } catch (final URISyntaxException e) {
            throw new ServletException(e.getMessage(), e);
        }
    } else {
        try {
            sourceInfo = new SourceInfo(SourceFactory.getSource(broker, moduleLoadPath, query, true), moduleLoadPath);
        } catch (final IOException e) {
            throw new ServletException("IO error while reading XQuery source: " + query);
        } catch (final PermissionDeniedException e) {
            throw new ServletException("Permission denied while reading XQuery source: " + query);
        }
    }
    return sourceInfo;
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI)

Example 4 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class DOMFile method findValue.

/**
 * Retrieve node at virtual address.
 *
 * @param broker the database broker
 * @param node The virtual address
 * @return The reference of the node
 * @throws IOException if an I/O error occurs
 * @throws BTreeException if an error occurs reading the tree
 */
protected long findValue(final DBBroker broker, final NodeProxy node) throws IOException, BTreeException {
    if (LOG.isDebugEnabled() && !lockManager.isBtreeLocked(getLockName())) {
        LOG.debug("The file doesn't own a lock");
    }
    final DocumentImpl doc = node.getOwnerDocument();
    final NodeRef nodeRef = new NativeBroker.NodeRef(doc.getDocId(), node.getNodeId());
    // first try to find the node in the index
    final long pointer = findValue(nodeRef);
    if (pointer == KEY_NOT_FOUND) {
        // node not found in index: try to find the nearest available
        // ancestor and traverse it
        NodeId nodeID = node.getNodeId();
        long parentPointer = KEY_NOT_FOUND;
        do {
            nodeID = nodeID.getParentId();
            if (nodeID == null) {
                SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":<null nodeID> not found.");
                throw new BTreeException("Node not found.");
            }
            if (nodeID == NodeId.DOCUMENT_NODE) {
                SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":" + nodeID + " not found.");
                return KEY_NOT_FOUND;
            }
            final NativeBroker.NodeRef parentRef = new NativeBroker.NodeRef(doc.getDocId(), nodeID);
            try {
                parentPointer = findValue(parentRef);
            } catch (final BTreeException bte) {
                LOG.error("report me", bte);
            }
        } while (parentPointer == KEY_NOT_FOUND);
        try {
            final int thisLevel = nodeID.getTreeLevel();
            // lazily initialized below
            Integer childLevel = null;
            final NodeProxy parent = new NodeProxy(doc, nodeID, parentPointer);
            final EmbeddedXMLStreamReader reader = (EmbeddedXMLStreamReader) broker.getXMLStreamReader(parent, true);
            while (reader.hasNext()) {
                final int status = reader.next();
                if (status != XMLStreamReader.END_ELEMENT) {
                    if (childLevel == null) {
                        childLevel = reader.getNode().getNodeType() == Node.ELEMENT_NODE ? thisLevel + 1 : thisLevel;
                    }
                    final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                    if (otherId.equals(node.getNodeId())) {
                        return reader.getCurrentPosition();
                    }
                }
                if (status == XMLStreamConstants.END_ELEMENT) {
                    final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                    final int otherLevel = otherId.getTreeLevel();
                    if (childLevel != null && childLevel != otherLevel && otherLevel == thisLevel) {
                        // exit-while
                        break;
                    }
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Node {} could not be found. Giving up. This is usually not an error.", node.getNodeId());
            }
            return KEY_NOT_FOUND;
        } catch (final XMLStreamException e) {
            SanityCheck.TRACE("Node " + node.getOwnerDocument().getDocId() + ":" + node.getNodeId() + " not found.");
            throw new BTreeException("Node " + node.getNodeId() + " not found.");
        }
    } else {
        return pointer;
    }
}
Also used : NodeRef(org.exist.storage.NativeBroker.NodeRef) BTreeException(org.exist.storage.btree.BTreeException) XMLStreamException(javax.xml.stream.XMLStreamException) NodeId(org.exist.numbering.NodeId) EmbeddedXMLStreamReader(org.exist.stax.EmbeddedXMLStreamReader) NativeBroker(org.exist.storage.NativeBroker) NodeRef(org.exist.storage.NativeBroker.NodeRef) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 5 with DocumentImpl

use of org.exist.dom.persistent.DocumentImpl in project exist by eXist-db.

the class RootNode method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    // first, if we have been explicitly given a context item or context sequence, we can just use that
    if (contextItem != null) {
        return new ValueSequence(contextItem);
    } else if (contextSequence != null && contextSequence != Sequence.EMPTY_SEQUENCE) {
        return contextSequence;
    }
    // second, check if a context item is declared
    final ContextItemDeclaration decl = context.getContextItemDeclartion();
    if (decl != null) {
        final Sequence seq = decl.eval(null, null);
        if (!seq.isEmpty()) {
            final Item item = seq.itemAt(0);
            // context item must be a node
            if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                throw new XPathException(this, ErrorCodes.XPTY0020, "Context item is not a node");
            }
            final NodeValue node = (NodeValue) item;
            // return fn:root(self::node()) treat as document-node()
            if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                return new NodeProxy(((NodeProxy) item).getOwnerDocument());
            } else {
                if (node.getType() == Type.DOCUMENT) {
                    return node;
                }
                return (org.exist.dom.memtree.DocumentImpl) node.getOwnerDocument();
            }
        }
        return Sequence.EMPTY_SEQUENCE;
    }
    // get statically known documents from the context
    DocumentSet ds = context.getStaticallyKnownDocuments();
    if (ds == null || ds.getDocumentCount() == 0) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // fix for util:eval-with-context
    if (contextSequence != null) {
        if (!contextSequence.isEmpty()) {
            final Item item = contextSequence.itemAt(0);
            // context item must be a node
            if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                throw new XPathException(this, ErrorCodes.XPTY0020, "Context item is not a node");
            }
            final NodeValue node = (NodeValue) item;
            // return fn:root(self::node()) treat as document-node()
            if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
                return new NodeProxy(((NodeProxy) item).getOwnerDocument());
            } else {
                if (node.getType() == Type.DOCUMENT) {
                    return node;
                }
                return (org.exist.dom.memtree.DocumentImpl) node.getOwnerDocument();
            }
        } else {
            return Sequence.EMPTY_SEQUENCE;
        }
    }
    // // if the expression occurs in a nested context, we might have cached the
    // // document set
    // // TODO: disabled cache for now as it may cause concurrency issues
    // // better use compile-time inspection and maybe a pragma to mark those
    // // sections in the query that can be safely cached
    // if (cachedDocs != null && cachedDocs.equalDocs(ds)) return cached;
    // check if the loaded documents should remain locked
    NewArrayNodeSet result = new NewArrayNodeSet();
    // NOTE(AR) locking the documents here does not actually do anything useful, eXist-db will still exhibit weak isolation with concurrent updates
    // ManagedLocks<ManagedDocumentLock> docLocks = null;
    // try {
    // // wait for pending updates
    // if (!context.inProtectedMode()) {
    // docLocks = ds.lock(context.getBroker(), false);
    // }
    DocumentImpl doc;
    for (final Iterator<DocumentImpl> i = ds.getDocumentIterator(); i.hasNext(); ) {
        doc = i.next();
        if (context.inProtectedMode() && !context.getProtectedDocs().containsKey(doc.getDocId())) {
            continue;
        }
        if (doc.getResourceType() == DocumentImpl.XML_FILE) {
            // skip binary resources
            result.add(new NodeProxy(doc));
        }
    }
    cached = result;
    cachedDocs = ds;
    // result.updateNoSort();
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    registerUpdateListener();
    return result;
}
Also used : NewArrayNodeSet(org.exist.dom.persistent.NewArrayNodeSet) NodeProxy(org.exist.dom.persistent.NodeProxy) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DocumentSet(org.exist.dom.persistent.DocumentSet)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11