Search in sources :

Example 41 with StringInputSource

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

the class MatchListenerTest method configureAndStore.

private void configureAndStore(String config, String xml) throws PermissionDeniedException, IOException, SAXException, EXistException, LockException, CollectionConfigurationException {
    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 root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        final CollectionConfigurationManager mgr = pool.getConfigurationManager();
        mgr.addConfiguration(transaction, broker, root, config);
        broker.storeDocument(transaction, XmldbURI.create("test_matches.xml"), new StringInputSource(xml), MimeType.XML_TYPE, root);
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager) BrokerPool(org.exist.storage.BrokerPool)

Example 42 with StringInputSource

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

the class IndexerTest3 method store_suppress_type.

private void store_suppress_type(final String propValue, final String xml) throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    pool.getConfiguration().setProperty(Indexer.PROPERTY_SUPPRESS_WHITESPACE, propValue);
    // Make sure to keep preserve whitespace mixed content stable even if default changes. fixme! - should test both. /ljo
    boolean propWSMValue = false;
    pool.getConfiguration().setProperty(Indexer.PROPERTY_PRESERVE_WS_MIXED_CONTENT, propWSMValue);
    final TransactionManager txnMgr = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
        final Txn txn = txnMgr.beginTransaction()) {
        try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
            broker.storeDocument(txn, TestConstants.TEST_XML_URI, new StringInputSource(xml), MimeType.XML_TYPE, collection);
            broker.flush();
            broker.saveCollection(txn, collection);
        }
        txnMgr.commit(txn);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 43 with StringInputSource

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

the class DeepEmbeddedBackupRestoreTest method createHierarchy.

private CollectionsAndDocuments createHierarchy(final XmldbURI baseCollectionUri, final int depth, final int maxWidth, final int xmlDocsPerCollection, final int binDocsPerCollection) throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final List<XmldbURI> collectionUris = new ArrayList<>();
    final List<ResourceInfo> documentInfos = new ArrayList<>();
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    try (final Txn transaction = brokerPool.getTransactionManager().beginTransaction();
        final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        try (final Collection baseCollection = broker.getOrCreateCollection(transaction, baseCollectionUri)) {
            collectionUris.add(baseCollectionUri);
            final Random random = new Random();
            XmldbURI subCollectionUri = baseCollectionUri;
            for (int d = 0; d < depth; d++) {
                subCollectionUri = subCollectionUri.append("sub_" + d);
                try (final Collection subCollection = broker.getOrCreateCollection(transaction, subCollectionUri)) {
                    collectionUris.add(subCollectionUri);
                    final int width = random.nextInt(maxWidth - 1) + 1;
                    for (int w = 0; w < width; w++) {
                        final XmldbURI sibCollectionUri = subCollectionUri.append("sib_" + w);
                        try (final Collection sibCollection = broker.getOrCreateCollection(transaction, sibCollectionUri)) {
                            collectionUris.add(sibCollectionUri);
                            for (int x = 0; x < xmlDocsPerCollection; x++) {
                                // store XML document
                                final XmldbURI xmlName = XmldbURI.create("doc_" + x + ".xml");
                                final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + EOL + "<position id=\"" + x + "\" d=\"" + d + "\" w=\"" + w + "\"/>";
                                broker.storeDocument(transaction, xmlName, new StringInputSource(xml), MimeType.XML_TYPE, sibCollection);
                                final byte[] xmlData = xml.getBytes(UTF_8);
                                final long xmlHash = hash64.hash(xmlData, 0, xmlData.length, XXHASH64_SEED);
                                documentInfos.add(new ResourceInfo(sibCollectionUri.append(xmlName), xmlHash));
                                // store Binary document
                                final XmldbURI binName = XmldbURI.create("doc_" + x + ".bin");
                                final String bin = x + ":" + d + ":" + w;
                                broker.storeDocument(transaction, binName, new StringInputSource(bin.getBytes(UTF_8)), MimeType.BINARY_TYPE, sibCollection);
                                final byte[] binData = bin.getBytes(UTF_8);
                                final long binHash = hash64.hash(binData, 0, binData.length, XXHASH64_SEED);
                                documentInfos.add(new ResourceInfo(sibCollectionUri.append(binName), binHash));
                            }
                        }
                    }
                }
            }
        }
        transaction.commit();
    }
    return new CollectionsAndDocuments(collectionUris, documentInfos);
}
Also used : Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) BrokerPool(org.exist.storage.BrokerPool)

