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);
}
}
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());
}
}
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;
}
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);
}
}
}
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);
}
}
Aggregations