} else {
r = ResourceUtils.getAnonResource(conn, resource);
}
if (r == null || !ResourceUtils.isUsed(conn, r)) {
throw new HttpErrorException(Response.Status.NOT_FOUND, resource, "the requested resource could not be found in Marmotta right now, but may be available again in the future");
}
// create parser
final RDFFormat serializer = kiWiIOService.getSerializer(mimetype);
if (serializer == null) {
ImmutableMap<String, String> headers = ImmutableMap.of("Content-Type", ResourceWebServiceHelper.appendMetaTypes(kiWiIOService.getProducedTypes()));
throw new HttpErrorException(Status.NOT_ACCEPTABLE, resource, "mimetype can not be processed", headers);
}
final Resource subject = r;
StreamingOutput entity = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
// FIXME: This method is executed AFTER the @Transactional!
RDFWriter writer = Rio.createWriter(serializer, output);
try {
RepositoryConnection connection = sesameService.getConnection();
try {
connection.begin();
connection.exportStatements(subject, null, null, true, writer);
} finally {
connection.commit();
connection.close();
}
} catch (RepositoryException e) {
throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
} catch (RDFHandlerException e) {
throw new IOException("error while writing RDF data to stream", e);
}
}
};
// build response
Response response = Response.ok(entity).lastModified(ResourceUtils.getLastModified(conn, r)).build();
response.getMetadata().add("ETag", "W/\"" + ETagGenerator.getWeakETag(conn, r) + "\"");
if (!mimetype.contains("html")) { // then create a proper filename
String[] components;
if (resource.contains("#")) {
components = resource.split("#");
} else {
components = resource.split("/");
}
final String fileName = components[components.length - 1] + "." + serializer.getDefaultFileExtension();
response.getMetadata().add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
}
// create the Content-Type header for the response
if (mimetype.startsWith("text/") || mimetype.startsWith("application/")) {
response.getMetadata().add("Content-Type", mimetype + "; charset=" + ResourceWebService.CHARSET);
} else {
response.getMetadata().add("Content-Type", mimetype);
}
// create the Link headers for the response
List<String> links = new LinkedList<String>();
// build the link to the human readable content of this resource (if it exists)
String contentLink = ResourceWebServiceHelper.buildContentLink(r, contentService.getContentType(r), configurationService);
if (!"".equals(contentLink)) {
links.add(contentLink);
}
if (links.size() > 0) {
response.getMetadata().add("Link", CollectionUtils.fold(links, ", "));
}
return response;
} finally {
if (conn.isOpen()) {
conn.commit();
conn.close();
}
}
} catch (RepositoryException e) {
throw new HttpErrorException(Status.INTERNAL_SERVER_ERROR, resource, e.getMessage());
}
}