Search in sources :

Example 36 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class RpcConnection method execute.

@Deprecated
@Override
public Map<String, Object> execute(final String pathToQuery, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
    final long startTime = System.currentTimeMillis();
    final Optional<String> sortBy = Optional.ofNullable(parameters.get(RpcAPI.SORT_EXPR)).map(Object::toString);
    return this.<Map<String, Object>>readDocument(XmldbURI.createInternal(pathToQuery)).apply((document, broker, transaction) -> {
        final BinaryDocument xquery = (BinaryDocument) document;
        if (xquery.getResourceType() != DocumentImpl.BINARY_FILE) {
            throw new EXistException("Document " + pathToQuery + " is not a binary resource");
        }
        if (!xquery.getPermissions().validate(user, Permission.READ | Permission.EXECUTE)) {
            throw new PermissionDeniedException("Insufficient privileges to access resource");
        }
        final Source source = new DBSource(broker, xquery, true);
        try {
            final Map<String, Object> rpcResponse = this.<Map<String, Object>>compileQuery(broker, transaction, source, parameters).apply(compiledQuery -> queryResultToRpcResponse(startTime, doQuery(broker, compiledQuery, null, parameters), sortBy));
            return rpcResponse;
        } catch (final XPathException e) {
            throw new EXistException(e);
        }
    });
}
Also used : DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) EXistException(org.exist.EXistException) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource)

Example 37 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class DBUtils method xquery.

public static ResourceSet xquery(final Collection collection, final String xquery) throws XMLDBException {
    final EXistXQueryService service = getXQueryService(collection);
    final Source source = new StringSource(xquery);
    return service.execute(source);
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) StringSource(org.exist.source.StringSource)

Example 38 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class EmbeddedBinariesTest method executeXQuery.

@Override
protected QueryResultAccessor<Sequence, IOException> executeXQuery(final String query) throws Exception {
    final Source source = new StringSource(query);
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    final XQueryPool pool = brokerPool.getXQueryPool();
    final XQuery xquery = brokerPool.getXQueryService();
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final CompiledXQuery existingCompiled = pool.borrowCompiledXQuery(broker, source);
        final XQueryContext context;
        final CompiledXQuery compiled;
        if (existingCompiled == null) {
            context = new XQueryContext(brokerPool);
            compiled = xquery.compile(context, source);
        } else {
            context = existingCompiled.getContext();
            context.prepareForReuse();
            compiled = existingCompiled;
        }
        final Sequence results = xquery.execute(broker, compiled, null);
        return consumer2E -> {
            try {
                // context.runCleanupTasks();  //TODO(AR) shows the ordering issue with binary values (see comment below)
                consumer2E.accept(results);
            } finally {
                // TODO(AR) performing #runCleanupTasks causes the stream to be closed, so if we do so before we are finished with the results, serialization fails.
                context.runCleanupTasks();
                pool.returnCompiledXQuery(source, compiled);
            }
        };
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) StringInputSource(org.exist.util.StringInputSource) org.exist.xquery.value(org.exist.xquery.value) IOException(java.io.IOException) ExistEmbeddedServer(org.exist.test.ExistEmbeddedServer) MimeType(org.exist.util.MimeType) Source(org.exist.source.Source) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Optional(java.util.Optional) XQueryPool(org.exist.storage.XQueryPool) Lock(org.exist.storage.lock.Lock) ClassRule(org.junit.ClassRule) XQueryContext(org.exist.xquery.XQueryContext) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) DBBroker(org.exist.storage.DBBroker) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) BrokerPool(org.exist.storage.BrokerPool)

Example 39 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class SMEvents method runScript.

