Search in sources :

Example 31 with LockedDocument

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

the class CopyResourceTest method copyDoc.

private void copyDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI srcDocName, final XmldbURI destDocName) throws EXistException, PermissionDeniedException, LockException, IOException, TriggerException {
    final XmldbURI src = TEST_COLLECTION_URI.append(srcDocName);
    final XmldbURI dest = TEST_COLLECTION_URI.append(destDocName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final LockedDocument lockedSrcDoc = broker.getXMLResource(src, LockMode.READ_LOCK);
        final Collection destCol = broker.openCollection(dest.removeLastSegment(), LockMode.WRITE_LOCK)) {
        broker.copyResource(transaction, lockedSrcDoc.getDocument(), destCol, dest.lastSegment(), preserve);
        transaction.commit();
    }
    // check the copy of the document is the same as the original
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedOriginal = broker.getXMLResource(src, LockMode.READ_LOCK);
        final LockedDocument lockedCopy = broker.getXMLResource(dest, LockMode.READ_LOCK)) {
        final Diff diff = DiffBuilder.compare(Input.fromDocument(lockedOriginal.getDocument())).withTest(Input.fromDocument(lockedCopy.getDocument())).build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI)

Example 32 with LockedDocument

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

the class StoreResourceTest method replaceXmlDoc.

private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            final String docXml = serializer.serialize(lockedDoc.getDocument());
            final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
        } finally {
            broker.returnSerializer(serializer);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Diff(org.xmlunit.diff.Diff) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) XmldbURI(org.exist.xmldb.XmldbURI) Serializer(org.exist.storage.serializers.Serializer)

Example 33 with LockedDocument

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

the class StoreResourceTest method replaceBinDoc.

private void replaceBinDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
    final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
    final BrokerPool pool = existWebServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content.getBytes(UTF_8)), MimeType.BINARY_TYPE, col);
        transaction.commit();
    }
    // check the replaced document is correct
    try (final DBBroker broker = pool.get(Optional.of(execAsUser));
        final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK);
        final InputStream is = broker.getBinaryResource((BinaryDocument) lockedDoc.getDocument());
        final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
        os.write(is);
        assertArrayEquals(content.getBytes(UTF_8), os.toByteArray());
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputStream(java.io.InputStream) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) XmldbURI(org.exist.xmldb.XmldbURI)

Example 34 with LockedDocument

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

the class AbstractRecoverTest method read.

/**
 * Read a document from the database.
 *
 * @param shouldExist true if the document should exist in the database, false if the document should not exist
 * @param data The data that was previously stored
 * @param dbFilename The name of the file to read from the database
 */
protected void read(final boolean shouldExist, final InputSource data, final String dbFilename) throws EXistException, PermissionDeniedException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XmldbURI uri = TestConstants.TEST_COLLECTION_URI.append(dbFilename);
        try (final LockedDocument doc = broker.getXMLResource(uri, LockMode.READ_LOCK)) {
            if (!shouldExist) {
                assertNull("Document should not exist in the database: " + uri, doc);
            } else {
                assertNotNull("Document does not exist in the database: " + uri, doc);
                readAndVerify(broker, doc.getDocument(), data, dbFilename);
            }
        }
    }
}
Also used : LockedDocument(org.exist.dom.persistent.LockedDocument) XmldbURI(org.exist.xmldb.XmldbURI)

Example 35 with LockedDocument

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

the class MetadataFunctions method extractMetadataFromLocalResource.

private Sequence extractMetadataFromLocalResource(final XmldbURI docUri) throws XPathException {
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(docUri, LockMode.READ_LOCK)) {
        if (lockedDoc != null && lockedDoc.getDocument() instanceof BinaryDocument) {
            final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
            final BrokerPool pool = context.getBroker().getBrokerPool();
            final BlobStore blobStore = pool.getBlobStore();
            try (final Txn transaction = pool.getTransactionManager().beginTransaction()) {
                final Sequence result = blobStore.with(transaction, binDoc.getBlobId(), blobFile -> TaggedTryUnchecked(XPathException.class, () -> exifToolExtract(blobFile))).get();
                transaction.commit();
                return result;
            }
        } else {
            throw new XPathException(this, "The binary document at " + docUri.toString() + " cannot be found.");
        }
    } catch (PermissionDeniedException | IOException | TransactionException e) {
        throw new XPathException(this, "Could not access binary document: " + e.getMessage(), e);
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) LockMode(org.exist.storage.lock.Lock.LockMode) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) SequenceType(org.exist.xquery.value.SequenceType) FunctionParameterSequenceType(org.exist.xquery.value.FunctionParameterSequenceType) QName(org.exist.dom.QName) URISyntaxException(java.net.URISyntaxException) FunctionSignature(org.exist.xquery.FunctionSignature) PermissionDeniedException(org.exist.security.PermissionDeniedException) Cardinality(org.exist.xquery.Cardinality) BlobStore(org.exist.storage.blob.BlobStore) BasicFunction(org.exist.xquery.BasicFunction) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Source(org.exist.source.Source) FunctionReturnSequenceType(org.exist.xquery.value.FunctionReturnSequenceType) XmldbURI(org.exist.xmldb.XmldbURI) URI(java.net.URI) Path(java.nio.file.Path) XQueryContext(org.exist.xquery.XQueryContext) OutputStream(java.io.OutputStream) InputSource(org.xml.sax.InputSource) LockedDocument(org.exist.dom.persistent.LockedDocument) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException) Logger(org.apache.logging.log4j.Logger) TaggedTryUnchecked(com.evolvedbinary.j8fu.Try.TaggedTryUnchecked) ModuleUtils(org.exist.xquery.modules.ModuleUtils) SAXException(org.xml.sax.SAXException) SourceFactory(org.exist.source.SourceFactory) Sequence(org.exist.xquery.value.Sequence) LogManager(org.apache.logging.log4j.LogManager) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) TransactionException(org.exist.storage.txn.TransactionException) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) BrokerPool(org.exist.storage.BrokerPool) BlobStore(org.exist.storage.blob.BlobStore)

Aggregations

LockedDocument (org.exist.dom.persistent.LockedDocument)91 DocumentImpl (org.exist.dom.persistent.DocumentImpl)40 XmldbURI (org.exist.xmldb.XmldbURI)39 DBBroker (org.exist.storage.DBBroker)36 PermissionDeniedException (org.exist.security.PermissionDeniedException)30 Collection (org.exist.collections.Collection)28 Txn (org.exist.storage.txn.Txn)27 BrokerPool (org.exist.storage.BrokerPool)21 EXistException (org.exist.EXistException)20 Test (org.junit.Test)20 IOException (java.io.IOException)18 BinaryDocument (org.exist.dom.persistent.BinaryDocument)18 Serializer (org.exist.storage.serializers.Serializer)15 XPathException (org.exist.xquery.XPathException)14 URISyntaxException (java.net.URISyntaxException)13 InputStream (java.io.InputStream)11 LockException (org.exist.util.LockException)11 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)9 Lock (org.exist.storage.lock.Lock)9 TransactionManager (org.exist.storage.txn.TransactionManager)9