public class ResponseChunks extends Object
renderable
object for streaming data with HTTP chunked transfer-encoding.
A renderer
for this type is implicitly provided by Ratpack core.
Example usage:
import org.reactivestreams.Publisher;
import ratpack.stream.Streams;
import ratpack.test.embed.EmbeddedApp;
import java.util.concurrent.TimeUnit;
import static ratpack.http.ResponseChunks.stringChunks;
public class Example {
public static void main(String[] args) {
EmbeddedApp.fromHandler(ctx -> {
Publisher<String> strings = Streams.periodically(ctx.getLaunchConfig(), 5, TimeUnit.MILLISECONDS,
i -> i < 5 ? i.toString() : null
);
ctx.render(stringChunks(strings));
}).test(httpClient -> {
assert httpClient.getText().equals("01234");
});
}
}
Modifier and Type | Method and Description |
---|---|
static ResponseChunks |
bufferChunks(CharSequence contentType,
org.reactivestreams.Publisher<? extends ByteBuf> publisher)
Transmit each set of bytes emitted by the publisher as a chunk.
|
CharSequence |
getContentType()
The intended value of the content-type header.
|
org.reactivestreams.Publisher<? extends ByteBuf> |
publisher(ByteBufAllocator byteBufAllocator)
Returns the chunk publisher.
|
static ResponseChunks |
stringChunks(CharSequence contentType,
Charset charset,
org.reactivestreams.Publisher<? extends CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
static ResponseChunks |
stringChunks(CharSequence contentType,
org.reactivestreams.Publisher<? extends CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
static ResponseChunks |
stringChunks(org.reactivestreams.Publisher<? extends CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
public static ResponseChunks stringChunks(org.reactivestreams.Publisher<? extends CharSequence> publisher)
The content type of the response is set to text/plain;charset=UTF-8
and each string is decoded as UTF-8.
publisher
- a publisher of stringspublic static ResponseChunks stringChunks(CharSequence contentType, org.reactivestreams.Publisher<? extends CharSequence> publisher)
The content type of the response is set to the given content type and each string is decoded as UTF-8.
contentType
- the value for the content-type headerpublisher
- a publisher of stringspublic static ResponseChunks stringChunks(CharSequence contentType, Charset charset, org.reactivestreams.Publisher<? extends CharSequence> publisher)
The content type of the response is set to the given content type and each string is decoded as the given charset.
contentType
- the value for the content-type headercharset
- the charset to use to decode each string chunkpublisher
- a publisher of stringspublic static ResponseChunks bufferChunks(CharSequence contentType, org.reactivestreams.Publisher<? extends ByteBuf> publisher)
The content type of the response is set to the given content type.
contentType
- the value for the content-type headerpublisher
- a publisher of byte bufferspublic org.reactivestreams.Publisher<? extends ByteBuf> publisher(ByteBufAllocator byteBufAllocator)
This method is called internally by the renderer for this type.
byteBufAllocator
- a byte buf allocator that can be used if necessary to allocate byte bufferspublic CharSequence getContentType()