use of org.exist.xquery.XQuery in project exist by eXist-db.
the class XQueryStartupTrigger method executeQuery.
/**
* Execute xquery on path
*
* @param broker eXist database broker
* @param path path to query, formatted as xmldb:exist:///db/...
*/
private void executeQuery(DBBroker broker, String path) {
XQueryContext context = null;
try {
// Get path to xquery
Source source = SourceFactory.getSource(broker, null, path, false);
if (source == null) {
LOG.info("No XQuery found at '{}'", path);
} else {
// Setup xquery service
XQuery service = broker.getBrokerPool().getXQueryService();
context = new XQueryContext(broker.getBrokerPool());
// Allow use of modules with relative paths
String moduleLoadPath = StringUtils.substringBeforeLast(path, "/");
context.setModuleLoadPath(moduleLoadPath);
// Compile query
CompiledXQuery compiledQuery = service.compile(context, source);
LOG.info("Starting XQuery at '{}'", path);
// Finish preparation
context.prepareForExecution();
// Execute
Sequence result = service.execute(broker, compiledQuery, null);
// Log results
LOG.info("Result XQuery: '{}'", result.getStringValue());
}
} catch (Throwable t) {
// Dirty, catch it all
LOG.error("An error occurred during preparation/execution of the XQuery script {}: {}", path, t.getMessage(), t);
} finally {
if (context != null) {
context.runCleanupTasks();
}
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class Launcher method checkInstalledApps.
private void checkInstalledApps() {
try {
final BrokerPool pool = BrokerPool.getInstance();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence pkgs = xquery.execute(broker, "repo:list()", null);
for (final SequenceIterator i = pkgs.iterate(); i.hasNext(); ) {
final ExistRepository.Notification notification = new ExistRepository.Notification(ExistRepository.Action.INSTALL, i.nextItem().getStringValue());
final Optional<ExistRepository> expathRepo = pool.getExpathRepo();
if (expathRepo.isPresent()) {
update(expathRepo.get(), notification);
utilityPanel.update(expathRepo.get(), notification);
}
expathRepo.orElseThrow(() -> new EXistException("EXPath repository is not available."));
}
}
} catch (final EXistException | XPathException | PermissionDeniedException e) {
System.err.println("Failed to check installed packages: " + e.getMessage());
e.printStackTrace();
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class SecurityManagerTest method removedGroupExists.
private boolean removedGroupExists(final DBBroker broker, final String groupName) throws XPathException, PermissionDeniedException {
final XQuery queryService = broker.getBrokerPool().getXQueryService();
final Sequence result = queryService.execute(broker, "declare namespace config='http://exist-db.org/Configuration'; collection('" + REMOVED_GROUPS_URI + "')//config:group[config:name eq '" + groupName + "']", null);
return result.getItemCount() == 1 && result.itemAt(0).toJavaObject(Boolean.class) == true;
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class XqueryApiTest method executeQuery.
private Sequence executeQuery(final String uid, final String pwd, final String query) throws ApiException {
try {
final BrokerPool pool = server.getBrokerPool();
final XQuery xquery = pool.getXQueryService();
final Subject user = pool.getSecurityManager().authenticate(uid, pwd);
try (final DBBroker broker = pool.get(Optional.of(user))) {
return xquery.execute(broker, query, null);
}
} catch (final AuthenticationException | EXistException | PermissionDeniedException | XPathException e) {
throw new ApiException(e.getMessage(), e);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class FnCollectionSecurityTest method cannotAccessCollectionInCollectionHierarchyWithDeniedExecute.
@Test(expected = PermissionDeniedException.class)
public void cannotAccessCollectionInCollectionHierarchyWithDeniedExecute() throws EXistException, AuthenticationException, PermissionDeniedException, XPathException {
// as docTestUser1 user
final String query = "fn:collection('" + TEST_SUB_COLLECTION_1_1 + "')";
final BrokerPool pool = server.getBrokerPool();
final SecurityManager securityManager = pool.getSecurityManager();
final Subject testUser1 = securityManager.authenticate(TEST_USER_1, TEST_USER_1);
try (final DBBroker broker = pool.get(Optional.of(testUser1));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
final XQuery xqueryService = pool.getXQueryService();
final Sequence result = xqueryService.execute(broker, query, null);
fail("Expected PermissionDeniedException via XPathException");
transaction.commit();
} catch (final XPathException e) {
if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
throw (PermissionDeniedException) e.getCause();
} else {
throw e;
}
}
}
Aggregations