Search in sources :

Example 1 with DocumentNotLockedException

use of org.exist.webdav.exceptions.DocumentNotLockedException in project exist by eXist-db.

the class ExistDocument method unlock.

/**
 * Unlock document in database.
 */
void unlock() throws PermissionDeniedException, DocumentNotLockedException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("unlock {}", xmldbUri);
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    // Try to get document
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.WRITE_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            final String msg = String.format("No resource found for path: %s", xmldbUri);
            LOG.debug(msg);
            throw new EXistException(msg);
        }
        // Get current userlock
        Account lock = document.getUserLock();
        // Check if Resource is already locked.
        if (lock == null) {
            LOG.debug("Resource {} is not locked.", xmldbUri);
            throw new DocumentNotLockedException("" + xmldbUri);
        }
        // Check if Resource is from subject
        if (!lock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            LOG.debug("Resource lock is from user {}", lock.getName());
            throw new PermissionDeniedException(lock.getName());
        }
        // Update document
        document.setUserLock(null);
        document.setLockToken(null);
        // Make it persistant
        broker.storeMetadata(txn, document);
        txnManager.commit(txn);
    } 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) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) DocumentNotLockedException(org.exist.webdav.exceptions.DocumentNotLockedException) TriggerException(org.exist.collections.triggers.TriggerException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 2 with DocumentNotLockedException

use of org.exist.webdav.exceptions.DocumentNotLockedException in project exist by eXist-db.

the class ExistDocument method refreshLock.

public LockToken refreshLock(String token) throws PermissionDeniedException, DocumentAlreadyLockedException, EXistException, DocumentNotLockedException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("refresh lock {}  lock={}", xmldbUri, token);
    }
    if (token == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("token is null");
        }
        throw new EXistException("token is null");
    }
    // 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.
        if (userLock == null) {
            final String msg = "Resource was not locked.";
            if (LOG.isDebugEnabled()) {
                LOG.debug(msg);
            }
            throw new DocumentNotLockedException(msg);
        }
        if (userLock.getName() != null && !userLock.getName().equals(subject.getName()) && !subject.hasDbaRole()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resource is locked by {}", userLock.getName());
            }
            throw new PermissionDeniedException(userLock.getName());
        }
        LockToken lockToken = document.getLockToken();
        if (!token.equals(lockToken.getOpaqueLockToken())) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Token does not match");
            }
            throw new PermissionDeniedException(String.format("Token %s does not match %s", token, lockToken.getOpaqueLockToken()));
        }
        lockToken.setTimeOut(LockToken.LOCK_TIMEOUT_INFINITE);
        // Make token persistant
        final TransactionManager txnManager = brokerPool.getTransactionManager();
        try (final Txn txn = txnManager.beginTransaction()) {
            broker.storeXMLResource(txn, document);
            txnManager.commit(txn);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully retrieved token");
        }
        return lockToken;
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw 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) LockToken(org.exist.dom.persistent.LockToken) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) Txn(org.exist.storage.txn.Txn) DocumentNotLockedException(org.exist.webdav.exceptions.DocumentNotLockedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

EXistException (org.exist.EXistException)2 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 LockedDocument (org.exist.dom.persistent.LockedDocument)2 Account (org.exist.security.Account)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 DocumentNotLockedException (org.exist.webdav.exceptions.DocumentNotLockedException)2 TriggerException (org.exist.collections.triggers.TriggerException)1 LockToken (org.exist.dom.persistent.LockToken)1