Search in sources :

Example 66 with Subject

use of org.exist.security.Subject 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)

Aggregations

Subject (org.exist.security.Subject)66 DBBroker (org.exist.storage.DBBroker)22 Test (org.junit.Test)18 EXistException (org.exist.EXistException)17 XmldbURI (org.exist.xmldb.XmldbURI)15 AuthenticationException (org.exist.security.AuthenticationException)14 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 Collection (org.exist.collections.Collection)12 SecurityManager (org.exist.security.SecurityManager)12 Permission (org.exist.security.Permission)10 ServletException (javax.servlet.ServletException)9 BrokerPool (org.exist.storage.BrokerPool)9 Txn (org.exist.storage.txn.Txn)9 Sequence (org.exist.xquery.value.Sequence)9 Properties (java.util.Properties)8 Descriptor (org.exist.http.Descriptor)8 XPathException (org.exist.xquery.XPathException)8 IOException (java.io.IOException)6 SAXException (org.xml.sax.SAXException)5 BadRequestException (org.exist.http.BadRequestException)4