Search in sources :

Example 41 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException 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 42 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class ExistCollection 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));
        final Collection collection = broker.openCollection(xmldbUri, LockMode.READ_LOCK)) {
        if (collection == null) {
            LOG.error("Collection for {} cannot be opened for metadata", xmldbUri);
            return;
        }
        // Retrieve some meta data
        permissions = collection.getPermissionsNoLock();
        readAllowed = permissions.validate(subject, Permission.READ);
        writeAllowed = permissions.validate(subject, Permission.WRITE);
        executeAllowed = permissions.validate(subject, Permission.EXECUTE);
        creationTime = collection.getCreated();
        // Collection does not have more information.
        lastModified = creationTime;
        ownerUser = permissions.getOwner().getUsername();
        ownerGroup = permissions.getGroup().getName();
    } catch (final PermissionDeniedException | EXistException pde) {
        LOG.error(pde);
    }
    // Set flag
    isInitialized = true;
}
Also used : DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException)

Example 43 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class ExistCollection method createCollection.

public XmldbURI createCollection(String name) throws PermissionDeniedException, CollectionExistsException, EXistException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Create  '{}' in '{}'", name, xmldbUri);
    }
    XmldbURI newCollection = xmldbUri.append(name);
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(newCollection, LockMode.WRITE_LOCK)) {
        if (collection != null) {
            final String msg = "Collection already exists";
            LOG.debug(msg);
            // XXX: double "abort" is bad thing!!!
            txnManager.abort(txn);
            throw new CollectionExistsException(msg);
        }
        // Create collection
        try (final Collection created = broker.getOrCreateCollection(txn, newCollection)) {
            broker.saveCollection(txn, created);
            broker.flush();
            // Commit change
            txnManager.commit(txn);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Collection created sucessfully");
            }
        }
    } catch (EXistException | PermissionDeniedException e) {
        LOG.error(e);
        throw e;
    } catch (Throwable e) {
        LOG.error(e);
        throw new EXistException(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished creation");
        }
    }
    return newCollection;
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) CollectionExistsException(org.exist.webdav.exceptions.CollectionExistsException)

Example 44 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class ExistCollection method delete.

/*
     * Delete document or collection.
     */
void delete() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Deleting '{}'", xmldbUri);
    }
    final TransactionManager txnManager = brokerPool.getTransactionManager();
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
        final Txn txn = txnManager.beginTransaction();
        final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
        // Open collection if possible, else abort
        if (collection == null) {
            txnManager.abort(txn);
            return;
        }
        // Remove collection
        broker.removeCollection(txn, collection);
        // Commit change
        txnManager.commit(txn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Document deleted sucessfully");
        }
    } catch (EXistException | IOException | PermissionDeniedException | TriggerException e) {
        LOG.error(e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Finished delete");
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 45 with PermissionDeniedException

use of org.exist.security.PermissionDeniedException in project exist by eXist-db.

the class MiltonCollection method createNew.

/* ===============
     * PutableResource
     * =============== */
@Override
public Resource createNew(String newName, InputStream is, Long length, String contentType) throws IOException, ConflictException {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Create '{}' in '{}'", newName, resourceXmldbUri);
    }
    Resource resource = null;
    try {
        // submit
        XmldbURI resourceURI = existCollection.createFile(newName, is, length, contentType);
        resource = new MiltonDocument(host, resourceURI, brokerPool, subject);
    } catch (PermissionDeniedException | CollectionDoesNotExistException | IOException e) {
        LOG.debug(e.getMessage());
        throw new ConflictException(this, "Create New '" + getXmldbUri().append(newName) + "' failed: " + e.getMessage());
    }
    return resource;
}
Also used : CollectionDoesNotExistException(org.exist.webdav.exceptions.CollectionDoesNotExistException) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

PermissionDeniedException (org.exist.security.PermissionDeniedException)182 EXistException (org.exist.EXistException)82 XmldbURI (org.exist.xmldb.XmldbURI)70 IOException (java.io.IOException)58 DocumentImpl (org.exist.dom.persistent.DocumentImpl)48 Collection (org.exist.collections.Collection)44 DBBroker (org.exist.storage.DBBroker)41 Txn (org.exist.storage.txn.Txn)38 LockException (org.exist.util.LockException)35 SAXException (org.xml.sax.SAXException)35 LockedDocument (org.exist.dom.persistent.LockedDocument)31 XPathException (org.exist.xquery.XPathException)31 Permission (org.exist.security.Permission)23 URISyntaxException (java.net.URISyntaxException)22 TriggerException (org.exist.collections.triggers.TriggerException)22 Source (org.exist.source.Source)20 Path (java.nio.file.Path)19 Account (org.exist.security.Account)18 InputSource (org.xml.sax.InputSource)18 Sequence (org.exist.xquery.value.Sequence)17