Search in sources :

Example 66 with Collection

use of org.exist.collections.Collection 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 67 with Collection

use of org.exist.collections.Collection 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 68 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class StoreResourceTest method setup.

@Before
public void setup() throws EXistException, PermissionDeniedException, LockException, SAXException, IOException, AuthenticationException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    // create user1 resources
    final Subject user1 = pool.getSecurityManager().authenticate(USER1_NAME, USER1_PWD);
    try (final DBBroker broker = pool.get(Optional.of(user1));
        final Txn transaction = pool.getTransactionManager().beginTransaction();
        final Collection collection = broker.openCollection(TEST_COLLECTION_URI, Lock.LockMode.WRITE_LOCK)) {
        final String u1d3xml = "<empty3/>";
        broker.storeDocument(transaction, USER1_DOC1, new StringInputSource(u1d3xml), MimeType.XML_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC1), USER1_DOC1_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_DOC1), GROUP1_NAME);
        final String u1d3bin = "bin3";
        broker.storeDocument(transaction, USER1_BIN_DOC1, new StringInputSource(u1d3bin.getBytes(UTF_8)), MimeType.TEXT_TYPE, collection);
        chmod(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC1), USER1_BIN_DOC1_MODE);
        chgrp(broker, transaction, TEST_COLLECTION_URI.append(USER1_BIN_DOC1), GROUP1_NAME);
        transaction.commit();
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Example 69 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class MoveResourceRecoveryTest method storeAborted.

private void storeAborted() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
            assertNotNull(test2);
            broker.saveCollection(transaction, test2);
            final String sample;
            try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
                sample = InputStreamUtil.readString(is, UTF_8);
            }
            broker.storeDocument(transaction, XmldbURI.create("new_test2.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
            transact.commit(transaction);
        }
        final Txn transaction = transact.beginTransaction();
        final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
        final DocumentImpl doc = test2.getDocument(broker, XmldbURI.create("new_test2.xml"));
        assertNotNull(doc);
        final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        broker.moveResource(transaction, doc, test, XmldbURI.create("new_test2.xml"));
        broker.saveCollection(transaction, test);
        // NOTE: do not commit the transaction
        pool.getJournalManager().get().flush(true, false);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 70 with Collection

use of org.exist.collections.Collection in project exist by eXist-db.

the class MoveResourceRecoveryTest method store.

private void store() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, URISyntaxException {
    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 Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(test);
        broker.saveCollection(transaction, test);
        final Collection test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI2);
        assertNotNull(test2);
        broker.saveCollection(transaction, test2);
        final String sample;
        try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
            sample = InputStreamUtil.readString(is, UTF_8);
        }
        broker.storeDocument(transaction, TestConstants.TEST_XML_URI, new StringInputSource(sample), MimeType.XML_TYPE, test2);
        final DocumentImpl doc = test2.getDocument(broker, TestConstants.TEST_XML_URI);
        assertNotNull(doc);
        broker.moveResource(transaction, doc, test, XmldbURI.create("new_test.xml"));
        broker.saveCollection(transaction, test);
        transact.commit(transaction);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) InputStream(java.io.InputStream) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

Collection (org.exist.collections.Collection)297 Txn (org.exist.storage.txn.Txn)160 XmldbURI (org.exist.xmldb.XmldbURI)99 DBBroker (org.exist.storage.DBBroker)89 TransactionManager (org.exist.storage.txn.TransactionManager)86 BrokerPool (org.exist.storage.BrokerPool)69 StringInputSource (org.exist.util.StringInputSource)57 Test (org.junit.Test)57 EXistException (org.exist.EXistException)43 PermissionDeniedException (org.exist.security.PermissionDeniedException)43 DocumentImpl (org.exist.dom.persistent.DocumentImpl)42 IOException (java.io.IOException)33 LockedDocument (org.exist.dom.persistent.LockedDocument)31 SAXException (org.xml.sax.SAXException)26 InputStream (java.io.InputStream)19 Path (java.nio.file.Path)19 Permission (org.exist.security.Permission)19 LockException (org.exist.util.LockException)16 TriggerException (org.exist.collections.triggers.TriggerException)15 BinaryDocument (org.exist.dom.persistent.BinaryDocument)15