throws XPathException, BadRequestException, PermissionDeniedException {
final Source source = new DBSource(broker, (BinaryDocument) resource, true);
final XQuery xquery = broker.getXQueryService();
final XQueryPool pool = xquery.getXQueryPool();
XQueryContext context;
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
// special header to indicate that the query is not returned from
// cache
response.setHeader("X-XQuery-Cached", "false");
context = xquery.newContext(AccessContext.REST);
} else {
response.setHeader("X-XQuery-Cached", "true");
context = compiled.getContext();
}
// 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 " + resource.getURI(), e);
}
}
DebuggeeFactory.checkForDebugRequest(request, context);
boolean wrap = outputProperties.getProperty("_wrap") != null
&& "yes".equals(outputProperties.getProperty("_wrap"));
try {
final Sequence result = xquery.execute(compiled, null, outputProperties);
writeResults(response, broker, result, -1, 1, false, outputProperties, wrap);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
}