Class ResponseChunks

  • All Implemented Interfaces:
    Renderable

    public class ResponseChunks
    extends Object
    implements Renderable
    A 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.exec.stream.Streams;
     import ratpack.test.embed.EmbeddedApp;
    
     import java.time.Duration;
    
     import static ratpack.core.http.ResponseChunks.stringChunks;
    
     import static org.junit.jupiter.api.Assertions.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());
         });
       }
     }
     
    See Also:
    Response.sendStream(org.reactivestreams.Publisher), Wikipedia - Chunked transfer encoding
    • Method Detail

      • stringChunks

        public static ResponseChunks stringChunks​(Publisher<? extends CharSequence> publisher)
        Transmit each string emitted by the publisher as a chunk.

        The content type of the response is set to text/plain;charset=UTF-8 and each string is decoded as UTF-8.

        Parameters:
        publisher - a publisher of strings
        Returns:
        a renderable object
      • stringChunks

        public static ResponseChunks stringChunks​(CharSequence contentType,
                                                  Publisher<? extends CharSequence> publisher)
        Transmit each string emitted by the publisher as a chunk.

        The content type of the response is set to the given content type and each string is decoded as UTF-8.

        Parameters:
        contentType - the value for the content-type header
        publisher - a publisher of strings
        Returns:
        a renderable object
      • stringChunks

        public static ResponseChunks stringChunks​(CharSequence contentType,
                                                  Charset charset,
                                                  Publisher<? extends CharSequence> publisher)
        Transmit each string emitted by the publisher as a chunk.

        The content type of the response is set to the given content type and each string is decoded as the given charset.

        Parameters:
        contentType - the value for the content-type header
        charset - the charset to use to decode each string chunk
        publisher - a publisher of strings
        Returns:
        a renderable object
      • bufferChunks

        public static ResponseChunks bufferChunks​(CharSequence contentType,
                                                  Publisher<? extends io.netty.buffer.ByteBuf> publisher)
        Transmit each set of bytes emitted by the publisher as a chunk.

        The content type of the response is set to the given content type.

        Parameters:
        contentType - the value for the content-type header
        publisher - a publisher of byte buffers
        Returns:
        a renderable object
      • publisher

        public Publisher<? extends io.netty.buffer.ByteBuf> publisher​(io.netty.buffer.ByteBufAllocator byteBufAllocator)
        Returns the chunk publisher.

        This method is called internally by the renderer for this type.

        Parameters:
        byteBufAllocator - a byte buf allocator that can be used if necessary to allocate byte buffers
        Returns:
        a publisher of byte buffers
      • getContentType

        public CharSequence getContentType()
        The intended value of the content-type header.
        Returns:
        the intended value of the content-type header.
      • render

        public void render​(Context context)
                    throws Exception
        Render this object to the response.
        Specified by:
        render in interface Renderable
        Parameters:
        context - the request handling context
        Throws:
        Exception - any