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