Search in sources :

Example 76 with Source

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

the class ModuleFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final ValueSequence list = new ValueSequence();
    if (getArgumentCount() == 1) {
        final XQueryContext tempContext = new XQueryContext(context.getBroker().getBrokerPool(), context.getProfiler());
        tempContext.setModuleLoadPath(context.getModuleLoadPath());
        tempContext.prepareForExecution();
        final AnyURIValue uri = ((AnyURIValue) args[0].itemAt(0));
        if (isCalledAs(FS_MODULE_FUNCTIONS_NAME)) {
            try {
                final URI locationUri = uri.toURI();
                final Source source = SourceFactory.getSource(context.getBroker(), tempContext.getModuleLoadPath(), locationUri.toString(), false);
                if (source != null) {
                    tempContext.setSource(source);
                }
            } catch (final IOException | PermissionDeniedException e) {
                throw new XPathException(this, ErrorCodes.XQST0059, e.getMessage());
            }
        }
        // attempt to import the module
        Module[] modules = null;
        try {
            modules = tempContext.importModule(null, null, new AnyURIValue[] { uri });
        } catch (final XPathException e) {
            if (e.getErrorCode().equals(ErrorCodes.XQST0059)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Failed to import module: {}: {}", args[0].getStringValue(), e.getMessage(), e);
                }
                modules = null;
            } else {
                if (e.getLine() < 1) {
                    e.setLocation(this.getLine(), this.getColumn(), this.getSource());
                }
                throw e;
            }
        }
        if (modules == null || modules.length == 0) {
            return Sequence.EMPTY_SEQUENCE;
        }
        // there can be only one!
        final Module module = modules[0];
        if (!module.isInternalModule()) {
            // ensure variable declarations in the imported module are analyzed.
            // unlike when using a normal import statement, this is not done automatically
            ((ExternalModule) module).analyzeGlobalVars();
        }
        LoadXQueryModule.addFunctionRefsFromModule(this, tempContext, list, module);
    } else {
        addFunctionRefsFromContext(list);
    }
    return list;
}
Also used : PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) Module(org.exist.xquery.Module) LoadXQueryModule(org.exist.xquery.functions.fn.LoadXQueryModule) URI(java.net.URI) Source(org.exist.source.Source)

Example 77 with Source

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

the class FunUnparsedText method readLines.

private Sequence readLines(final String uriParam, final String encoding) throws XPathException {
    try {
        final Sequence result = new ValueSequence();
        final Source source = getSource(uriParam);
        final Charset charset = getCharset(encoding, source);
        try (final InputStream inputStream = source.getInputStream()) {
            // Nested try() as inputStream can be null
            if (inputStream == null) {
                throw new XPathException(this, ErrorCodes.FOUT1170, "Unable to retrieve bytestream from " + uriParam);
            }
            try (final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    result.add(new StringValue(line));
                }
            }
        }
        return result;
    } catch (final IOException e) {
        throw new XPathException(this, ErrorCodes.FOUT1170, e.getMessage());
    }
}
Also used : Charset(java.nio.charset.Charset) FileSource(org.exist.source.FileSource) Source(org.exist.source.Source)

Example 78 with Source

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

the class UserXQueryJob method execute.

