Search in sources :

Example 16 with LockedDocument

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

the class CollectionRemovalTest method retrieveDoc.

private void retrieveDoc(final XmldbURI uri) throws EXistException, PermissionDeniedException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Collection test = broker.openCollection(uri, LockMode.READ_LOCK)) {
        assertNotNull(test);
        try (final LockedDocument lockedDoc = test.getDocumentWithLock(broker, XmldbURI.createInternal("document.xml"), LockMode.READ_LOCK)) {
            assertNotNull(lockedDoc);
            final Serializer serializer = broker.borrowSerializer();
            try {
                String xml = serializer.serialize(lockedDoc.getDocument());
            } finally {
                broker.returnSerializer(serializer);
            }
            // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
            test.close();
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 17 with LockedDocument

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

the class XMLReaderSecurityTest method cannotExpandExternalEntitiesWhenDisabled.

@Test
public void cannotExpandExternalEntitiesWhenDisabled() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    // create a temporary file on disk that contains secret info
    final Tuple2<String, Path> secret = createTempSecretFile();
    final XmldbURI docName = XmldbURI.create("expand-secret.xml");
    // attempt to store a document with an external entity which would be expanded to the content of the secret file
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
            // debugReader("cannotExpandExternalEntitiesWhenDisabled", broker, testCollection);
            final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
            broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
        }
        transaction.commit();
    }
    // read back the document, to confirm that it does not contain the secret
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
            try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
                // release the collection lock early inline with asymmetrical locking
                testCollection.close();
                assertNotNull(testDoc);
                final String expected = EXPECTED_EXPANSION_DISABLED_DOC;
                final String actual = serialize(testDoc.getDocument());
                assertEquals(expected, actual);
            }
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 18 with LockedDocument

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

the class XMLReaderExpansionTest method expandExternalEntities.

@Test
public void expandExternalEntities() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final Map<String, Boolean> parserConfig = new HashMap<>();
    parserConfig.put(FEATURE_EXTERNAL_GENERAL_ENTITIES, true);
    brokerPool.getConfiguration().setProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY, parserConfig);
    // create a temporary file on disk that contains secret info
    final Tuple2<String, Path> secret = createTempSecretFile();
    final XmldbURI docName = XmldbURI.create("expand-secret.xml");
    // attempt to store a document with an external entity which would be expanded to the content of the secret file
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.WRITE_LOCK)) {
            // debugReader("expandExternalEntities", broker, testCollection);
            final String docContent = EXPANSION_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._2.toUri().toString());
            broker.storeDocument(transaction, docName, new StringInputSource(docContent), MimeType.XML_TYPE, testCollection);
        }
        transaction.commit();
    }
    // read back the document, to confirm that it does contain the secret
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection testCollection = broker.openCollection(TEST_COLLECTION, Lock.LockMode.READ_LOCK)) {
            try (final LockedDocument testDoc = testCollection.getDocumentWithLock(broker, docName, Lock.LockMode.READ_LOCK)) {
                // release the collection lock early inline with asymmetrical locking
                testCollection.close();
                assertNotNull(testDoc);
                final String expected = EXPECTED_EXPANDED_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._1);
                final String actual = serialize(testDoc.getDocument());
                assertEquals(expected, actual);
            }
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 19 with LockedDocument

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

the class RemoveIndex method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // Get first parameter, this is the document
    final String path = args[0].itemAt(0).getStringValue();
    // Retrieve document from database
    try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
        // Verify the document actually exists
        if (lockedDoc == null) {
            throw new XPathException("Document " + path + " does not exist.");
        }
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        // Note: code order is important here,
        index.setDocument(lockedDoc.getDocument(), ReindexMode.REMOVE_BINARY);
        index.flush();
    } catch (Exception ex) {
        // PermissionDeniedException
        throw new XPathException(ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 20 with LockedDocument

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

the class Index method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        if (isCalledAs("index")) {
            // Get first parameter, this is the document
            String path = args[0].itemAt(0).getStringValue();
            // Retrieve document from database
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
                // Verify the document actually exists
                final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                if (doc == null) {
                    throw new XPathException(this, "Document " + path + " does not exist.");
                }
                boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
                // Note: code order is important here,
                index.setDocument(doc, ReindexMode.STORE);
                index.setMode(ReindexMode.STORE);
                // Get 'solr' node from second parameter
                NodeValue descriptor = (NodeValue) args[1].itemAt(0);
                // Pas document and index instructions to indexer
                index.indexNonXML(descriptor);
                if (flush) {
                    // Make sure things are written
                    index.writeNonXML();
                }
            }
        } else {
            // "close"
            index.writeNonXML();
        }
    } catch (Exception ex) {
        // PermissionDeniedException
        logger.error(ex.getMessage(), ex);
        throw new XPathException(this, ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

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