Search in sources :

Example 16 with DBSource

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

the class XQueryContextAttributesTest method storeQuery.

private static DBSource storeQuery(final DBBroker broker, final Txn transaction, final XmldbURI uri, final InputSource source) throws IOException, PermissionDeniedException, SAXException, LockException, EXistException {
    try (final Collection collection = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
        broker.storeDocument(transaction, uri.lastSegment(), source, MimeType.XQUERY_TYPE, collection);
        final BinaryDocument doc = (BinaryDocument) collection.getDocument(broker, uri.lastSegment());
        return new DBSource(broker, doc, false);
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) Collection(org.exist.collections.Collection) DBSource(org.exist.source.DBSource)

Example 17 with DBSource

use of org.exist.source.DBSource 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 18 with DBSource

use of org.exist.source.DBSource 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 19 with DBSource

use of org.exist.source.DBSource 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 20 with DBSource

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

the class XQueryCompiler method compile.

public static CompiledXQuery compile(final DBBroker broker, final DocumentImpl document) throws RestXqServiceCompilationException {
    try {
        if (document instanceof BinaryDocument) {
            if (document.getMimeType().equals(XQUERY_MIME_TYPE)) {
                // compile the query
                final XQueryContext context = new XQueryContext(broker.getBrokerPool());
                final DBSource source = new DBSource(broker, (BinaryDocument) document, true);
                // set the module load path for any module imports that are relative
                context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI_PREFIX + source.getDocumentPath().removeLastSegment());
                return broker.getBrokerPool().getXQueryService().compile(context, source);
            } else {
                throw new RestXqServiceCompilationException("Invalid mimetype '" + document.getMimeType() + "' for XQuery: " + document.getURI().toString());
            }
        } else {
            throw new RestXqServiceCompilationException("Invalid document location for XQuery: " + document.getURI().toString());
        }
    } catch (XPathException xpe) {
        throw new RestXqServiceCompilationException("Unable to compile XQuery: " + document.getURI().toString() + ": " + xpe.getMessage(), xpe);
    } catch (IOException ioe) {
        throw new RestXqServiceCompilationException("Unable to access XQuery: " + document.getURI().toString() + ": " + ioe.getMessage(), ioe);
    } catch (PermissionDeniedException pde) {
        throw new RestXqServiceCompilationException("Permission to access XQuery denied: " + document.getURI().toString() + ": " + pde.getMessage(), pde);
    }
}
Also used : BinaryDocument(org.exist.dom.persistent.BinaryDocument) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException)

Aggregations

DBSource (org.exist.source.DBSource)20 Source (org.exist.source.Source)12 XmldbURI (org.exist.xmldb.XmldbURI)10 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 StringSource (org.exist.source.StringSource)8 InputSource (org.xml.sax.InputSource)8 IOException (java.io.IOException)7 BinaryDocument (org.exist.dom.persistent.BinaryDocument)6 LockedDocument (org.exist.dom.persistent.LockedDocument)5 Sequence (org.exist.xquery.value.Sequence)5 URISyntaxException (java.net.URISyntaxException)4 DocumentImpl (org.exist.dom.persistent.DocumentImpl)4 BrokerPool (org.exist.storage.BrokerPool)4 DBBroker (org.exist.storage.DBBroker)4 XQueryPool (org.exist.storage.XQueryPool)4 XQueryContext (org.exist.xquery.XQueryContext)4 Properties (java.util.Properties)3 EXistException (org.exist.EXistException)3 Subject (org.exist.security.Subject)3 FileSource (org.exist.source.FileSource)3