@Override
public final void execute(final JobExecutionContext jec) throws JobExecutionException {
    final JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap();
    // TODO why are these values not used from the class members?
    final String xqueryResource = (String) jobDataMap.get(XQUERY_SOURCE);
    final Subject user = (Subject) jobDataMap.get(ACCOUNT);
    final BrokerPool pool = (BrokerPool) jobDataMap.get(DATABASE);
    final Properties params = (Properties) jobDataMap.get(PARAMS);
    final boolean unschedule = ((Boolean) jobDataMap.get(UNSCHEDULE));
    // if invalid arguments then abort
    if ((pool == null) || (xqueryResource == null) || (user == null)) {
        abort("BrokerPool or XQueryResource or User was null!");
    }
    try (final DBBroker broker = pool.get(Optional.of(user))) {
        if (xqueryResource.indexOf(':') > 0) {
            final Source source = SourceFactory.getSource(broker, "", xqueryResource, true);
            if (source != null) {
                executeXQuery(pool, broker, source, params);
                return;
            }
        } else {
            final XmldbURI pathUri = XmldbURI.create(xqueryResource);
            try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
                if (lockedResource != null) {
                    final Source source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
                    executeXQuery(pool, broker, source, params);
                    return;
                }
            }
        }
        LOG.warn("XQuery User Job not found: {}, job not scheduled", xqueryResource);
    } catch (final EXistException ee) {
        abort("Could not get DBBroker!");
    } catch (final PermissionDeniedException pde) {
        abort("Permission denied for the scheduling user: " + user.getName() + "!");
    } catch (final XPathException xpe) {
        abort("XPathException in the Job: " + xpe.getMessage() + "!", unschedule);
    } catch (final IOException e) {
        abort("Could not load XQuery: " + e.getMessage());
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) XPathException(org.exist.xquery.XPathException) EXistException(org.exist.EXistException) IOException(java.io.IOException) Properties(java.util.Properties) Subject(org.exist.security.Subject) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 79 with Source

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

the class Eval method loadQueryFromURI.

/**
 * @param expr
 * @throws XPathException
 * @throws NullPointerException
 * @throws IllegalArgumentException
 */
private Source loadQueryFromURI(final Item expr) throws XPathException, NullPointerException, IllegalArgumentException {
    final String location = expr.getStringValue();
    Source querySource = null;
    if (location.indexOf(':') < 0 || location.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
        try {
            XmldbURI locationUri = XmldbURI.xmldbUriFor(location);
            // be added.
            if (location.indexOf('/') < 0 || location.startsWith(".")) {
                final XmldbURI moduleLoadPathUri = XmldbURI.xmldbUriFor(context.getModuleLoadPath());
                locationUri = moduleLoadPathUri.resolveCollectionPath(locationUri);
            }
            try (final LockedDocument lockedSourceDoc = context.getBroker().getXMLResource(locationUri.toCollectionPathURI(), LockMode.READ_LOCK)) {
                final DocumentImpl sourceDoc = lockedSourceDoc == null ? null : lockedSourceDoc.getDocument();
                if (sourceDoc == null) {
                    throw new XPathException(this, "source for module " + location + " not found in database");
                }
                if (sourceDoc.getResourceType() != DocumentImpl.BINARY_FILE || !"application/xquery".equals(sourceDoc.getMetadata().getMimeType())) {
                    throw new XPathException(this, "source for module " + location + " is not an XQuery or " + "declares a wrong mime-type");
                }
                querySource = new DBSource(context.getBroker(), (BinaryDocument) sourceDoc, true);
            } catch (final PermissionDeniedException e) {
                throw new XPathException(this, "permission denied to read module source from " + location);
            }
        } catch (final URISyntaxException e) {
            throw new XPathException(this, e);
        }
    } else {
        // No. Load from file or URL
        try {
            // TODO: use URIs to ensure proper resolution of relative locations
            querySource = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), location, true);
            if (querySource == null) {
                throw new XPathException(this, "source for query at " + location + " not found");
            }
        } catch (final MalformedURLException e) {
            throw new XPathException(this, "source location for query at " + location + " should be a valid URL: " + e.getMessage());
        } catch (final IOException e) {
            throw new XPathException(this, "source for query at " + location + " not found: " + e.getMessage());
        } catch (final PermissionDeniedException e) {
            throw new XPathException(this, "Permission denied to access query at " + location + " : " + e.getMessage());
        }
    }
    return querySource;
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) MalformedURLException(java.net.MalformedURLException) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) FileSource(org.exist.source.FileSource) XmldbURI(org.exist.xmldb.XmldbURI)

Example 80 with Source

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

the class RESTServer method search.

