if (collection == null)
{throw new BadRequestException("Collection " + request.getPath() + " does not exist.");}
final XQuery xquery = broker.getXQueryService();
final XQueryContext context = xquery.newContext(AccessContext.REST);
context.setModuleLoadPath(getContext().getModuleLoadPath());
String contentType = request.getHeader("Content-Type");
String charset = getContext().getDefaultCharset();
MimeType mime = MimeType.XML_TYPE;
if (contentType != null) {
final int semicolon = contentType.indexOf(';');
if (semicolon > 0) {
contentType = contentType.substring(0, semicolon).trim();
}
mime = MimeTable.getInstance().getContentType(contentType);
final int equals = contentType.indexOf('=', semicolon);
if (equals > 0) {
final String param = contentType.substring(semicolon + 1, equals).trim();
if (param.compareToIgnoreCase("charset=") == 0) {
charset = param.substring(equals + 1).trim();
}
}
}
if (!mime.isXMLType() && !mime.equals(xqueryMimeType)) {
throw new BadRequestException(
"The xquery mime type is not an XML mime type nor application/xquery");
}
CompiledXQuery compiledQuery = null;
try {
final StringBuilder builder = new StringBuilder();
final Reader r = new InputStreamReader(request.getInputStream(), charset);
final char[] buffer = new char[4096];
int len;
long count = 0;
final long contentLength = request.getContentLength();
while ((len = r.read(buffer)) >= 0 && count < contentLength) {
count += len;
builder.append(buffer, 0, len);
}
compiledQuery = xquery.compile(context, new StringSource(builder.toString()));
} catch (final XPathException ex) {
throw new EXistException("Cannot compile xquery.", ex);
} catch (final IOException ex) {
throw new EXistException(
"I/O exception while compiling xquery.", ex);
}
context.setStaticallyKnownDocuments(
new XmldbURI[] {
XmldbURI.create(request.getPath()).append(AtomProtocol.FEED_DOCUMENT_NAME)
}
);