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 ratpack.handling.Handler; import ratpack.handling.Context; import ratpack.func.Function; import ratpack.stream.Streams; import ratpack.launch.HandlerFactory; import ratpack.launch.LaunchConfig; import ratpack.launch.LaunchConfigBuilder; import ratpack.test.embed.EmbeddedApplication; import ratpack.test.embed.LaunchConfigEmbeddedApplication; import ratpack.test.http.TestHttpClient; import ratpack.test.http.TestHttpClients; import static ratpack.http.ResponseChunks.stringChunks; import java.util.concurrent.TimeUnit; import java.util.concurrent.ScheduledExecutorService; import org.reactivestreams.Publisher; public class Example { private static EmbeddedApplication createApp() { return new LaunchConfigEmbeddedApplication() { protected LaunchConfig createLaunchConfig() { return LaunchConfigBuilder.noBaseDir().port(0).build(new HandlerFactory() { public Handler create(LaunchConfig launchConfig) { // Example of streaming chunks return new Handler() { public void handle(Context context) { // simulate streaming by periodically publishing ScheduledExecutorService executor = context.getLaunchConfig().getExecController().getExecutor(); Publisher<String> strings = Streams.periodically(executor, 5, TimeUnit.MILLISECONDS, new Function<Integer, String>() { public String apply(Integer i) { if (i.intValue() < 5) { return i.toString(); } else { return null; } } }); context.render(stringChunks(strings)); } }; } }); } }; } public static void main(String[] args) { try(EmbeddedApplication app = createApp()) { assert app.getHttpClient().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()