/**
 * TODO: pass request and response objects to XQuery.
 *
 * @param broker the database broker
 * @param transaction the database transaction
 * @param query the XQuery
 * @param path the path of the request
 * @param namespaces any XQuery namespace bindings
 * @param variables any XQuery variable bindings
 * @param howmany the number of items in the results to return
 * @param start the start position in the results to return
 * @param typed whether the result nodes should be typed
 * @param outputProperties the serialization properties
 * @param wrap true to wrap the result of the XQuery in an exist:result
 * @param cache whether to cache the results
 * @param request the request
 * @param response the response
 *
 * @throws BadRequestException if a bad request is made
 * @throws PermissionDeniedException if the request has insufficient permissions
 * @throws XPathException if the XQuery raises an error
 */
protected void search(final DBBroker broker, final Txn transaction, final String query, final String path, final List<Namespace> namespaces, final ElementImpl variables, final int howmany, final int start, final boolean typed, final Properties outputProperties, final boolean wrap, final boolean cache, final HttpServletRequest request, final HttpServletResponse response) throws BadRequestException, PermissionDeniedException, XPathException {
    if (xquerySubmission == EXistServlet.FeatureEnabled.FALSE) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    } else if (xquerySubmission == EXistServlet.FeatureEnabled.AUTHENTICATED_USERS_ONLY) {
        final Subject currentSubject = broker.getCurrentSubject();
        if (!currentSubject.isAuthenticated() || currentSubject.getId() == RealmImpl.GUEST_GROUP_ID) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
    }
    final String sessionIdParam = outputProperties.getProperty(Serializer.PROPERTY_SESSION_ID);
    if (sessionIdParam != null) {
        try {
            final int sessionId = Integer.parseInt(sessionIdParam);
            if (sessionId > -1) {
                final Sequence cached = sessionManager.get(query, sessionId);
                if (cached != null) {
                    LOG.debug("Returning cached query result");
                    writeResults(response, broker, transaction, cached, howmany, start, typed, outputProperties, wrap, 0, 0);
                } else {
                    LOG.debug("Cached query result not found. Probably timed out. Repeating query.");
                }
            }
        } catch (final NumberFormatException e) {
            throw new BadRequestException("Invalid session id passed in query request: " + sessionIdParam);
        }
    }
    final XmldbURI pathUri = XmldbURI.createInternal(path);
    final Source source = new StringSource(query);
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    CompiledXQuery compiled = null;
    try {
        final XQuery xquery = broker.getBrokerPool().getXQueryService();
        compiled = pool.borrowCompiledXQuery(broker, source);
        XQueryContext context;
        if (compiled == null) {
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        context.setStaticallyKnownDocuments(new XmldbURI[] { pathUri });
        context.setBaseURI(new AnyURIValue(pathUri.toString()));
        declareNamespaces(context, namespaces);
        declareVariables(context, variables, request, response);
        final long compilationTime;
        if (compiled == null) {
            final long compilationStart = System.currentTimeMillis();
            compiled = xquery.compile(context, source);
            compilationTime = System.currentTimeMillis() - compilationStart;
        } else {
            compiled.getContext().updateContext(context);
            context.getWatchDog().reset();
            compilationTime = 0;
        }
        try {
            final long executeStart = System.currentTimeMillis();
            final Sequence resultSequence = xquery.execute(broker, compiled, null, outputProperties);
            final long executionTime = System.currentTimeMillis() - executeStart;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found {} in {}ms.", resultSequence.getItemCount(), executionTime);
            }
            if (cache) {
                final int sessionId = sessionManager.add(query, resultSequence);
                outputProperties.setProperty(Serializer.PROPERTY_SESSION_ID, Integer.toString(sessionId));
                if (!response.isCommitted()) {
                    response.setIntHeader("X-Session-Id", sessionId);
                }
            }
            writeResults(response, broker, transaction, resultSequence, howmany, start, typed, outputProperties, wrap, compilationTime, executionTime);
        } finally {
            context.runCleanupTasks();
        }
    } catch (final IOException e) {
        throw new BadRequestException(e.getMessage(), e);
    } finally {
        if (compiled != null) {
            pool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : Subject(org.exist.security.Subject) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) InputSource(org.xml.sax.InputSource) URLSource(org.exist.source.URLSource) XQueryPool(org.exist.storage.XQueryPool) StringSource(org.exist.source.StringSource) XmldbURI(org.exist.xmldb.XmldbURI)

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