Search in sources :

Example 1 with TransactionException

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

the class RestoreHandler method setDeferredPermissions.

private void setDeferredPermissions() {
    final DeferredPermission deferredPermission = deferredPermissions.pop();
    try (final Txn transaction = beginTransaction()) {
        deferredPermission.apply(broker, transaction);
        transaction.commit();
    } catch (final TransactionException e) {
        final String msg = "ERROR: Failed to set permissions on: '" + deferredPermission.getTarget() + "'.";
        LOG.error(msg, e);
        listener.warn(msg);
    }
}
Also used : TransactionException(org.exist.storage.txn.TransactionException) Txn(org.exist.storage.txn.Txn)

Example 2 with TransactionException

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

the class SystemImportHandler method setDeferredPermissions.

private void setDeferredPermissions() {
    final DeferredPermission deferredPermission = deferredPermissions.pop();
    try (final Txn transaction = beginTransaction()) {
        deferredPermission.apply(broker, transaction);
        transaction.commit();
    } catch (final TransactionException e) {
        final String msg = "ERROR: Failed to set permissions on: '" + deferredPermission.getTarget() + "'.";
        LOG.error(msg, e);
        listener.warn(msg);
    }
}
Also used : TransactionException(org.exist.storage.txn.TransactionException) Txn(org.exist.storage.txn.Txn)

Example 3 with TransactionException

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

the class RestoreHandler method restoreCollectionEntry.

private DeferredPermission restoreCollectionEntry(final Attributes atts) throws SAXException {
    final String name = atts.getValue("name");
    if (name == null) {
        throw new SAXException("Collection requires a name attribute");
    }
    final String owner = getAttr(atts, "owner", SecurityManager.SYSTEM);
    final String group = getAttr(atts, "group", SecurityManager.DBA_GROUP);
    final String mode = getAttr(atts, "mode", "644");
    final String created = atts.getValue("created");
    final String strVersion = atts.getValue("version");
    if (strVersion != null) {
        try {
            this.version = Integer.parseInt(strVersion);
        } catch (final NumberFormatException nfe) {
            final String msg = "Could not parse version number for Collection '" + name + "', defaulting to version 0";
            listener.warn(msg);
            LOG.warn(msg);
            this.version = 0;
        }
    }
    try {
        listener.createdCollection(name);
        final XmldbURI collUri;
        if (version >= STRICT_URI_VERSION) {
            collUri = XmldbURI.create(name);
        } else {
            try {
                collUri = URIUtils.encodeXmldbUriFor(name);
            } catch (final URISyntaxException e) {
                listener.warn("Could not parse document name into a URI: " + e.getMessage());
                return new SkippedEntryDeferredPermission();
            }
        }
        if (version >= BLOB_STORE_VERSION) {
            this.deduplicateBlobs = Boolean.parseBoolean(atts.getValue("deduplicate-blobs"));
        } else {
            this.deduplicateBlobs = false;
        }
        final LockManager lockManager = broker.getBrokerPool().getLockManager();
        try (final Txn transaction = beginTransaction();
            final ManagedCollectionLock colLock = lockManager.acquireCollectionWriteLock(collUri)) {
            Collection collection = broker.getCollection(collUri);
            if (collection == null) {
                final Tuple2<Permission, Long> creationAttributes = Tuple(null, getDateFromXSDateTimeStringForItem(created, name).getTime());
                collection = broker.getOrCreateCollection(transaction, collUri, Optional.of(creationAttributes));
                broker.saveCollection(transaction, collection);
            }
            transaction.commit();
            this.currentCollectionUri = collection.getURI();
        }
        final DeferredPermission deferredPermission;
        if (name.startsWith(XmldbURI.SYSTEM_COLLECTION)) {
            // prevents restore of a backup from changing System collection ownership
            deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, SecurityManager.SYSTEM, SecurityManager.DBA_GROUP, Integer.parseInt(mode, 8));
        } else {
            deferredPermission = new CollectionDeferredPermission(listener, currentCollectionUri, owner, group, Integer.parseInt(mode, 8));
        }
        return deferredPermission;
    } catch (final IOException | LockException | TransactionException | PermissionDeniedException e) {
        final String msg = "An unrecoverable error occurred while restoring collection '" + name + "': " + e.getMessage() + ". Aborting restore!";
        LOG.error(msg, e);
        listener.warn(msg);
        throw new SAXException(msg, e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) Txn(org.exist.storage.txn.Txn) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) LockManager(org.exist.storage.lock.LockManager) TransactionException(org.exist.storage.txn.TransactionException) ACLPermission(org.exist.security.ACLPermission) Permission(org.exist.security.Permission) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 4 with TransactionException

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

