public class ResponseChunks extends java.lang.Object implements Renderable
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.time.Duration;
import static ratpack.http.ResponseChunks.stringChunks;
import static org.junit.Assert.assertEquals;
public class Example {
public static void main(String[] args) throws Exception {
EmbeddedApp.fromHandler(ctx -> {
Publisher<String> strings = Streams.periodically(ctx, Duration.ofMillis(5),
i -> i < 5 ? i.toString() : null
);
ctx.render(stringChunks(strings));
}).test(httpClient -> {
assertEquals("01234", httpClient.getText());
});
}
}
Modifier and Type | Method and Description |
---|---|
static ResponseChunks |
bufferChunks(java.lang.CharSequence contentType,
Publisher<? extends ByteBuf> publisher)
Transmit each set of bytes emitted by the publisher as a chunk.
|
java.lang.CharSequence |
getContentType()
The intended value of the content-type header.
|
Publisher<? extends ByteBuf> |
publisher(ByteBufAllocator byteBufAllocator)
Returns the chunk publisher.
|
void |
render(Context context)
Render this object to the response.
|
static ResponseChunks |
stringChunks(java.lang.CharSequence contentType,
java.nio.charset.Charset charset,
Publisher<? extends java.lang.CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
static ResponseChunks |
stringChunks(java.lang.CharSequence contentType,
Publisher<? extends java.lang.CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
static ResponseChunks |
stringChunks(Publisher<? extends java.lang.CharSequence> publisher)
Transmit each string emitted by the publisher as a chunk.
|
public static ResponseChunks stringChunks(Publisher<? extends java.lang.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(java.lang.CharSequence contentType, Publisher<? extends java.lang.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(java.lang.CharSequence contentType, java.nio.charset.Charset charset, Publisher<? extends java.lang.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(java.lang.CharSequence contentType, 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 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 java.lang.CharSequence getContentType()
public void render(Context context) throws java.lang.Exception
render
in interface Renderable
context
- the request handling contextjava.lang.Exception
- any