private Response getBinaryContent ( final String rangeValue , final FedoraResource resource ) throws IOException { final FedoraBinary binary = ( FedoraBinary ) resource ; final CacheControl cc = new CacheControl ( ) ; cc . setMaxAge ( 0 ) ; cc . setMustRevalidate ( true ) ; final 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 { @ SuppressWarnings ( "resource" ) final RangeRequestInputStream rangeInputStream = new RangeRequestInputStream ( binary . getContent ( ) , range . start ( ) , range . size ( ) ) ; builder = status ( PARTIAL_CONTENT ) . entity ( rangeInputStream ) . header ( "Content-Range" , contentRangeValue ) . header ( CONTENT_LENGTH , range . size ( ) ) ; } } else { @ SuppressWarnings ( "resource" ) final InputStream content = binary . getContent ( ) ; builder = ok ( content ) ; } // we set the content-type explicitly to avoid content-negotiation from getting in the way // getBinaryResourceMediaType will try to use the mime type on the resource, falling back on // 'application/octet-stream' if the mime type is syntactically invalid return builder . type ( getBinaryResourceMediaType ( resource ) . toString ( ) ) . cacheControl ( cc ) . build ( ) ; }
Get the binary content of a datastream