Search in sources :

Example 26 with DocumentImpl

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

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

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

the class DocumentNameOrId method getResourceByAbsoluteId.

private Sequence getResourceByAbsoluteId(final IntegerValue ivAbsoluteId) throws XPathException, PermissionDeniedException, LockException, IOException {
    final BigInteger absoluteId = ivAbsoluteId.toJavaObject(BigInteger.class);
    final Tuple3<Integer, Integer, Byte> decoded = decodeAbsoluteResourceId(absoluteId);
    final DocumentImpl doc = context.getBroker().getResourceById(decoded._1, decoded._3, decoded._2);
    if (doc != null) {
        try (final ManagedDocumentLock docLock = context.getBroker().getBrokerPool().getLockManager().acquireDocumentReadLock(doc.getURI())) {
            if (doc instanceof BinaryDocument) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final InputStream is = context.getBroker().getBinaryResource(bin);
                return Base64BinaryDocument.getInstance(context, is);
            } else {
                return new NodeProxy(doc);
            }
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : BigInteger(java.math.BigInteger) BinaryDocument(org.exist.dom.persistent.BinaryDocument) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) InputStream(java.io.InputStream) BigInteger(java.math.BigInteger) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 29 with DocumentImpl

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

the class FunDoctype method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final MutableDocumentSet docs = new DefaultDocumentSet();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
        for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
            final String next = j.nextItem().getStringValue();
            try {
                context.getBroker().getXMLResourcesByDoctype(next, docs);
            } catch (final PermissionDeniedException | LockException e) {
                LOG.error(e.getMessage(), e);
                throw new XPathException(this, e);
            }
        }
    }
    final NodeSet result = new ExtArrayNodeSet(1);
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 30 with DocumentImpl

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

the class Modification method finishTriggers.

protected void finishTriggers(Txn transaction) throws TriggerException {
    final Iterator<DocumentImpl> iterator = modifiedDocuments.getDocumentIterator();
    while (iterator.hasNext()) {
        final DocumentImpl doc = iterator.next();
        context.addModifiedDoc(doc);
        finishTrigger(transaction, doc);
    }
    triggers.clear();
}
Also used : DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

DocumentImpl (org.exist.dom.persistent.DocumentImpl)128 PermissionDeniedException (org.exist.security.PermissionDeniedException)51 Collection (org.exist.collections.Collection)40 LockedDocument (org.exist.dom.persistent.LockedDocument)39 Txn (org.exist.storage.txn.Txn)35 XmldbURI (org.exist.xmldb.XmldbURI)34 EXistException (org.exist.EXistException)27 LockException (org.exist.util.LockException)26 DBBroker (org.exist.storage.DBBroker)25 IOException (java.io.IOException)19 NodeProxy (org.exist.dom.persistent.NodeProxy)18 URISyntaxException (java.net.URISyntaxException)17 BrokerPool (org.exist.storage.BrokerPool)17 TransactionManager (org.exist.storage.txn.TransactionManager)17 Test (org.junit.Test)17 XPathException (org.exist.xquery.XPathException)16 NodeId (org.exist.numbering.NodeId)14 SAXException (org.xml.sax.SAXException)13 StoredNode (org.exist.dom.persistent.StoredNode)12 BinaryDocument (org.exist.dom.persistent.BinaryDocument)11