use of org.exist.storage.DBBroker in project exist by eXist-db.
the class EmbeddedOutputStream method uploadToDb.
private static void uploadToDb(final BrokerPool pool, final XmldbURL url, final Path tempFile) throws IOException {
try (final DBBroker broker = pool.getBroker()) {
final XmldbURI collectionUri = XmldbURI.create(url.getCollection());
final XmldbURI documentUri = XmldbURI.create(url.getDocumentName());
try (final Collection collection = broker.openCollection(collectionUri, Lock.LockMode.WRITE_LOCK)) {
if (collection == null) {
throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
}
if (collection.hasChildCollection(broker, documentUri)) {
throw new IOException("Resource " + documentUri.toString() + " is a collection.");
}
final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
final TransactionManager transact = pool.getTransactionManager();
try (final Txn txn = transact.beginTransaction()) {
broker.storeDocument(txn, documentUri, new FileInputSource(tempFile), mime, collection);
txn.commit();
}
}
} catch (final EXistException | PermissionDeniedException | LockException | SAXException e) {
LOG.error(e);
throw new IOException(e.getMessage(), e);
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("End document upload");
}
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class DatabaseResources method executeQuery.
public Sequence executeQuery(String queryPath, Map<String, String> params, Subject user) {
final String namespace = params.get(TARGETNAMESPACE);
final String publicId = params.get(PUBLICID);
final String catalogPath = params.get(CATALOG);
final String collection = params.get(COLLECTION);
if (logger.isDebugEnabled()) {
logger.debug("collection={} namespace={} publicId={} catalogPath={}", collection, namespace, publicId, catalogPath);
}
Sequence result = null;
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user))) {
final XQuery xquery = brokerPool.getXQueryService();
final XQueryContext context = new XQueryContext(brokerPool);
if (collection != null) {
context.declareVariable(COLLECTION, collection);
}
if (namespace != null) {
context.declareVariable(TARGETNAMESPACE, namespace);
}
if (publicId != null) {
context.declareVariable(PUBLICID, publicId);
}
if (catalogPath != null) {
context.declareVariable(CATALOG, catalogPath);
}
CompiledXQuery compiled = xquery.compile(context, new ClassLoaderSource(queryPath));
result = xquery.execute(broker, compiled, null);
} catch (final EXistException | XPathException | IOException | PermissionDeniedException ex) {
logger.error("Problem executing xquery", ex);
result = null;
}
return result;
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class AbstractLocal method withDb.
/**
* Higher-order-function for performing an XMLDB operation on
* the database
*
* @param dbOperation The operation to perform on the database
* @param <R> The return type of the operation
*
* @return the result of the operation
*
* @throws XMLDBException if an error occurs when executing the operation.
*/
protected <R> R withDb(final LocalXmldbFunction<R> dbOperation) throws XMLDBException {
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user));
final Txn transaction = transaction().apply(broker)) {
try {
final R result = dbOperation.apply(broker, transaction);
transaction.commit();
return result;
} catch (final XMLDBException | EXistException e) {
transaction.abort();
throw e;
}
} catch (final EXistException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
}
use of org.exist.storage.DBBroker in project exist by eXist-db.
the class IndexerTest2 method executeQuery.
private String executeQuery() throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final StringWriter out = new StringWriter()) {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final Sequence result = xquery.execute(broker, XQUERY, null);
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 IndexerTest2 method storeDoc.
private static void storeDoc() throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
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_URI2, new StringInputSource(XML), MimeType.XML_TYPE, collection);
broker.flush();
broker.saveCollection(txn, collection);
}
txnMgr.commit(txn);
}
}
Aggregations