use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class HistoryTriggerTest method setup.
@Before
public void setup() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
// create and store the collection.xconf for the test collection
Collection configCollection = broker.getOrCreateCollection(transaction, TEST_CONFIG_COLLECTION_URI);
broker.saveCollection(transaction, configCollection);
broker.storeDocument(transaction, CollectionConfiguration.DEFAULT_COLLECTION_CONFIG_FILE_URI, new StringInputSource(COLLECTION_CONFIG), MimeType.XML_TYPE, configCollection);
// create the test collection
Collection testCollection = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI);
broker.saveCollection(transaction, testCollection);
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class XMLReaderSecurityTest method cannotExpandExternalEntitiesWhenDisabled.
@Test
public void cannotExpandExternalEntitiesWhenDisabled() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, TransformerException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
// 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("cannotExpandExternalEntitiesWhenDisabled", 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 not 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_EXPANSION_DISABLED_DOC;
final String actual = serialize(testDoc.getDocument());
assertEquals(expected, actual);
}
}
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class AbstractXMLReaderSecurityTest method removeTestData.
@After
public void removeTestData() throws EXistException, PermissionDeniedException, IOException, TriggerException {
final BrokerPool brokerPool = getExistEmbeddedServer().getBrokerPool();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
final Collection testCollection = broker.getCollection(TEST_COLLECTION);
if (testCollection != null && !broker.removeCollection(transaction, testCollection)) {
transaction.abort();
fail("Unable to remove test collection");
}
transaction.commit();
}
}
use of org.exist.storage.BrokerPool 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();
}
}
use of org.exist.storage.BrokerPool 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();
}
}
Aggregations