}
public void getEntryById(DBBroker broker, String path, String id,
OutgoingMessage response) throws EXistException,
BadRequestException, PermissionDeniedException {
final XQuery xquery = broker.getXQueryService();
CompiledXQuery feedQuery = xquery.getXQueryPool().borrowCompiledXQuery(
broker, entryByIdSource);
XQueryContext context;
if (feedQuery == null) {
context = xquery.newContext(AccessContext.REST);
try {
feedQuery = xquery.compile(context, entryByIdSource);
} catch (final XPathException ex) {
throw new EXistException("Cannot compile xquery "
+ entryByIdSource.getURL(), ex);
} catch (final IOException ex) {
throw new EXistException(
"I/O exception while compiling xquery "
+ entryByIdSource.getURL(), ex);
}
} else {
context = feedQuery.getContext();
}
context.setStaticallyKnownDocuments(new XmldbURI[] { XmldbURI.create(
path).append(AtomProtocol.FEED_DOCUMENT_NAME) });
try {
context.declareVariable("id", id);
final Sequence resultSequence = xquery.execute(feedQuery, null);
if (resultSequence.isEmpty()) {
throw new BadRequestException("No topic was found.");
}
final String charset = getContext().getDefaultCharset();
response.setContentType("application/atom+xml; 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 " + path, ex);
throw new EXistException("I/O error on read of resource "
+ path, 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 "
+ entryByIdSource.getURL(), ex);
} finally {
xquery.getXQueryPool().returnCompiledXQuery(entryByIdSource, feedQuery);
}
}