Search in sources :

Example 1 with URLSource

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

the class RESTServer method executeXProc.

/**
 * Directly execute an XProc stored as a XML document in the database.
 *
 * @throws PermissionDeniedException
 */
private void executeXProc(final DBBroker broker, final Txn transaction, final DocumentImpl resource, final HttpServletRequest request, final HttpServletResponse response, final Properties outputProperties, final String servletPath, final String pathInfo) throws XPathException, BadRequestException, PermissionDeniedException {
    final URLSource source = new URLSource(this.getClass().getResource("run-xproc.xq"));
    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.declareVariable("pipeline", resource.getURI().toString());
        final String stdin = request.getParameter("stdin");
        context.declareVariable("stdin", stdin == null ? "" : stdin);
        final String debug = request.getParameter("debug");
        context.declareVariable("debug", debug == null ? "0" : "1");
        final String bindings = request.getParameter("bindings");
        context.declareVariable("bindings", bindings == null ? "<bindings/>" : bindings);
        final String autobind = request.getParameter("autobind");
        context.declareVariable("autobind", autobind == null ? "0" : "1");
        final String options = request.getParameter("options");
        context.declareVariable("options", options == null ? "<options/>" : options);
        // TODO: don't hardcode this?
        context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(resource.getCollection().getURI()).toString());
        context.setStaticallyKnownDocuments(new XmldbURI[] { resource.getCollection().getURI() });
        final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
        reqw.setServletPath(servletPath);
        reqw.setPathInfo(pathInfo);
        final long compilationTime;
        if (compiled == null) {
            try {
                final long compilationStart = System.currentTimeMillis();
                compiled = xquery.compile(context, source);
                compilationTime = System.currentTimeMillis() - compilationStart;
            } catch (final IOException e) {
                throw new BadRequestException("Failed to read query from " + source.getURL(), e);
            }
        } else {
            compilationTime = 0;
        }
        try {
            final long executeStart = System.currentTimeMillis();
            final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
            writeResults(response, broker, transaction, result, -1, 1, false, outputProperties, false, compilationTime, System.currentTimeMillis() - executeStart);
        } finally {
            context.runCleanupTasks();
        }
    } finally {
        if (compiled != null) {
            pool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) URLSource(org.exist.source.URLSource) HttpRequestWrapper(org.exist.http.servlets.HttpRequestWrapper)

Example 2 with URLSource

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

the class DocUtils method getDocumentByPathFromURL.

private static Sequence getDocumentByPathFromURL(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
    try {
        final Source source = SourceFactory.getSource(context.getBroker(), "", path, false);
        if (source == null) {
            return Sequence.EMPTY_SEQUENCE;
        }
        try (final InputStream is = source.getInputStream()) {
            if (source instanceof URLSource) {
                final int responseCode = ((URLSource) source).getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
                    // Special case: '404'
                    return Sequence.EMPTY_SEQUENCE;
                } else if (responseCode != HttpURLConnection.HTTP_OK) {
                    throw new PermissionDeniedException("Server returned code " + responseCode);
                }
            }
            final org.exist.dom.memtree.DocumentImpl memtreeDoc = parse(context.getBroker().getBrokerPool(), context, is);
            memtreeDoc.setDocumentURI(path);
            return memtreeDoc;
        }
    } catch (final ConnectException e) {
        // prevent long stack traces
        throw new XPathException(e.getMessage() + " (" + path + ")");
    } catch (final MalformedURLException e) {
        throw new XPathException(e.getMessage(), e);
    } catch (final IOException e) {
        throw new XPathException("An error occurred while parsing " + path + ": " + e.getMessage(), e);
    }
}
Also used : URLSource(org.exist.source.URLSource) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) Source(org.exist.source.Source) InputSource(org.xml.sax.InputSource) URLSource(org.exist.source.URLSource)

Aggregations

URLSource (org.exist.source.URLSource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpRequestWrapper (org.exist.http.servlets.HttpRequestWrapper)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1 Source (org.exist.source.Source)1 XQueryPool (org.exist.storage.XQueryPool)1 XPathException (org.exist.xquery.XPathException)1 InputSource (org.xml.sax.InputSource)1