Search in sources :

Example 21 with StringInputSource

use of org.exist.util.StringInputSource in project exist by eXist-db.

the class CopyResourceRecoveryTest method store.

private void store(final String testCollectionName, final String subCollection) 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()))) {
        Collection testCollection;
        DocumentImpl doc;
        try (final Txn transaction = transact.beginTransaction()) {
            final Collection root = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test"));
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            testCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName));
            assertNotNull(testCollection);
            broker.saveCollection(transaction, testCollection);
            final Collection subTestCollection = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("test").append(testCollectionName).append(subCollection));
            assertNotNull(subTestCollection);
            broker.saveCollection(transaction, subTestCollection);
            final String sample;
            try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
                assertNotNull(is);
                sample = InputStreamUtil.readString(is, UTF_8);
            }
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, subTestCollection);
            doc = subTestCollection.getDocument(broker, XmldbURI.create("test.xml"));
            transact.commit(transaction);
        }
        try (final Txn transaction = transact.beginTransaction()) {
            broker.copyResource(transaction, doc, testCollection, XmldbURI.create("new_test.xml"));
            broker.saveCollection(transaction, testCollection);
            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)

Example 22 with StringInputSource

use of org.exist.util.StringInputSource 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 23 with StringInputSource

use of org.exist.util.StringInputSource 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 24 with StringInputSource

use of org.exist.util.StringInputSource 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 25 with StringInputSource

use of org.exist.util.StringInputSource 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)

Aggregations

StringInputSource (org.exist.util.StringInputSource)72 Collection (org.exist.collections.Collection)57 Txn (org.exist.storage.txn.Txn)56 BrokerPool (org.exist.storage.BrokerPool)34 DBBroker (org.exist.storage.DBBroker)34 TransactionManager (org.exist.storage.txn.TransactionManager)27 XmldbURI (org.exist.xmldb.XmldbURI)17 Test (org.junit.Test)13 InputStream (java.io.InputStream)10 DocumentImpl (org.exist.dom.persistent.DocumentImpl)8 Sequence (org.exist.xquery.value.Sequence)7 CollectionConfigurationManager (org.exist.collections.CollectionConfigurationManager)5 EXistException (org.exist.EXistException)4 ManagedCollectionLock (org.exist.storage.lock.ManagedCollectionLock)4 LockException (org.exist.util.LockException)4 InputSource (org.xml.sax.InputSource)4 IOException (java.io.IOException)3 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)3 LockedDocument (org.exist.dom.persistent.LockedDocument)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3