the class ElementImpl method insertBefore.

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {
    if (refChild == null) {
        return appendChild(newChild);
    } else if (!(refChild instanceof IStoredNode)) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Wrong node type");
    }
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        insertBefore(transaction, nl, refChild);
        broker.storeXMLResource(transaction, getOwnerDocument());
        transact.commit(transaction);
        return refChild.getPreviousSibling();
    } catch (final TransactionException e) {
        throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, e.getMessage());
    } catch (final EXistException e) {
        LOG.warn("Exception while inserting node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : org.w3c.dom(org.w3c.dom) TransactionException(org.exist.storage.txn.TransactionException) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 5 with TransactionException

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

the class BinaryDoc method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final Sequence emptyParamReturnValue = (isCalledAs(FS_BINARY_DOC_NAME) || isCalledAs(FS_BINARY_DOC_CONTENT_DIGEST_NAME)) ? Sequence.EMPTY_SEQUENCE : BooleanValue.FALSE;
    if (args[0].isEmpty()) {
        return emptyParamReturnValue;
    }
    final String path = args[0].getStringValue();
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
        if (lockedDoc == null) {
            return emptyParamReturnValue;
        }
        final DocumentImpl doc = lockedDoc.getDocument();
        if (doc.getResourceType() != DocumentImpl.BINARY_FILE) {
            return emptyParamReturnValue;
        } else if (isCalledAs(FS_BINARY_DOC_NAME)) {
            try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final InputStream is = context.getBroker().getBinaryResource(transaction, bin);
                final Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
                b64doc.setUrl(path);
                transaction.commit();
                return b64doc;
            }
        } else if (isCalledAs(FS_BINARY_DOC_CONTENT_DIGEST_NAME)) {
            final String algorithm = args[1].getStringValue();
            final DigestType digestType;
            try {
                digestType = DigestType.forCommonName(algorithm);
            } catch (final IllegalArgumentException e) {
                throw new XPathException(this, "Invalid algorithm: " + algorithm, e);
            }
            try (final Txn transaction = context.getBroker().getBrokerPool().getTransactionManager().beginTransaction()) {
                final BinaryDocument bin = (BinaryDocument) doc;
                final MessageDigest messageDigest = context.getBroker().getBinaryResourceContentDigest(transaction, bin, digestType);
                final InputStream is = new UnsynchronizedByteArrayInputStream(messageDigest.getValue());
                final Sequence result = BinaryValueFromInputStream.getInstance(context, new HexBinaryValueType(), is);
                transaction.commit();
                return result;
            }
        } else {
            return BooleanValue.TRUE;
        }
    } catch (final URISyntaxException e) {
        logger.error("Invalid resource URI", e);
        throw new XPathException(this, "Invalid resource uri", e);
    } catch (final PermissionDeniedException e) {
        logger.error("{}: permission denied to read resource", path, e);
        throw new XPathException(this, path + ": permission denied to read resource");
    } catch (final IOException | TransactionException e) {
        logger.error("{}: I/O error while reading resource", path, e);
        throw new XPathException(this, path + ": I/O error while reading resource", e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) BinaryDocument(org.exist.dom.persistent.BinaryDocument) TransactionException(org.exist.storage.txn.TransactionException) DigestType(org.exist.util.crypto.digest.DigestType) LockedDocument(org.exist.dom.persistent.LockedDocument) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) MessageDigest(org.exist.util.crypto.digest.MessageDigest)

Aggregations

TransactionException (org.exist.storage.txn.TransactionException)13 Txn (org.exist.storage.txn.Txn)13 IOException (java.io.IOException)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 XmldbURI (org.exist.xmldb.XmldbURI)5 URISyntaxException (java.net.URISyntaxException)4 BinaryDocument (org.exist.dom.persistent.BinaryDocument)4 LockedDocument (org.exist.dom.persistent.LockedDocument)4 XPathException (org.exist.xquery.XPathException)4 URI (java.net.URI)3 DocumentImpl (org.exist.dom.persistent.DocumentImpl)3 InputStream (java.io.InputStream)2 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Collection (org.exist.collections.Collection)2 Permission (org.exist.security.Permission)2 BrokerPool (org.exist.storage.BrokerPool)2 SAXException (org.xml.sax.SAXException)2 TaggedTryUnchecked (com.evolvedbinary.j8fu.Try.TaggedTryUnchecked)1