final HttpServletRequest request, final HttpServletResponse response,
final Properties outputProperties, final String servletPath, final String pathInfo)
throws XPathException, BadRequestException, PermissionDeniedException {
final URLSource source = new URLSource(this.getClass().getResource("run-xproc.xq"));
final XQuery xquery = broker.getXQueryService();
final XQueryPool pool = xquery.getXQueryPool();
XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = xquery.newContext(AccessContext.REST);
} else {
context = compiled.getContext();
}
context.declareVariable("pipeline", resource.getURI().toString());
final String stdin = request.getParameter("stdin");
context.declareVariable("stdin", stdin == null ? "" : stdin);
final String debug = request.getParameter("debug");
context.declareVariable("debug", debug == null ? "0" : "1");
final String bindings = request.getParameter("bindings");
context.declareVariable("bindings", bindings == null ? "<bindings/>" : bindings);
final String autobind = request.getParameter("autobind");
context.declareVariable("autobind", autobind == null ? "0" : "1");
final String options = request.getParameter("options");
context.declareVariable("options", options == null ? "<options/>" : options);
// TODO: don't hardcode this?
context.setModuleLoadPath(
XmldbURI.EMBEDDED_SERVER_URI.append(
resource.getCollection().getURI()).toString());
context.setStaticallyKnownDocuments(
new XmldbURI[]{resource.getCollection().getURI()});
final HttpRequestWrapper reqw = declareVariables(context, null, request, response);
reqw.setServletPath(servletPath);
reqw.setPathInfo(pathInfo);
if (compiled == null) {
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new BadRequestException("Failed to read query from "
+ source.getURL(), e);
}
}
try {
final Sequence result = xquery.execute(compiled, null, outputProperties);
writeResults(response, broker, result, -1, 1, false, outputProperties, false);
} finally {
pool.returnCompiledXQuery(source, compiled);
}
}