// TODO: handle post body
doQuery(broker, request, response, post);
} else if (allowQueryPost) {
final Collection collection = broker.getCollection(XmldbURI.create(request.getPath()));
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)
}
);
try {
final Sequence resultSequence = xquery.execute(compiledQuery, null);
if (resultSequence.isEmpty()) {
throw new BadRequestException("No topic was found.");
}
response.setStatusCode(200);
response.setContentType(Atom.MIME_TYPE + "; charset=" + charset);
final Serializer serializer = broker.getSerializer();
serializer.reset();
try {
final Writer w = new OutputStreamWriter(
response.getOutputStream(), charset);
final SAXSerializer sax = (SAXSerializer) SerializerPool
.getInstance().borrowObject(SAXSerializer.class);
final Properties outputProperties = new Properties();
sax.setOutput(w, outputProperties);
serializer.setProperties(outputProperties);
serializer.setSAXHandlers(sax, sax);
serializer.toSAX(resultSequence, 1, 1, false, false);
SerializerPool.getInstance().returnObject(sax);
w.flush();
w.close();
} catch (final IOException ex) {
LOG.fatal("Cannot read resource " + request.getPath(), ex);
throw new EXistException("I/O error on read of resource "
+ request.getPath(), ex);
} catch (final SAXException saxe) {
LOG.warn(saxe);
throw new BadRequestException(
"Error while serializing XML: " + saxe.getMessage());
}
resultSequence.itemAt(0);
} catch (final XPathException ex) {
throw new EXistException("Cannot execute xquery.", ex);