質問

I building a REST-API with restlet to serve a mp3 file. But the File is created and served at the same time. More informations about that here.

It work perfectly but I only tested it in a desktop environment. When I fired up my iPad to test the API, it starts playing the File but after some seconds it stops, send a new Request and starts playing the File from the beginning.

After some researcher I find out that the iPad sends a Partial Request and therefore expects a partial response.

So I modified the Response-Header to fulfill the requirements.

I use curl to test the API :

curl -v -r 0-1 http://localhost:12345/api/path/file.mp3

Request-Header

GET /api/path/file.mp3 HTTP/1.1
Range: bytes=0-1
User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
Host: localhost:12345
Accept: */*

Response-Header

HTTP/1.1 206 Partial Content
Date: Tue, 29 Oct 2013 12:18:11 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.1.0
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Content-Length: 2
Content-Range: bytes 0-1/19601021
Content-Type: audio/mpeg; charset=UTF-8
Expires: Tue, 29 Oct 2013 12:18:07 GMT
Last-Modified: Tue, 29 Oct 2013 12:18:07 GMT 

Error-Message

But there is no Data that comes back from the server. Curl keeps the connection open to the server and the iPad sends a new Request. When I shut the server down curl gives me:

transfer closed with 1 bytes remaining to read
Closing connection #0
curl: (18) transfer closed with 1 bytes remaining to read

Code

And here is the code I use to return the data. As you can see, this method is just for testing and should always return 2 bytes.

private InputRepresentation rangeGetRequest() throws IOException {

    final byte[] bytes = new byte[2];
    bytes[0] = 68;
    bytes[1] = 68;

    final InputRepresentation inputRepresentation = new InputRepresentation(new ByteArrayInputStream(bytes), MediaType.AUDIO_MPEG);

    return inputRepresentation;

}

I have no idea what to do. I try to write my own InputStream that returns the 2 bytes, but without success. Or is the InputRepresentation not suitable for this area of application?

Thanks in advance

役に立ちましたか?

解決

Ok, I solved the problem, it was a stupid mistake.

To modify the response-header, I used

getResponse().setEntity(new StringRepresentation(" "));
getResponse().getEntity().setSize(19601021);
getResponse().getEntity().setRange(new Range(0, 2));
getResponse().getEntity().setModificationDate(new Date());
getResponse().getEntity().setExpirationDate(new Date());
getResponse().getEntity().setMediaType(MediaType.AUDIO_MPEG);

The first line was a relic from an old test. If I just use the actual InputRepresentation that holds the two bytes and it work just fine.

What do I learn from that, first tidy up your code, then ask questions ^^

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top