Search in sources :

Example 51 with StringInputSource

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

the class ConstructedNodesRecoveryTest method storeTestDocument.

private void storeTestDocument(final DBBroker broker, final TransactionManager transact, final String documentName) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
    // create a transaction
    try (final Txn transaction = transact.beginTransaction()) {
        // get the test collection
        final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
        // store test document
        broker.storeDocument(transaction, XmldbURI.create(documentName), new StringInputSource(testDocument), MimeType.XML_TYPE, root);
        // commit the transaction
        transact.commit(transaction);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn)

Example 52 with StringInputSource

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

the class EmbeddedBinariesTest method storeBinaryFile.

@Override
protected void storeBinaryFile(final XmldbURI filePath, final byte[] content) throws Exception {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final ManagedCollectionLock collectionLock = brokerPool.getLockManager().acquireCollectionWriteLock(filePath.removeLastSegment())) {
            final Collection collection = broker.getOrCreateCollection(transaction, filePath.removeLastSegment());
            broker.storeDocument(transaction, filePath.lastSegment(), new StringInputSource(content), MimeType.BINARY_TYPE, collection);
            broker.saveCollection(transaction, collection);
        }
        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) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock)

Example 53 with StringInputSource

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

the class ExampleTrigger method afterCreateDocument.

@Override
public void afterCreateDocument(DBBroker broker, Txn txn, DocumentImpl document) throws TriggerException {
    if (document.getFileURI().toString().contains("copied")) {
        LOG.info("Prevent recreation of document {}", document.getFileURI().toString());
        return;
    }
    LOG.info("Created document {}", document.getDocumentURI());
    final byte[] data = "<a>dummy data</a>".getBytes();
    final XmldbURI newDocumentURI = XmldbURI.create(document.getFileURI().toString() + "-copied.xml");
    try (final Collection collection = broker.openCollection(document.getCollection().getURI(), Lock.LockMode.WRITE_LOCK)) {
        // Stream into database
        broker.storeDocument(txn, newDocumentURI, new StringInputSource(data), MimeType.XML_TYPE, collection);
    } catch (Exception e) {
        LOG.error(e);
        throw new TriggerException(e);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) TriggerException(org.exist.collections.triggers.TriggerException) XmldbURI(org.exist.xmldb.XmldbURI) TriggerException(org.exist.collections.triggers.TriggerException)

Example 54 with StringInputSource

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

the class FnDocSecurityTest method createDocument.

private static void createDocument(final DBBroker broker, final Txn transaction, final String collectionUri, final String docName, final String content, final String modeStr) throws PermissionDeniedException, LockException, SAXException, EXistException, IOException, SyntaxException {
    try (final Collection collection = broker.openCollection(XmldbURI.create(collectionUri), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(content), MimeType.XML_TYPE, collection);
        PermissionFactory.chmod_str(broker, transaction, XmldbURI.create(collectionUri).append(docName), Optional.of(modeStr), Optional.empty());
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection)

Example 55 with StringInputSource

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

the class DirtyShutdownTest method storeRepeatedly.

public void storeRepeatedly() {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        Collection root;
        try (final Txn transaction = transact.beginTransaction()) {
            root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            assertNotNull(root);
            broker.saveCollection(transaction, root);
            transact.commit(transaction);
        }
        final String data;
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream();
            final InputStream is = SAMPLES.getMacbethSample()) {
            os.write(is);
            data = new String(os.toByteArray(), UTF_8);
        }
        for (int i = 0; i < 50; i++) {
            try (final Txn transaction = transact.beginTransaction()) {
                broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(data), MimeType.XML_TYPE, root);
                transact.commit(transaction);
            }
        }
    } catch (final PermissionDeniedException | EXistException | SAXException | LockException | IOException e) {
        LOG.error(e.getMessage(), e);
        fail(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) SAXException(org.xml.sax.SAXException) StringInputSource(org.exist.util.StringInputSource) LockException(org.exist.util.LockException) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException)

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