Search in sources :

Example 81 with Source

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

the class AuditTrailSessionListener method executeXQuery.

private void executeXQuery(String xqueryResourcePath) {
    if (xqueryResourcePath != null && xqueryResourcePath.length() > 0) {
        xqueryResourcePath = xqueryResourcePath.trim();
        try {
            final BrokerPool pool = BrokerPool.getInstance();
            final Subject sysSubject = pool.getSecurityManager().getSystemSubject();
            try (final DBBroker broker = pool.get(Optional.of(sysSubject))) {
                if (broker == null) {
                    LOG.error("Unable to retrieve DBBroker for {}", sysSubject.getName());
                    return;
                }
                final XmldbURI pathUri = XmldbURI.create(xqueryResourcePath);
                try (final LockedDocument lockedResource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
                    final Source source;
                    if (lockedResource != null) {
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Resource [{}] exists.", xqueryResourcePath);
                        }
                        source = new DBSource(broker, (BinaryDocument) lockedResource.getDocument(), true);
                    } else {
                        LOG.error("Resource [{}] does not exist.", xqueryResourcePath);
                        return;
                    }
                    final XQuery xquery = pool.getXQueryService();
                    if (xquery == null) {
                        LOG.error("broker unable to retrieve XQueryService");
                        return;
                    }
                    final XQueryPool xqpool = pool.getXQueryPool();
                    CompiledXQuery compiled = xqpool.borrowCompiledXQuery(broker, source);
                    final 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()));
                    if (compiled == null) {
                        compiled = xquery.compile(context, source);
                    } else {
                        compiled.getContext().updateContext(context);
                        context.getWatchDog().reset();
                    }
                    final Properties outputProperties = new Properties();
                    try {
                        final long startTime = System.currentTimeMillis();
                        final Sequence result = xquery.execute(broker, compiled, null, outputProperties);
                        final long queryTime = System.currentTimeMillis() - startTime;
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("XQuery execution results: {} in {}ms.", result.toString(), queryTime);
                        }
                    } finally {
                        context.runCleanupTasks();
                        xqpool.returnCompiledXQuery(source, compiled);
                    }
                }
            }
        } catch (final Exception e) {
            LOG.error("Exception while executing [{}] script", xqueryResourcePath, e);
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Properties(java.util.Properties) Subject(org.exist.security.Subject) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) LockedDocument(org.exist.dom.persistent.LockedDocument) DBSource(org.exist.source.DBSource) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 82 with Source

use of org.exist.source.Source 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)

Example 83 with Source

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

the class MetadataFunctions method exifToolWebExtract.

private Sequence exifToolWebExtract(final URI uri) throws XPathException {
    final ExiftoolModule module = (ExiftoolModule) getParentModule();
    try {
        final Process p = Runtime.getRuntime().exec(module.getExiftoolPath() + " -fast -X -");
        try (final InputStream stdIn = p.getInputStream();
            final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
            try (final OutputStream stdOut = p.getOutputStream()) {
                final Source src = SourceFactory.getSource(context.getBroker(), null, uri.toString(), false);
                if (src == null) {
                    throw new XPathException(this, "Could not read source for the Exiftool: " + uri.toString());
                }
                try (final InputStream isSrc = src.getInputStream()) {
                    // write the remote data to stdOut
                    int read = -1;
                    byte[] buf = new byte[4096];
                    while ((read = isSrc.read(buf)) > -1) {
                        stdOut.write(buf, 0, read);
                    }
                }
            }
            // read stdin to buffer
            baos.write(stdIn);
            // make sure process is complete
            p.waitFor();
            return ModuleUtils.inputSourceToXML(context, new InputSource(baos.toInputStream()));
        }
    } catch (final IOException | InterruptedException | PermissionDeniedException ex) {
        throw new XPathException(this, "Could not execute the Exiftool " + ex.getMessage(), ex);
    } catch (final SAXException saxe) {
        throw new XPathException(this, "Could not parse output from the Exiftool " + saxe.getMessage(), saxe);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Source(org.exist.source.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

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