/jaxrs-range-filter

HTTP Ranged Requests for JAX-RS.

Primary LanguageJavaApache License 2.0Apache-2.0

HTTP Ranged Requests for JAX-RS

jaxrs range filter jaxrs range filter total jaxrs range filter jaxrs range filter jaxrs range filter

Example usage

<dependency>
  <groupId>to.lova.jaxrs</groupId>
  <artifactId>jaxrs-range-filter</artifactId>
  <version>1.0.0</version>
</dependency>
import java.util.Collections;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("app")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return Collections.singleton(RangeResponseFilter.class);
    }

}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("alphabet")
public class AlphabetResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String alphabet() {
        return "abcdefghijklmnopqrstuvwxyz";
    }

}

Then JAX-RS will serve byte ranges whenever a HTTP Range header is present on the request, e.g.

$ curl -i http://localhost:8080/app/alphabet
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 26

abcdefghijklmnopqrstuvwxyz
$ curl -i http://localhost:8080/app/alphabet -H "Range: bytes=6-10"
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Range: bytes 6-10/26
Content-Type: text/plain
Content-Length: 5

ghijk
$ curl -i http://localhost:8080/app/alphabet -H "Range: bytes=6-10,18-"
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=VXTwimfDqIB3LRZ2jjQ8vxbSvnGAB2sMn3UVq
Content-Length: 257

--VXTwimfDqIB3LRZ2jjQ8vxbSvnGAB2sMn3UVq
Content-Type: text/plain
Content-Range: bytes 6-10/26

ghijk
--VXTwimfDqIB3LRZ2jjQ8vxbSvnGAB2sMn3UVq
Content-Type: text/plain
Content-Range: bytes 18-26/26

stuvwxyz
--VXTwimfDqIB3LRZ2jjQ8vxbSvnGAB2sMn3UVq--