Search in sources :

Example 86 with Txn

use of org.exist.storage.txn.Txn 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 87 with Txn

use of org.exist.storage.txn.Txn 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 88 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class EXistServlet method doPatch.

protected void doPatch(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    // first, adjust the path
    String path = adjustPath(request);
    // second, perform descriptor actions
    final Descriptor descriptor = Descriptor.getDescriptorSingleton();
    if (descriptor != null) {
        // TODO: figure out a way to log PATCH requests with
        // HttpServletRequestWrapper and
        // Descriptor.doLogRequestInReplayLog()
        // map's the path if a mapping is specified in the descriptor
        path = descriptor.mapPath(path);
    }
    // third, authenticate the user
    final Subject user = authenticate(request, response);
    if (user == null) {
        // You now get a HTTP Authentication challenge if there is no user
        return;
    }
    // fourth, process the request
    try (final DBBroker broker = getPool().get(Optional.of(user));
        final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
        final XmldbURI dbpath = XmldbURI.createInternal(path);
        try (final Collection collection = broker.getCollection(dbpath)) {
            if (collection != null) {
                transaction.abort();
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "A PATCH request is not allowed against a plain collection path.");
                return;
            }
        }
        try {
            srvREST.doPatch(broker, transaction, dbpath, request, response);
            transaction.commit();
        } catch (final Throwable t) {
            transaction.abort();
            throw t;
        }
    } catch (final MethodNotAllowedException e) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getMessage());
    } catch (final BadRequestException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage(), e);
        }
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    } catch (final PermissionDeniedException e) {
        // Else return a FORBIDDEN Error
        if (user.equals(getDefaultUser())) {
            getAuthenticator().sendChallenge(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
        }
    } catch (final EXistException e) {
        if (response.isCommitted()) {
            throw new ServletException(e.getMessage(), e);
        }
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (final Throwable e) {
        LOG.error(e);
        throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
    }
}
Also used : MethodNotAllowedException(org.exist.http.MethodNotAllowedException) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) Subject(org.exist.security.Subject) ServletException(javax.servlet.ServletException) DBBroker(org.exist.storage.DBBroker) Descriptor(org.exist.http.Descriptor) Collection(org.exist.collections.Collection) BadRequestException(org.exist.http.BadRequestException) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 89 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class RpcConnection method withDb.

/**
 * Higher-order-function for performing an XMLDB operation on
 * the database
 *
 * @param user        The user to execute the operation as
 * @param dbOperation The operation to perform on the database
 * @param <R>         The return type of the operation
 * @throws EXistException if an internal error occurs
 * @throws PermissionDeniedException If the current user is not allowed to perform this action
 */
private <R> R withDb(final Subject user, final XmlRpcFunction<R> dbOperation) throws EXistException, PermissionDeniedException {
    try (final DBBroker broker = factory.getBrokerPool().get(Optional.of(user));
        final Txn transaction = factory.getBrokerPool().getTransactionManager().beginTransaction()) {
        final R result = dbOperation.apply(broker, transaction);
        transaction.commit();
        return result;
    }
}
Also used : Txn(org.exist.storage.txn.Txn)

Example 90 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class Modification method checkFragmentation.

/**
 * Check if any of the modified documents needs defragmentation.
 *
 * Defragmentation will take place if the number of split pages in the
 * document exceeds the limit defined in the configuration file.
 *
 * @param context current context
 * @param docs document set to check
 * @param splitCount number of page splits
 * @throws EXistException on general errors during defrag
 * @throws LockException in case locking failed
 */
public static void checkFragmentation(XQueryContext context, DocumentSet docs, int splitCount) throws EXistException, LockException {
    final DBBroker broker = context.getBroker();
    final LockManager lockManager = broker.getBrokerPool().getLockManager();
    // if there is no batch update transaction, start a new individual transaction
    try (final Txn transaction = broker.continueOrBeginTransaction()) {
        for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
            final DocumentImpl next = i.next();
            if (next.getSplitCount() > splitCount) {
                try (final ManagedDocumentLock nextLock = lockManager.acquireDocumentWriteLock(next.getURI())) {
                    broker.defragXMLResource(transaction, next);
                }
            }
            broker.checkXMLResourceConsistency(next);
        }
        transaction.commit();
    }
}
Also used : LockManager(org.exist.storage.lock.LockManager) DBBroker(org.exist.storage.DBBroker) ManagedDocumentLock(org.exist.storage.lock.ManagedDocumentLock) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

Txn (org.exist.storage.txn.Txn)358 DBBroker (org.exist.storage.DBBroker)215 BrokerPool (org.exist.storage.BrokerPool)179 Collection (org.exist.collections.Collection)162 TransactionManager (org.exist.storage.txn.TransactionManager)129 Sequence (org.exist.xquery.value.Sequence)84 StringInputSource (org.exist.util.StringInputSource)83 Test (org.junit.Test)80 XmldbURI (org.exist.xmldb.XmldbURI)55 EXistException (org.exist.EXistException)50 PermissionDeniedException (org.exist.security.PermissionDeniedException)38 Source (org.exist.source.Source)37 StringSource (org.exist.source.StringSource)37 DocumentImpl (org.exist.dom.persistent.DocumentImpl)35 InputSource (org.xml.sax.InputSource)33 XQuery (org.exist.xquery.XQuery)32 IOException (java.io.IOException)31 LockedDocument (org.exist.dom.persistent.LockedDocument)28 InputStream (java.io.InputStream)27 Path (java.nio.file.Path)24