Search in sources :

Example 46 with LockedDocument

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

the class XQueryURLRewrite method findSourceFromDb.

@Nullable
private SourceInfo findSourceFromDb(final DBBroker broker, final String basePath, final String path, final String[] components) {
    LockedDocument lockedControllerDoc = null;
    try {
        final XmldbURI locationUri = XmldbURI.xmldbUriFor(basePath);
        XmldbURI resourceUri = locationUri;
        for (final String component : components) {
            resourceUri = resourceUri.append(component);
        }
        lockedControllerDoc = findDbControllerXql(broker, locationUri, resourceUri);
        if (lockedControllerDoc == null) {
            LOG.warn("XQueryURLRewrite controller could not be found for path: {}", path);
            return null;
        }
        final DocumentImpl controllerDoc = lockedControllerDoc.getDocument();
        if (LOG.isTraceEnabled()) {
            LOG.trace("Found controller file: {}", controllerDoc.getURI());
        }
        if (controllerDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(controllerDoc.getMimeType())) {
            LOG.warn("XQuery resource: {} is not an XQuery or declares a wrong mime-type", query);
            return null;
        }
        final String controllerPath = controllerDoc.getCollection().getURI().getRawCollectionPath();
        return new SourceInfo(new DBSource(broker, (BinaryDocument) controllerDoc, true), "xmldb:exist://" + controllerPath, controllerPath.substring(locationUri.getCollectionPath().length()));
    } catch (final URISyntaxException e) {
        LOG.warn("Bad URI for base path: {}", e.getMessage(), e);
        return null;
    } finally {
        if (lockedControllerDoc != null) {
            lockedControllerDoc.close();
        }
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) URISyntaxException(java.net.URISyntaxException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) XmldbURI(org.exist.xmldb.XmldbURI) Nullable(javax.annotation.Nullable)

Example 47 with LockedDocument

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

the class GetXMLResourceNoLockTest method testCollectionMaintainsLockWhenResourceIsSelectedNoLock.

@Test
public void testCollectionMaintainsLockWhenResourceIsSelectedNoLock() throws EXistException, LockException, SAXException, PermissionDeniedException, IOException {
    storeTestResource();
    final BrokerPool pool = BrokerPool.getInstance();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Collection testCollection = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
        final XmldbURI docPath = TestConstants.TEST_COLLECTION_URI.append(DOCUMENT_NAME_URI);
        try (final LockedDocument lockedDoc = broker.getXMLResource(docPath, LockMode.NO_LOCK)) {
            // if document is not present, null is returned
            if (lockedDoc == null) {
                fail("Binary document '" + docPath + " does not exist.");
            }
        }
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        final MultiLock colLock = lockManager.getPathLock(testCollection.getURI().toString());
        assertEquals("Collection does not have lock!", true, colLock.getReadHoldCount() > 0);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) MultiLock(uk.ac.ic.doc.slurp.multilock.MultiLock) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 48 with LockedDocument

use of org.exist.dom.persistent.LockedDocument 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 49 with LockedDocument

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

the class ExistDocument method stream.

/**
 * Stream document to framework.
 *
 * @param os Outputstream.
 * @throws IOException IO exception
 * @throws PermissionDeniedException permission is denied.
 */
public void stream(OutputStream os) throws IOException, PermissionDeniedException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Stream started");
    }
    long startTime = System.currentTimeMillis();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject))) {
        // If it is not a collection, check if it is a document
        try (final LockedDocument lockedDocument = broker.getXMLResource(xmldbUri, LockMode.READ_LOCK)) {
            final DocumentImpl document = lockedDocument.getDocument();
            if (document.getResourceType() == DocumentImpl.XML_FILE) {
                try {
                    serialize(broker, document, os);
                    os.flush();
                } catch (SAXException e) {
                    LOG.error(e);
                    throw new IOException(String.format("Error while serializing XML document: %s", e.getMessage()), e);
                }
            } else {
                // Stream NON-XML document
                broker.readBinaryResource((BinaryDocument) document, os);
                os.flush();
            }
        }
    } catch (EXistException e) {
        LOG.error(e);
        throw new IOException(e.getMessage());
    } catch (PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Stream stopped, duration {} msec.", System.currentTimeMillis() - startTime);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) SAXException(org.xml.sax.SAXException)

Example 50 with LockedDocument

use of org.exist.dom.persistent.LockedDocument 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)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)91 DocumentImpl (org.exist.dom.persistent.DocumentImpl)40 XmldbURI (org.exist.xmldb.XmldbURI)39 DBBroker (org.exist.storage.DBBroker)36 PermissionDeniedException (org.exist.security.PermissionDeniedException)30 Collection (org.exist.collections.Collection)28 Txn (org.exist.storage.txn.Txn)27 BrokerPool (org.exist.storage.BrokerPool)21 EXistException (org.exist.EXistException)20 Test (org.junit.Test)20 IOException (java.io.IOException)18 BinaryDocument (org.exist.dom.persistent.BinaryDocument)18 Serializer (org.exist.storage.serializers.Serializer)15 XPathException (org.exist.xquery.XPathException)14 URISyntaxException (java.net.URISyntaxException)13 InputStream (java.io.InputStream)11 LockException (org.exist.util.LockException)11 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)9 Lock (org.exist.storage.lock.Lock)9 TransactionManager (org.exist.storage.txn.TransactionManager)9