Example 44 with StringInputSource

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

the class JournalXmlTest method largeJournalEntry_nonCorrupt.

@Test
public void largeJournalEntry_nonCorrupt() throws IllegalAccessException, EXistException, NoSuchFieldException, LockException, SAXException, PermissionDeniedException, IOException, InterruptedException {
    checkpointJournalAndSwitchFile();
    // generate a string filled with random a-z characters which is larger than the journal buffer
    // 3 * the journal buffer size
    final byte[] buf = new byte[Journal.BUFFER_SIZE * 3];
    final Random random = new Random();
    for (int i = 0; i < buf.length; i++) {
        final byte singleByteChar = (byte) ('a' + random.nextInt('z' - 'a' - 1));
        buf[i] = singleByteChar;
    }
    final String largeText = new String(buf, UTF_8);
    final String xml = "<large-text>" + largeText + "</large-text>";
    final InputSource source = new StringInputSource(xml);
    source.setEncoding("UTF-8");
    BrokerPool.FORCE_CORRUPTION = false;
    final TxnDoc<String> stored = store(COMMIT, source, "large-non-corrupt.xml");
    flushJournal();
    // shutdown the broker pool (without destroying the data dir)
    existEmbeddedServer.getBrokerPool().shutdown();
    // check journal entries written for store
    assertPartialOrdered(store_expected(stored, 0, largeText), readLatestJournalEntries());
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) StringInputSource(org.exist.util.StringInputSource) Random(java.util.Random) Test(org.junit.Test)

Example 45 with StringInputSource

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

the class ConnectionIT method getConnectionFromModuleIsAutomaticallyClosed.

@Test
public void getConnectionFromModuleIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException, LockException, SAXException {
    final String moduleQuery = "module namespace mymodule = \"http://mymodule.com\";\n" + "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "declare function mymodule:get-handle() {\n" + "    sql:get-connection(\"" + h2Database.getDriverClass().getName() + "\", \"" + h2Database.getUrl() + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + "};\n";
    final String mainQuery = "import module namespace mymodule = \"http://mymodule.com\" at \"xmldb:exist:///db/mymodule.xqm\";\n" + "mymodule:get-handle()";
    final Source mainQuerySource = new StringSource(mainQuery);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        // store module
        try (final Collection collection = broker.openCollection(XmldbURI.create("/db"), Lock.LockMode.WRITE_LOCK)) {
            broker.storeDocument(transaction, XmldbURI.create("mymodule.xqm"), new StringInputSource(moduleQuery.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
        }
        final Tuple2<XQueryContext, ModuleContext> escapedContexts = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
            final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
            // get the context of the library module
            final Module[] libraryModules = mainQueryContext.getModules("http://mymodule.com");
            assertEquals(1, libraryModules.length);
            assertTrue(libraryModules[0] instanceof ExternalModule);
            final ExternalModule libraryModule = (ExternalModule) libraryModules[0];
            final XQueryContext libraryQueryContext = libraryModule.getContext();
            assertTrue(libraryQueryContext instanceof ModuleContext);
            // execute the query
            final Sequence result = executeQuery(broker, mainCompiledQuery);
            // check that the handle for the sql connection that was created was valid
            assertEquals(1, result.getItemCount());
            assertTrue(result.itemAt(0) instanceof IntegerValue);
            assertEquals(Type.LONG, result.itemAt(0).getType());
            final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
            assertFalse(connectionHandle == 0);
            // intentionally escape the contexts from the lambda
            return Tuple(mainQueryContext, (ModuleContext) libraryQueryContext);
        });
        final XQueryContext escapedMainQueryContext = escapedContexts._1;
        final ModuleContext escapedLibraryQueryContext = escapedContexts._2;
        assertTrue(escapedMainQueryContext != escapedLibraryQueryContext);
        // check the connections were closed in the main module
        final int mainConnectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
        assertEquals(0, mainConnectionsCount);
        // check the connections were closed in the library module
        final int libraryConnectionsCount = ModuleUtils.readContextMap(escapedLibraryQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
        assertEquals(0, libraryConnectionsCount);
        transaction.commit();
    }
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) StringSource(org.exist.source.StringSource) Map(java.util.Map) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

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