protected void runScript(Subject subject, String scriptURI, String script, QName functionName, List<Expression> args) {
    final Database db = getDatabase();
    try (final DBBroker broker = db.get(Optional.ofNullable(subject))) {
        final Source source = getQuerySource(broker, scriptURI, script);
        if (source == null) {
            return;
        }
        final XQuery xquery = broker.getBrokerPool().getXQueryService();
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        final CompiledXQuery compiled = xquery.compile(context, source);
        // Sequence result = xquery.execute(compiled, subject.getName());
        final ProcessMonitor pm = db.getProcessMonitor();
        // execute the XQuery
        try {
            final UserDefinedFunction function = context.resolveFunction(functionName, 0);
            if (function != null) {
                context.getProfiler().traceQueryStart();
                pm.queryStarted(context.getWatchDog());
                final FunctionCall call = new FunctionCall(context, function);
                if (args != null) {
                    call.setArguments(args);
                }
                final Sequence contextSequence;
                final ContextItemDeclaration cid = context.getContextItemDeclartion();
                if (cid != null) {
                    contextSequence = cid.eval(null);
                } else {
                    contextSequence = NodeSet.EMPTY_SET;
                }
                call.analyze(new AnalyzeContextInfo());
                call.eval(contextSequence);
            }
        } catch (final XPathException e) {
            // XXX: log
            e.printStackTrace();
        } finally {
            if (pm != null) {
                context.getProfiler().traceQueryEnd(context);
                pm.queryCompleted(context.getWatchDog());
            }
            compiled.reset();
            context.reset();
        }
    } catch (final Exception e) {
        // XXX: log
        e.printStackTrace();
    }
}
Also used : Sequence(org.exist.xquery.value.Sequence) ProcessMonitor(org.exist.storage.ProcessMonitor) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database)

Example 40 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class FunctionFactory method getXQueryModuleFunction.

/**
 * Gets an XQuery function from an XQuery Module
 *
 * @param throwOnNotFound true to throw an XPST0017 if the functions is not found, false to just return null
 */
private static FunctionCall getXQueryModuleFunction(final XQueryContext context, final XQueryAST ast, final List<Expression> params, final QName qname, final Module module, final boolean throwOnNotFound) throws XPathException {
    final FunctionCall fc;
    final UserDefinedFunction func = ((ExternalModule) module).getFunction(qname, params.size(), context);
    if (func == null) {
        // check if the module has been compiled already
        if (module.isReady()) {
            final StringBuilder msg = new StringBuilder("Function ").append(qname.getStringValue()).append('#').append(params.size()).append(" is not defined in namespace '").append(qname.getNamespaceURI()).append('\'');
            if (module instanceof ExternalModule) {
                final Source moduleSource = ((ExternalModule) module).getSource();
                msg.append(" for module: ").append(moduleSource.pathOrShortIdentifier());
            }
            if (throwOnNotFound) {
                throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, msg.toString());
            } else {
                return null;
            }
        // If not, postpone the function resolution
        // Register a forward reference with the root module, so it gets resolved
        // when the main query has been compiled.
        } else {
            fc = new FunctionCall(((ExternalModule) module).getContext(), qname, params);
            fc.setLocation(ast.getLine(), ast.getColumn());
            if (((ExternalModule) module).getContext() == context) {
                context.addForwardReference(fc);
            } else {
                context.getRootContext().addForwardReference(fc);
            }
        }
    } else {
        fc = new FunctionCall(context, func);
        fc.setArguments(params);
        fc.setLocation(ast.getLine(), ast.getColumn());
    }
    return fc;
}
Also used : Source(org.exist.source.Source)

Aggregations

Source (org.exist.source.Source)83 StringSource (org.exist.source.StringSource)62 Sequence (org.exist.xquery.value.Sequence)43 BrokerPool (org.exist.storage.BrokerPool)42 DBBroker (org.exist.storage.DBBroker)42 Txn (org.exist.storage.txn.Txn)40 Test (org.junit.Test)35 StringInputSource (org.exist.util.StringInputSource)32 DBSource (org.exist.source.DBSource)23 IOException (java.io.IOException)19 PermissionDeniedException (org.exist.security.PermissionDeniedException)19 InputSource (org.xml.sax.InputSource)15 EXistException (org.exist.EXistException)12 XmldbURI (org.exist.xmldb.XmldbURI)11 XQueryPool (org.exist.storage.XQueryPool)9 XQueryContext (org.exist.xquery.XQueryContext)9 FileSource (org.exist.source.FileSource)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)8 XQuery (org.exist.xquery.XQuery)8 Collection (org.exist.collections.Collection)7