Search in sources :

Example 1 with LockToken

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

the class ExistDocument method getCurrentLock.

/**
 * Get lock token from database.
 *
 * @return current lock token.
 */
public LockToken getCurrentLock() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Get current lock {}", xmldbUri);
    }
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.READ_LOCK)) {
        final DocumentImpl document = lockedDocument.getDocument();
        if (document == null) {
            LOG.debug("No resource found for path: {}", xmldbUri);
            return null;
        }
        // TODO consider. A Webdav lock can be set without subject lock.
        Account lock = document.getUserLock();
        if (lock == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document {} does not contain userlock", xmldbUri);
            }
            return null;
        }
        // Retrieve Locktoken from document metadata
        org.exist.dom.persistent.LockToken token = document.getLockToken();
        if (token == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document meta data does not contain a LockToken");
            }
            return null;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Successfully retrieved token");
        }
        return token;
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        return null;
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished probe lock");
        }
    }
}
Also used : Account(org.exist.security.Account) DBBroker(org.exist.storage.DBBroker) LockToken(org.exist.dom.persistent.LockToken) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 2 with LockToken

use of org.exist.dom.persistent.LockToken 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 LockToken (org.exist.dom.persistent.LockToken)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)1 Txn (org.exist.storage.txn.Txn)1 DocumentNotLockedException (org.exist.webdav.exceptions.DocumentNotLockedException)1