xp-framework/xp-framework — pull request #261 by thekid (2013-03-25 11:49:07+00:00)
Title: Rest download

This pull request adds the ability to easily create file downloads in REST methods. Currently, a hack like this is necessary:

```groovy
[@webmethod(verb = 'GET')]
public Response downloadFile() {
$target= ...

// FIXME: The REST API should support return streams. Until that is available,
// hack up the RestFormat enum and create a serializer which will simply read
// the stream contents and return them in one big chunk to the client!
$mime= util.MimeType::getByFileName($target.getURI());
RestFormat::$UNKNOWN= new RestFormat(
0,
'UNKNOWN',
new RestSerializer() {
public string contentType() { return $mime; }
public string serialize(var $payload) { return io.streams.Streams::readAll($payload.value); }
},
null
);
return Response::ok()
.withHeader('Content-Type', $mime)
.withHeader('Content-Length', $target.getSize())
.withPayload($target.getInputStream())
;
}
```