/** * Get the binary content of a datastream * * @param rangeValue the range value * @return Binary blob * @throws IOException if io exception occurred */ protected Response getBinaryContent(final String rangeValue) throws IOException { final FedoraBinary binary = (FedoraBinary) resource(); // we include an explicit etag, because the default behavior is to use the JCR node's etag, not // the jcr:content node digest. The etag is only included if we are not within a transaction. final String txId = TransactionServiceImpl.getCurrentTransactionId(session()); if (txId == null) { checkCacheControlHeaders(request, servletResponse, binary, session()); } final CacheControl cc = new CacheControl(); cc.setMaxAge(0); cc.setMustRevalidate(true); Response.ResponseBuilder builder; if (rangeValue != null && rangeValue.startsWith("bytes")) { final Range range = Range.convert(rangeValue); final long contentSize = binary.getContentSize(); final String endAsString; if (range.end() == -1) { endAsString = Long.toString(contentSize - 1); } else { endAsString = Long.toString(range.end()); } final String contentRangeValue = String.format("bytes %s-%s/%s", range.start(), endAsString, contentSize); if (range.end() > contentSize || (range.end() == -1 && range.start() > contentSize)) { builder = status(REQUESTED_RANGE_NOT_SATISFIABLE).header("Content-Range", contentRangeValue); } else { final RangeRequestInputStream rangeInputStream = new RangeRequestInputStream(binary.getContent(), range.start(), range.size()); builder = status(PARTIAL_CONTENT) .entity(rangeInputStream) .header("Content-Range", contentRangeValue); } } else { final InputStream content = binary.getContent(); builder = ok(content); } // we set the content-type explicitly to avoid content-negotiation from getting in the way return builder.type(binary.getMimeType()).cacheControl(cc).build(); }