Search in sources :

Example 1 with Txn

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

the class MutableCollection method storeDocument.

@Override
public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, @Nullable MimeType mimeType, @Nullable final Date createdDate, @Nullable final Date lastModifiedDate, @Nullable final Permission permission, @Nullable final DocumentType documentType, @Nullable final XMLReader xmlReader) throws EXistException, PermissionDeniedException, SAXException, LockException, IOException {
    if (mimeType == null) {
        mimeType = MimeType.BINARY_TYPE;
    }
    if (mimeType.isXMLType()) {
        // Store XML Document
        final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> validatorFn = (xmlReader1, validateIndexInfo) -> {
            validateIndexInfo.setReader(xmlReader1, null);
            try {
                xmlReader1.parse(source);
            } catch (final SAXException e) {
                throw new SAXException("The XML parser reported a problem: " + e.getMessage(), e);
            } catch (final IOException e) {
                throw new EXistException(e);
            }
        };
        final BiConsumer2E<XMLReader, IndexInfo, SAXException, EXistException> parserFn = (xmlReader1, storeIndexInfo) -> {
            try {
                storeIndexInfo.setReader(xmlReader1, null);
                xmlReader1.parse(source);
            } catch (final IOException e) {
                throw new EXistException(e);
            }
        };
        storeXmlDocument(transaction, broker, name, mimeType, createdDate, lastModifiedDate, permission, documentType, xmlReader, validatorFn, parserFn);
    } else {
        // Store Binary Document
        try (final InputStream is = source.getByteStream()) {
            if (is == null) {
                throw new IOException("storeDocument received a null InputStream when trying to store a Binary Document");
            }
            addBinaryResource(transaction, broker, name, is, mimeType.getName(), -1, createdDate, lastModifiedDate, permission);
        }
    }
}
Also used : CloseShieldReader(org.apache.commons.io.input.CloseShieldReader) java.util(java.util) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) Consumer2E(com.evolvedbinary.j8fu.function.Consumer2E) QName(org.exist.dom.QName) PermissionDeniedException(org.exist.security.PermissionDeniedException) org.exist.dom.persistent(org.exist.dom.persistent) Constants(org.exist.xquery.Constants) VariableByteOutputStream(org.exist.storage.io.VariableByteOutputStream) VALIDATION_SETTING(org.exist.util.XMLReaderObjectFactory.VALIDATION_SETTING) MimeType(org.exist.util.MimeType) Account(org.exist.security.Account) XMLReader(org.xml.sax.XMLReader) org.exist.storage(org.exist.storage) IndexController(org.exist.indexing.IndexController) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) LockException(org.exist.util.LockException) Subject(org.exist.security.Subject) VariableByteInput(org.exist.storage.io.VariableByteInput) Node(org.w3c.dom.Node) XmldbURI(org.exist.xmldb.XmldbURI) LockType(org.exist.storage.lock.Lock.LockType) BiConsumer2E(com.evolvedbinary.j8fu.function.BiConsumer2E) EXistException(org.exist.EXistException) Indexer(org.exist.Indexer) Permission(org.exist.security.Permission) Nullable(javax.annotation.Nullable) PermissionFactory(org.exist.security.PermissionFactory) InputSource(org.xml.sax.InputSource) Database(org.exist.Database) XMLReaderObjectFactory(org.exist.util.XMLReaderObjectFactory) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) org.exist.storage.lock(org.exist.storage.lock) DOMStreamer(org.exist.util.serializer.DOMStreamer) Sync(org.exist.storage.sync.Sync) NotThreadSafe(net.jcip.annotations.NotThreadSafe) org.exist.collections.triggers(org.exist.collections.triggers) DocumentType(org.w3c.dom.DocumentType) StreamListener(org.exist.indexing.StreamListener) Logger(org.apache.logging.log4j.Logger) java.io(java.io) SAXException(org.xml.sax.SAXException) GuardedBy(net.jcip.annotations.GuardedBy) Configuration(org.exist.util.Configuration) LogManager(org.apache.logging.log4j.LogManager) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) EXistException(org.exist.EXistException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 2 with Txn

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

the class CollectionConfigurationManager method checkRootCollectionConfig.

/**
 * Create a stored default configuration document for the root collection
 *
 * @param broker The broker which will do the operation
 * @throws EXistException if something goes wrong
 * @throws PermissionDeniedException if user does not have sufficient rights
 */
public void checkRootCollectionConfig(DBBroker broker) throws EXistException, PermissionDeniedException {
    // Copied from the legacy conf.xml in order to make the test suite work
    // TODO : backward compatibility could be ensured by copying the
    // relevant parts of conf.xml
    final String configuration = "<collection xmlns=\"http://exist-db.org/collection-config/1.0\">" + "    <index>" + "    </index>" + "</collection>";
    final TransactionManager transact = broker.getDatabase().getTransactionManager();
    try (final Txn txn = transact.beginTransaction()) {
        try (final Collection collection = broker.openCollection(XmldbURI.ROOT_COLLECTION_URI, LockMode.READ_LOCK)) {
            if (collection == null) {
                transact.abort(txn);
                throw new EXistException("collection " + XmldbURI.ROOT_COLLECTION_URI + " not found!");
            }
            final CollectionConfiguration conf = getConfiguration(collection);
            if (conf != null) {
                // it
                if (conf.getDocName() != null) {
                    transact.abort(txn);
                    return;
                }
            }
            // Configure the root collection
            addConfiguration(txn, broker, collection, configuration);
            LOG.info("Configured '{}'", collection.getURI());
        }
        transact.commit(txn);
    } catch (final CollectionConfigurationException e) {
        throw new EXistException(e.getMessage());
    }
}
Also used : TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException)

Example 3 with Txn

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

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

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

the class SystemImportHandler method mkcol.

private Collection mkcol(final XmldbURI collPath, final Date created) throws SAXException {
    try (final Txn transaction = beginTransaction()) {
        final Tuple2<Permission, Long> creationAttributes = Tuple(null, created.getTime());
        final Collection col = broker.getOrCreateCollection(transaction, collPath, Optional.of(creationAttributes));
        transaction.commit();
        return col;
    } catch (final Exception e) {
        throw new SAXException(e);
    }
}
Also used : Permission(org.exist.security.Permission) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) XPathException(org.exist.xquery.XPathException) SAXException(org.xml.sax.SAXException)

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