Search in sources :

Example 41 with Collection

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

the class XMLReaderExpansionTest method expandExternalEntities.

@Test
public void expandExternalEntities() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final Map<String, Boolean> parserConfig = new HashMap<>();
    parserConfig.put(FEATURE_EXTERNAL_GENERAL_ENTITIES, true);
    brokerPool.getConfiguration().setProperty(XMLReaderPool.XmlParser.XML_PARSER_FEATURES_PROPERTY, parserConfig);
    // 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("expandExternalEntities", 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 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_EXPANDED_DOC.replace(EXTERNAL_FILE_PLACEHOLDER, secret._1);
                final String actual = serialize(testDoc.getDocument());
                assertEquals(expected, actual);
            }
        }
        transaction.commit();
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI) Test(org.junit.Test)

Example 42 with Collection

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

the class LexerTest method query.

@Test
public void query() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, RecognitionException, XPathException, TokenStreamException {
    String query = "//p[. = '\u4ED6\u4E3A\u8FD9\u9879\u5DE5\u7A0B\u6295" + "\u5165\u4E86\u5341\u4E09\u5E74\u65F6\u95F4\u3002']";
    // get a BrokerPool for access to the database engine
    BrokerPool pool = BrokerPool.getInstance();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Txn transaction = transact.beginTransaction()) {
            // parse the xml source
            Collection collection = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            broker.saveCollection(transaction, collection);
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(xml), MimeType.XML_TYPE, collection);
            // TODO : unlock the collection here ?
            transact.commit(transaction);
        }
        // parse the query into the internal syntax tree
        XQueryContext context = new XQueryContext(broker.getBrokerPool());
        XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
        XQueryParser xparser = new XQueryParser(lexer);
        XQueryTreeParser treeParser = new XQueryTreeParser(context);
        xparser.xpath();
        if (xparser.foundErrors()) {
            fail(xparser.getErrorMessage());
            return;
        }
        AST ast = xparser.getAST();
        PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            fail(treeParser.getErrorMessage());
            return;
        }
        expr.analyze(new AnalyzeContextInfo());
        // execute the query
        Sequence result = expr.eval(null, null);
        // check results
        int count = result.getItemCount();
    }
}
Also used : AST(antlr.collections.AST) XQueryParser(org.exist.xquery.parser.XQueryParser) Txn(org.exist.storage.txn.Txn) XQueryLexer(org.exist.xquery.parser.XQueryLexer) Sequence(org.exist.xquery.value.Sequence) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 43 with Collection

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

the class ImportModuleTest method storeModules.

private void storeModules(final DBBroker broker, final Txn transaction, final String collectionUri, final Tuple2<String, String>... modules) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
    // store modules
    try (final Collection collection = broker.openCollection(XmldbURI.create(collectionUri), Lock.LockMode.WRITE_LOCK)) {
        for (final Tuple2<String, String> module : modules) {
            final XmldbURI moduleName = XmldbURI.create(module._1);
            broker.storeDocument(transaction, moduleName, new StringInputSource(module._2.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
        }
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Example 44 with Collection

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

the class LuceneMatchListenerTest method configureAndStore.

private void configureAndStore(final String config, final String data) throws EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, 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 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(data), 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 45 with Collection

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

the class NGramIndexWorker method getDefinedIndexes.

/**
 * Check index configurations for all collection in the given DocumentSet and return
 * a list of QNames, which have indexes defined on them.
 *
 * @param broker the database broker
 * @param docs   documents
 */
private List<QName> getDefinedIndexes(final DBBroker broker, final DocumentSet docs) {
    final List<QName> indexes = new ArrayList<>(20);
    for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
        final Collection collection = i.next();
        final IndexSpec idxConf = collection.getIndexConfiguration(broker);
        if (idxConf != null) {
            final Map<?, ?> config = (Map<?, ?>) idxConf.getCustomIndexSpec(NGramIndex.ID);
            if (config != null) {
                for (final Object name : config.keySet()) {
                    indexes.add((QName) name);
                }
            }
        }
    }
    return indexes;
}
Also used : IndexSpec(org.exist.storage.IndexSpec) QName(org.exist.dom.QName) Collection(org.exist.collections.Collection)

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