Search in sources :

Example 56 with Txn

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

the class CollectionRemovalTest method initDB.

@Before
public void initDB() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final int worldReadable = 0744;
        final int worldForbidden = 0700;
        /*
             * Creates 3 collections: /db/test, /db/test/test2, /db/test/test2/test3 and /db/test/test2/test4,
             * and stores one document into each.
             * Collection /db/test/test2/test3 is only readable by the owner (i.e. admin user).
             */
        final List<Tuple2<XmldbURI, Integer>> collectionUriAndModes = Arrays.asList(Tuple(TestConstants.TEST_COLLECTION_URI2, worldReadable), Tuple(TestConstants.TEST_COLLECTION_URI3, worldForbidden), Tuple(TestConstants.TEST_COLLECTION_URI2.append("test4"), worldReadable));
        // creat collections
        for (final Tuple2<XmldbURI, Integer> collectionUriAndMode : collectionUriAndModes) {
            final XmldbURI collectionUri = collectionUriAndMode._1;
            final int mode = collectionUriAndMode._2;
            // create collection
            final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
            assertNotNull(collection);
            final Permission perms = collection.getPermissions();
            perms.setMode(mode);
            broker.saveCollection(transaction, collection);
            // store document
            broker.storeDocument(transaction, XmldbURI.create("document.xml"), new StringInputSource(DATA), MimeType.XML_TYPE, collection);
        }
        transact.commit(transaction);
    }
}
Also used : Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) Permission(org.exist.security.Permission) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 57 with Txn

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

the class HistoryTriggerTest method setup.

@Before
public void setup() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        // create and store the collection.xconf for the test collection
        Collection configCollection = broker.getOrCreateCollection(transaction, TEST_CONFIG_COLLECTION_URI);
        broker.saveCollection(transaction, configCollection);
        broker.storeDocument(transaction, CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE_URI, new StringInputSource(COLLECTION_CONFIG), MimeType.XML_TYPE, configCollection);
        // create the test collection
        Collection testCollection = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
        broker.saveCollection(transaction, testCollection);
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 58 with Txn

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

the class HistoryTriggerTest method checkHistoryOfOriginal.

private void checkHistoryOfOriginal(final BrokerPool brokerPool, final XmldbURI originalDocName, final String orginalDocContent) throws EXistException, PermissionDeniedException, LockException {
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection historyCollection = broker.openCollection(HistoryTrigger.DEFAULT_ROOT_PATH.append(TEST_COLLECTION_URI).append(originalDocName), Lock.LockMode.READ_LOCK)) {
            assertNotNull(historyCollection);
            final DocumentSet documentSet = historyCollection.getDocuments(broker, new DefaultDocumentSet());
            assertEquals(1, documentSet.getDocumentCount());
            final Iterator<DocumentImpl> it = documentSet.getDocumentIterator();
            assertTrue(it.hasNext());
            final DocumentImpl doc = it.next();
            final Diff diff = DiffBuilder.compare(Input.from(orginalDocContent)).withTest(Input.from(doc)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
            assertFalse(it.hasNext());
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) Diff(org.xmlunit.diff.Diff) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 59 with Txn

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

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

the class AbstractXMLReaderSecurityTest method removeTestData.

@After
public void removeTestData() throws EXistException, PermissionDeniedException, IOException, TriggerException {
    final BrokerPool brokerPool = getExistEmbeddedServer().getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        final Collection testCollection = broker.getCollection(TEST_COLLECTION);
        if (testCollection != null && !broker.removeCollection(transaction, testCollection)) {
            transaction.abort();
            fail("Unable to remove test collection");
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

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