On Sat, Jan 3, 2015 at 11:22 AM, Tim Dudgeon <tdudgeon...@gmail.com> wrote:
> On 03/01/2015 09:35, Henryk Konsek wrote:
>>
>> Hi Tim,
>>
>>> Is there a way to stream output using the Rest DSL?
>>
>> I used the following to download stream of bytes from the REST API:
>>
>> rest("/api").
>>
>> get("/download").bindingMode(RestBindingMode.off).produces('application/octet-stream').route().
>>          setBody().constant(new ByteArrayOutputStream("foo".getBytes()));.
>>
>> So I guess that setting OutputStream as the response body should do the
>> trick.
>>
>> Cheers.
>
> Henryk, thanks for the suggestion.
> So the problem I see with that is that
> 1. the complete byte array has to be held in memory so it doesn't scale
> 2. the first byte isn't sent until the last one is generated so you don't
> get an immediate response
>

That is just because of this example using a byte array. But you
should be able to use a FileInputStream or other stream that reads in
chunks.

For 2 I guess it depends on the component in use what it does and how
big its buffers are. I guess you can tweak those to be higher/lower to
force it to write the first bytes sooner.

Also I guess it may depend on whether you use chunked http mode as well.


> I was thinking you need to somehow get a "forward reference" to an
> OutputStream to write to. I tried setting a javax.ws.rs.core.StreamingOutput
> in the hope that this would be handled, but it wasn't (just got the
> toString() representation).
>
> Also, I don't understand the .constant() bit. The value is not a constant,
> so I don't see why that's needed.
>
> Tim
>
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cib...@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Reply via email to