Package ratpack.http

Class ResponseChunks

  • All Implemented Interfaces:
    Renderable

    public class ResponseChunks
    extends java.lang.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.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());
         });
       }
     }
     
    See Also:
    Response.sendStream(org.reactivestreams.Publisher), Wikipedia - Chunked transfer encoding
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ResponseChunks bufferChunks​(java.lang.CharSequence contentType, Publisher<? extends io.netty.buffer.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 io.netty.buffer.ByteBuf> publisher​(io.netty.buffer.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • stringChunks

        public static ResponseChunks stringChunks​(Publisher<? extends java.lang.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​(java.lang.CharSequence contentType,
                                                  Publisher<? extends java.lang.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​(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.

        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​(java.lang.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 java.lang.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 java.lang.Exception
        Render this object to the response.
        Specified by:
        render in interface Renderable
        Parameters:
        context - the request handling context
        Throws:
        java.lang.Exception - any