Search in sources :

Example 46 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class LocalCollection method storeXMLResource.

private void storeXMLResource(final LocalXMLResource res) throws XMLDBException {
    final XmldbURI resURI;
    try {
        resURI = XmldbURI.xmldbUriFor(res.getId());
    } catch (final URISyntaxException e) {
        throw new XMLDBException(ErrorCodes.INVALID_URI, e);
    }
    modify().apply((collection, broker, transaction) -> {
        String uri = null;
        if (res.file != null) {
            uri = res.file.toUri().toASCIIString();
        }
        try (final ManagedDocumentLock documentLock = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(collection.getURI().append(resURI))) {
            final String strMimeType = res.getMimeType(broker, transaction);
            final MimeType mimeType = strMimeType != null ? MimeTable.getInstance().getContentType(strMimeType) : null;
            if (res.root != null) {
                collection.storeDocument(transaction, broker, resURI, res.root, mimeType, res.datecreated, res.datemodified, null, null, null);
            } else {
                final InputSource source;
                if (uri != null) {
                    source = new InputSource(uri);
                } else if (res.inputSource != null) {
                    source = res.inputSource;
                } else {
                    source = new StringInputSource(res.content);
                }
                final XMLReader reader;
                if (useHtmlReader(broker, transaction, res)) {
                    reader = getHtmlReader();
                } else {
                    reader = null;
                }
                broker.storeDocument(transaction, resURI, source, mimeType, res.datecreated, res.datemodified, null, null, reader, collection);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            return null;
        } catch (final EXistException | SAXException e) {
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
        }
    });
}
Also used : ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputSource(org.xml.sax.InputSource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 47 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class AbstractLocal method withDb.

/**
 * Higher-order-function for performing an XMLDB operation on
 * the database
 *
 * @param dbOperation The operation to perform on the database
 * @param <R> The return type of the operation
 *
 * @return the result of the operation
 *
 * @throws XMLDBException if an error occurs when executing the operation.
 */
protected <R> R withDb(final LocalXmldbFunction<R> dbOperation) throws XMLDBException {
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user));
        final Txn transaction = transaction().apply(broker)) {
        try {
            final R result = dbOperation.apply(broker, transaction);
            transaction.commit();
            return result;
        } catch (final XMLDBException | EXistException e) {
            transaction.abort();
            throw e;
        }
    } catch (final EXistException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XMLDBException(org.xmldb.api.base.XMLDBException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 48 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Conditional method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    LOG.debug("Processing xupdate:if ...");
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    final Source source = new StringSource(selectStmt);
    CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    // context.setBackwardsCompatibility(true);
    context.setStaticallyKnownDocuments(docs);
    declareNamespaces(context);
    declareVariables(context);
    if (compiled == null)
        try {
            compiled = xquery.compile(context, source);
        } catch (final IOException e) {
            throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
        }
    Sequence seq = null;
    try {
        seq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (seq.effectiveBooleanValue()) {
        long mods = 0;
        for (final Modification modification : modifications) {
            mods += modification.process(transaction);
            broker.flush();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} modifications processed.", mods);
        }
        return mods;
    } else {
        return 0;
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 49 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Remove method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            final NodeImpl parent = (NodeImpl) getParent(node);
            if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE) {
                throw new EXistException("you cannot remove the document element. Use update " + "instead");
            } else {
                parent.removeChild(transaction, node);
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
        return ql.length;
    } finally {
        unlockDocuments(transaction);
    }
}
Also used : NodeImpl(org.exist.dom.persistent.NodeImpl) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 50 with EXistException

use of org.exist.EXistException in project exist by eXist-db.

the class Update method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    final NodeList children = content;
    if (children.getLength() == 0) {
        return 0;
    }
    int modifications = children.getLength();
    try {
        final StoredNode[] ql = selectAndLock(transaction);
        final NotificationService notifier = broker.getBrokerPool().getNotificationService();
        for (final StoredNode node : ql) {
            if (node == null) {
                LOG.warn("select {} returned empty node", selectStmt);
                continue;
            }
            final DocumentImpl doc = node.getOwnerDocument();
            if (!doc.getPermissions().validate(broker.getCurrentSubject(), Permission.WRITE)) {
                throw new PermissionDeniedException("User '" + broker.getCurrentSubject().getName() + "' does not have permission to write to the document '" + doc.getDocumentURI() + "'!");
            }
            switch(node.getNodeType()) {
                case Node.ELEMENT_NODE:
                    if (modifications == 0) {
                        modifications = 1;
                    }
                    ((ElementImpl) node).update(transaction, children);
                    break;
                case Node.TEXT_NODE:
                    final ElementImpl textParent = (ElementImpl) node.getParentNode();
                    final Node textTemp = children.item(0);
                    final TextImpl text = new TextImpl(textTemp.getNodeValue());
                    modifications = 1;
                    text.setOwnerDocument(doc);
                    textParent.updateChild(transaction, node, text);
                    break;
                case Node.ATTRIBUTE_NODE:
                    final ElementImpl attrParent = (ElementImpl) ((Attr) node).getOwnerElement();
                    if (attrParent == null) {
                        LOG.warn("parent node not found for {}", node.getNodeId());
                        break;
                    }
                    final AttrImpl attr = (AttrImpl) node;
                    final Node attrTemp = children.item(0);
                    final AttrImpl attribute = new AttrImpl(attr.getQName(), attrTemp.getNodeValue(), broker.getBrokerPool().getSymbols());
                    attribute.setOwnerDocument(doc);
                    attrParent.updateChild(transaction, node, attribute);
                    break;
                default:
                    throw new EXistException("unsupported node-type");
            }
            doc.setLastModified(System.currentTimeMillis());
            modifiedDocuments.add(doc);
            broker.storeXMLResource(transaction, doc);
            notifier.notifyUpdate(doc, UpdateListener.UPDATE);
        }
        checkFragmentation(transaction, modifiedDocuments);
    } finally {
        unlockDocuments(transaction);
    }
    return modifications;
}
Also used : ElementImpl(org.exist.dom.persistent.ElementImpl) NodeList(org.w3c.dom.NodeList) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) NotificationService(org.exist.storage.NotificationService) PermissionDeniedException(org.exist.security.PermissionDeniedException) AttrImpl(org.exist.dom.persistent.AttrImpl) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) TextImpl(org.exist.dom.persistent.TextImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Aggregations

EXistException (org.exist.EXistException)218 PermissionDeniedException (org.exist.security.PermissionDeniedException)80 IOException (java.io.IOException)63 DBBroker (org.exist.storage.DBBroker)58 Txn (org.exist.storage.txn.Txn)46 XmldbURI (org.exist.xmldb.XmldbURI)42 Collection (org.exist.collections.Collection)41 SAXException (org.xml.sax.SAXException)32 LockException (org.exist.util.LockException)31 DocumentImpl (org.exist.dom.persistent.DocumentImpl)28 Subject (org.exist.security.Subject)23 XPathException (org.exist.xquery.XPathException)22 LockedDocument (org.exist.dom.persistent.LockedDocument)21 TriggerException (org.exist.collections.triggers.TriggerException)20 Path (java.nio.file.Path)19 URISyntaxException (java.net.URISyntaxException)18 BrokerPool (org.exist.storage.BrokerPool)18 TransactionManager (org.exist.storage.txn.TransactionManager)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17