}
final DocumentImpl feedDoc = collection.getDocument(broker,
FEED_DOCUMENT_URI);
if (feedDoc == null) {
throw new BadRequestException("Collection "
+ request.getPath() + " is not an Atom feed.");
}
// Return the collection feed
// String charset = getContext().getDefaultCharset();
if (returnContent) {
if (id == null) {
response.setStatusCode(200);
getFeed(broker, request.getPath(), response);
} else {
response.setStatusCode(200);
getEntryById(broker, request.getPath(), id,
response);
}
} else {
response.setStatusCode(204);
}
} else {
throw new NotFoundException("Resource " + request.getPath()
+ " not found");
}
} else {
// Do we have permission to read the resource
if (!resource.getPermissions().validate(broker.getSubject(),
Permission.READ)) {
throw new PermissionDeniedException(
"Not allowed to read resource");
}
if (returnContent) {
response.setStatusCode(200);
if (resource.getResourceType() == DocumentImpl.BINARY_FILE) {
response.setContentType(resource.getMetadata()
.getMimeType());
try {
final OutputStream os = response.getOutputStream();
broker.readBinaryResource(
(BinaryDocument) resource, os);
os.flush();
} 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);
}
} else {
// xml resource
final Serializer serializer = broker.getSerializer();
serializer.reset();
final String charset = getContext().getDefaultCharset();
// Serialize the document
try {
response.setContentType(resource.getMetadata()
.getMimeType() + "; charset=" + charset);
final Writer w = new OutputStreamWriter(
response.getOutputStream(), charset);
serializer.serialize(resource, w);
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());
}
}