Search in sources :

Example 11 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class AbstractRealm method initialiseRealmStorage.

private void initialiseRealmStorage(final DBBroker broker, final Txn transaction) throws EXistException {
    final XmldbURI realmCollectionURL = SecurityManager.SECURITY_COLLECTION_URI.append(getId());
    try {
        collectionRealm = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL);
        collectionAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts"));
        collectionGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups"));
        collectionRemovedAccounts = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("accounts").append("removed"));
        collectionRemovedGroups = Utils.getOrCreateCollection(broker, transaction, realmCollectionURL.append("groups").append("removed"));
    } catch (final PermissionDeniedException | IOException | TriggerException | LockException e) {
        throw new EXistException(e);
    }
}
Also used : LockException(org.exist.util.LockException) IOException(java.io.IOException) EXistException(org.exist.EXistException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 12 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class ExistDocument method lock.

/**
 * Lock document.
 *
 * @param inputToken Lock token.
 * @return Input lock token.
 * @throws PermissionDeniedException Permission denied
 * @throws DocumentAlreadyLockedException Document is already locked
 * @throws EXistException Generic existdb exception
 */
public LockToken lock(LockToken inputToken) throws PermissionDeniedException, DocumentAlreadyLockedException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("create lock {}", xmldbUri);
    }
    // Try to get document
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("No resource found for path: {}", xmldbUri);
            }
            // return null; // throw exception?
            throw new EXistException("No resource found.");
        }
        // Get current userlock
        Account userLock = document.getUserLock();
        // Check if Resource is already locked. @@ToDo
        if (userLock != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource was already locked, ignored.");
            }
        }
        if (userLock != null && userLock.getName() != null && !userLock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource is locked by user {}.", userLock.getName());
            }
            throw new PermissionDeniedException(userLock.getName());
        }
        // Check for request for shared lock. @@TODO
        if (inputToken.getScope() == LockToken.LockScope.SHARED) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Shared locks are not implemented.");
            }
            throw new EXistException("Shared locks are not implemented.");
        }
        // Update locktoken
        inputToken.setOwner(subject.getName());
        inputToken.createOpaqueLockToken();
        // inputToken.setTimeOut(inputToken.getTimeOut());
        inputToken.setTimeOut(LockToken.LOCK_TIMEOUT_INFINITE);
        // Update document
        document.setLockToken(inputToken);
        document.setUserLock(subject);
        // Make token persistant
        final TransactionManager txnManager = brokerPool.getTransactionManager();
        try (final Txn txn = txnManager.beginTransaction()) {
            broker.storeMetadata(txn, document);
            txnManager.commit(txn);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully retrieved token");
        }
        return inputToken;
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } catch (TriggerException e) {
        LOG.error(e);
        throw new EXistException(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished create lock");
        }
    }
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) Txn(org.exist.storage.txn.Txn) TriggerException(org.exist.collections.triggers.TriggerException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 13 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class ExistDocument method resourceCopyMove.

/**
 * Copy document or collection in database.
 */
void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("{} {} to {} named {}", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
    }
    XmldbURI newNameUri = null;
    try {
        newNameUri = XmldbURI.xmldbUriFor(newName);
    } catch (URISyntaxException ex) {
        LOG.error(ex);
        throw new EXistException(ex.getMessage());
    }
    // use WRITE_LOCK if moving or if src and dest collection are the same
    final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(xmldbUri.removeLastSegment()) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
    DocumentImpl srcDocument = null;
    // Need to split path into collection and document name
    final XmldbURI srcCollectionUri = xmldbUri.removeLastSegment();
    final XmldbURI srdDocumentUri = xmldbUri.lastSegment();
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
        // Open collection if possible, else abort
        if (srcCollection == null) {
            txnManager.abort(txn);
            // TODO throw
            return;
        }
        // Open document if possible, else abort
        srcDocument = srcCollection.getDocument(broker, srdDocumentUri);
        if (srcDocument == null) {
            LOG.debug("No resource found for path: {}", xmldbUri);
            txnManager.abort(txn);
            return;
        }
        // Open collection if possible, else abort
        try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
            if (destCollection == null) {
                LOG.debug("Destination collection {} does not exist.", xmldbUri);
                txnManager.abort(txn);
                return;
            }
            // Perform actial move/copy
            if (mode == Mode.COPY) {
                broker.copyResource(txn, srcDocument, destCollection, newNameUri);
            } else {
                broker.moveResource(txn, srcDocument, destCollection, newNameUri);
            }
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document {}d successfully", mode);
            }
        }
    } catch (LockException e) {
        LOG.error("Resource is locked.", e);
        throw new EXistException(e.getMessage());
    } catch (EXistException e) {
        LOG.error(e);
        throw e;
    } catch (IOException | PermissionDeniedException | TriggerException e) {
        LOG.error(e);
        throw new EXistException(e.getMessage());
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished {}", mode);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) DBBroker(org.exist.storage.DBBroker) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 14 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class ExistCollection method resourceCopyMove.

void resourceCopyMove(XmldbURI destCollectionUri, String newName, Mode mode) throws EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("{} '{}' to '{}' named '{}'", String.valueOf(mode), xmldbUri, destCollectionUri, newName);
    }
    XmldbURI newNameUri = null;
    try {
        newNameUri = XmldbURI.xmldbUriFor(newName);
    } catch (URISyntaxException ex) {
        LOG.error(ex);
        throw new EXistException(ex.getMessage());
    }
    // This class contains already the URI of the resource that shall be moved/copied
    XmldbURI srcCollectionUri = xmldbUri;
    // use WRITE_LOCK if moving or if src and dest collection are the same
    final LockMode srcCollectionLockMode = mode == Mode.MOVE || destCollectionUri.equals(srcCollectionUri) ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection srcCollection = broker.openCollection(srcCollectionUri, srcCollectionLockMode)) {
        // Open collection if possible, else abort
        if (srcCollection == null) {
            txnManager.abort(txn);
            // TODO throw
            return;
        }
        // Open collection if possible, else abort
        try (final Collection destCollection = broker.openCollection(destCollectionUri, LockMode.WRITE_LOCK)) {
            if (destCollection == null) {
                LOG.debug("Destination collection {} does not exist.", xmldbUri);
                txnManager.abort(txn);
                // TODO throw?
                return;
            }
            // Perform actial move/copy
            if (mode == Mode.COPY) {
                broker.copyCollection(txn, srcCollection, destCollection, newNameUri);
            } else {
                broker.moveCollection(txn, srcCollection, destCollection, newNameUri);
            }
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Collection {}d successfully", mode);
            }
        }
    } catch (LockException e) {
        LOG.error("Resource is locked.", e);
        throw new EXistException(e.getMessage());
    } catch (EXistException e) {
        LOG.error(e);
        throw e;
    } catch (IOException | PermissionDeniedException | TriggerException e) {
        LOG.error(e);
        throw new EXistException(e.getMessage());
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished {}", mode);
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 15 with TriggerException

use of org.exist.collections.triggers.TriggerException in project exist by eXist-db.

the class RestoreHandler method restoreDeletedEntry.

private void restoreDeletedEntry(final Attributes atts) {
    final String name = atts.getValue("name");
    final String type = atts.getValue("type");
    if ("collection".equals(type)) {
        try {
            try (final Txn transaction = beginTransaction();
                final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK)) {
                if (collection != null) {
                    final boolean triggersEnabled = broker.isTriggersEnabled();
                    try {
                        broker.setTriggersEnabled(false);
                        broker.removeCollection(transaction, collection);
                    } finally {
                        // restore triggers enabled setting
                        broker.setTriggersEnabled(triggersEnabled);
                    }
                }
                transaction.commit();
            }
        } catch (final PermissionDeniedException | IOException | TriggerException | TransactionException e) {
            listener.warn("Failed to remove deleted collection: " + name + ": " + e.getMessage());
        }
    } else if ("resource".equals(type)) {
        final XmldbURI docName = XmldbURI.create(name);
        try (final Txn transaction = beginTransaction();
            final Collection collection = broker.openCollection(currentCollectionUri.append(name), Lock.LockMode.WRITE_LOCK);
            final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, docName, Lock.LockMode.WRITE_LOCK)) {
            // Check that the document exists
            if (lockedDocument != null) {
                final boolean triggersEnabled = broker.isTriggersEnabled();
                try {
                    broker.setTriggersEnabled(false);
                    final boolean xmlType = !(lockedDocument.getDocument() instanceof BinaryDocument);
                    if (xmlType) {
                        collection.removeXMLResource(transaction, broker, docName);
                    } else {
                        collection.removeBinaryResource(transaction, broker, docName);
                    }
                } finally {
                    // restore triggers enabled setting
                    broker.setTriggersEnabled(triggersEnabled);
                }
            }
            transaction.commit();
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
        } catch (final PermissionDeniedException | TransactionException | TriggerException | LockException | IOException e) {
            listener.warn("Failed to remove deleted resource: " + name + ": " + e.getMessage());
        }
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) TransactionException(org.exist.storage.txn.TransactionException) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

TriggerException (org.exist.collections.triggers.TriggerException)25 EXistException (org.exist.EXistException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)18 Txn (org.exist.storage.txn.Txn)13 XmldbURI (org.exist.xmldb.XmldbURI)11 IOException (java.io.IOException)10 Collection (org.exist.collections.Collection)10 LockException (org.exist.util.LockException)10 DocumentImpl (org.exist.dom.persistent.DocumentImpl)9 DBBroker (org.exist.storage.DBBroker)7 TransactionManager (org.exist.storage.txn.TransactionManager)6 Sequence (org.exist.xquery.value.Sequence)6 LockedDocument (org.exist.dom.persistent.LockedDocument)5 NotificationService (org.exist.storage.NotificationService)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 StoredNode (org.exist.dom.persistent.StoredNode)4 XPathException (org.exist.xquery.XPathException)4 URISyntaxException (java.net.URISyntaxException)3 Path (java.nio.file.Path)3