Search in sources :

Example 1 with XQuery

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();
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Source(org.exist.source.Source)

Example 2 with XQuery

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();
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) DBBroker(org.exist.storage.DBBroker) SequenceIterator(org.exist.xquery.value.SequenceIterator) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool) ExistRepository(org.exist.repo.ExistRepository)

Example 3 with XQuery

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;
}
Also used : XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence)

Example 4 with XQuery

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);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) EXistException(org.exist.EXistException) BrokerPool(org.exist.storage.BrokerPool)

Example 5 with XQuery

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;
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11