Search in sources :

Example 21 with LockedDocument

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

the class GetField method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    XmldbURI uri = XmldbURI.createInternal(args[0].getStringValue());
    String field = args[1].getStringValue();
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(uri, LockMode.READ_LOCK)) {
        if (lockedDoc == null) {
            return Sequence.EMPTY_SEQUENCE;
        }
        // Get the lucene worker
        final LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        final String content = index.getFieldContent(lockedDoc.getDocument().getDocId(), field);
        return content == null ? Sequence.EMPTY_SEQUENCE : new org.exist.xquery.value.StringValue(content);
    } catch (PermissionDeniedException e) {
        throw new XPathException(this, LuceneModule.EXXQDYFT0001, "Permission denied to read document " + args[0].getStringValue());
    } catch (IOException e) {
        throw new XPathException(this, LuceneModule.EXXQDYFT0002, "IO error while reading document " + args[0].getStringValue());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker)

Example 22 with LockedDocument

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

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

the class ExistDocument method initMetadata.

/**
 * Initialize Collection, authenticate() is required first
 */
@Override
public void initMetadata() {
    if (subject == null) {
        LOG.error("User not initialized yet");
        return;
    }
    // check if initialization is required
    if (isInitialized) {
        LOG.debug("Already initialized");
        return;
    }
    try (final DBBroker broker = brokerPool.get(Optional.of(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) {
                isXmlDocument = true;
            }
            // Get meta data
            creationTime = document.getCreated();
            lastModified = document.getLastModified();
            mimeType = document.getMimeType();
            // Retrieve perssions
            permissions = document.getPermissions();
            readAllowed = permissions.validate(subject, Permission.READ);
            writeAllowed = permissions.validate(subject, Permission.WRITE);
            executeAllowed = permissions.validate(subject, Permission.EXECUTE);
            ownerUser = permissions.getOwner().getUsername();
            ownerGroup = permissions.getGroup().getName();
            // Get (estimated) file size
            contentLength = document.getContentLength();
        }
    } catch (final EXistException | PermissionDeniedException e) {
        LOG.error(e);
    }
    isInitialized = true;
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 24 with LockedDocument

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

the class ExistDocument method delete.

/**
 * Remove document from database.
 */
void delete() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting {}", xmldbUri);
    }
    // Need to split path into collection and document name
    final XmldbURI collName = xmldbUri.removeLastSegment();
    final XmldbURI docName = xmldbUri.lastSegment();
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(collName, LockMode.WRITE_LOCK)) {
        // Open collection if possible, else abort
        if (collection == null) {
            LOG.debug("Collection does not exist");
            txnManager.abort(txn);
            return;
        }
        // Open document if possible, else abort
        try (final LockedDocument lockedResource = collection.getDocumentWithLock(broker, docName, LockMode.WRITE_LOCK)) {
            if (lockedResource == null) {
                LOG.debug("No resource found for path: {}", xmldbUri);
                txnManager.abort(txn);
                return;
            }
            final DocumentImpl resource = lockedResource.getDocument();
            if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
                collection.removeBinaryResource(txn, broker, resource.getFileURI());
            } else {
                collection.removeXMLResource(txn, broker, resource.getFileURI());
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            collection.close();
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Document deleted sucessfully");
            }
        }
    } catch (final LockException e) {
        LOG.error("Resource is locked.", e);
    } catch (final EXistException | IOException | TriggerException | PermissionDeniedException e) {
        LOG.error(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished delete");
        }
    }
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) 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) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 25 with LockedDocument

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

the class ExistResourceFactory method getResourceType.

/*
     * Returns the resource type indicated by the path: either COLLECTION, DOCUMENT or NOT_EXISTING.
     */
private ResourceType getResourceType(BrokerPool brokerPool, XmldbURI xmldbUri) {
    ResourceType type = ResourceType.NOT_EXISTING;
    // MacOsX finder specific files
    String documentSeqment = xmldbUri.lastSegment().toString();
    if (documentSeqment.startsWith("._") || documentSeqment.equals(".DS_Store")) {
    // LOG.debug(String.format("Ignoring MacOSX file '%s'", xmldbUri.lastSegment().toString()));
    // return ResourceType.IGNORABLE;
    }
    // Documents that start with a dot
    if (documentSeqment.startsWith(".")) {
    // LOG.debug(String.format("Ignoring '.' file '%s'", xmldbUri.lastSegment().toString()));
    // return ResourceType.IGNORABLE;
    }
    // is performed.
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Collection collection = broker.openCollection(xmldbUri, LockMode.READ_LOCK)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Path: {}", xmldbUri);
        }
        // First check if resource is a collection
        if (collection != null) {
            type = ResourceType.COLLECTION;
        } else {
            // If it is not a collection, check if it is a document
            try (final LockedDocument lockedDoc = broker.getXMLResource(xmldbUri, LockMode.READ_LOCK)) {
                if (lockedDoc != null) {
                    // Document is found
                    type = ResourceType.DOCUMENT;
                } else {
                    // No document and no collection.
                    type = ResourceType.NOT_EXISTING;
                }
            }
        }
    } catch (final Exception ex) {
        LOG.error("Error determining nature of resource {}", xmldbUri.toString(), ex);
        type = ResourceType.NOT_EXISTING;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Resource type={}", type.toString());
    }
    return type;
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) URISyntaxException(java.net.URISyntaxException) EXistException(org.exist.EXistException)

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