use of org.exist.storage.DBBroker in project exist by eXist-db.
the class CollectionStoreTest method storeBinary.
private void storeBinary(final PreserveType preserveOnCopy) throws EXistException, PermissionDeniedException, IOException, TriggerException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final Collection col = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI)) {
final byte[] bin = TEST_BIN_DOC.getBytes(UTF_8);
try (final InputStream is = new UnsynchronizedByteArrayInputStream(bin)) {
final int docId = broker.getNextResourceId(transaction);
final BinaryDocument binDoc = col.addBinaryResource(transaction, broker, new BinaryDocument(broker.getBrokerPool(), col, docId, TEST_BIN_DOC_URI), is, "text/plain", bin.length, null, null, preserveOnCopy);
assertNotNull(binDoc);
}
broker.saveCollection(transaction, col);
}
try (final Collection col = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
try (final LockedDocument lockedDoc = col.getDocumentWithLock(broker, TEST_BIN_DOC_URI, LockMode.READ_LOCK)) {
// NOTE: early release of collection lock inline with async locking
col.close();
if (lockedDoc != null) {
assertTrue(lockedDoc.getDocument() instanceof BinaryDocument);
final BinaryDocument doc = (BinaryDocument) lockedDoc.getDocument();
final Try<String, IOException> docContent = broker.withBinaryFile(transaction, doc, is -> Try.TaggedTryUnchecked(IOException.class, () -> new String(Files.readAllBytes(is), UTF_8)));
assertEquals(TEST_BIN_DOC, docContent.get());
}
}
}
transaction.commit();
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class IndexerTest method store_and_retrieve_ws_mixed_content_value.
private String store_and_retrieve_ws_mixed_content_value(final boolean preserve, final String typeXml, final String typeXquery) throws EXistException, IOException, LockException, AuthenticationException, PermissionDeniedException, SAXException, XPathException {
store_preserve_ws_mixed_content_value(preserve, typeXml);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence result = xquery.execute(broker, typeXquery, null);
try (final StringWriter out = new StringWriter()) {
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "yes");
final SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
return out.toString();
}
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class IndexerTest method store_preserve_ws_mixed_content_value.
private void store_preserve_ws_mixed_content_value(final boolean propValue, final String xml) throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
pool.getConfiguration().setProperty(Indexer.PROPERTY_PRESERVE_WS_MIXED_CONTENT, propValue);
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);
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class IndexerTest3 method store_and_retrieve_suppress_type.
private String store_and_retrieve_suppress_type(final String type, final String typeXml, final String typeXquery) throws EXistException, IOException, LockException, AuthenticationException, PermissionDeniedException, SAXException, XPathException {
store_suppress_type(type, typeXml);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final StringWriter out = new StringWriter()) {
final XQuery xquery = pool.getXQueryService();
final Sequence result = xquery.execute(broker, typeXquery, null);
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "no");
final SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
return out.toString();
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class RestoreAppsTest method restoreAndCheck.
private void restoreAndCheck(BrokerPool pool, Path backup, String expectedMessage) throws Exception {
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
Restore restore = new Restore();
TestRestoreListener listener = new TestRestoreListener();
restore.restore(broker, transaction, null, backup, listener, false);
if (expectedMessage != null) {
assertEquals(1, listener.skipped.size());
assertTrue(listener.skipped.get(0).endsWith(expectedMessage));
} else {
assertEquals(0, listener.skipped.size());
}
}
existEmbeddedServer.restart(true);
}
Aggregations