/** * Transmit each set of bytes emitted by the publisher as a chunk. * <p> * The content type of the response is set to the given content type. * * @param contentType the value for the content-type header * @param publisher a publisher of byte buffers * @return a renderable object */ public static ResponseChunks bufferChunks(CharSequence contentType, Publisher<? extends ByteBuf> publisher) { return new ResponseChunks(contentType, byteBufAllocator -> publisher); }
/** * Transmit each string emitted by the publisher as a chunk. * <p> * The content type of the response is set to {@code text/plain;charset=UTF-8} and each string is decoded as UTF-8. * * @param publisher a publisher of strings * @return a renderable object */ public static ResponseChunks stringChunks(Publisher<? extends CharSequence> publisher) { return stringChunks(HttpHeaderConstants.PLAIN_TEXT_UTF8, CharsetUtil.UTF_8, publisher); }
return ResponseChunks.bufferChunks(HttpHeaderConstants.JSON, Streams.streamMap(stream, (s, out) -> { JsonGenerator generator = objectWriter.getFactory().createGenerator(new OutputStream() { @Override
/** * {@inheritDoc} */ @Override public void render(Context context) throws Exception { Response response = context.getResponse(); response.getHeaders().add(HttpHeaderConstants.TRANSFER_ENCODING, HttpHeaderConstants.CHUNKED); response.getHeaders().set(HttpHeaderConstants.CONTENT_TYPE, getContentType()); Publisher<? extends ByteBuf> publisher = publisher(context.get(ByteBufAllocator.class)); response.sendStream(publisher); }
return ResponseChunks.bufferChunks(HttpHeaderConstants.JSON, Streams.streamMap(stream, out -> { JsonGenerator generator = objectWriter.getFactory().createGenerator(new OutputStream() { @Override
/** * Transmit each string emitted by the publisher as a chunk. * <p> * The content type of the response is set to the given content type and each string is decoded as UTF-8. * * @param contentType the value for the content-type header * @param publisher a publisher of strings * @return a renderable object */ public static ResponseChunks stringChunks(CharSequence contentType, Publisher<? extends CharSequence> publisher) { return stringChunks(contentType, CharsetUtil.UTF_8, publisher); }
/** * Transmit each string emitted by the publisher as a chunk. * <p> * The content type of the response is set to the given content type and each string is decoded as the given charset. * * @param contentType the value for the content-type header * @param charset the charset to use to decode each string chunk * @param publisher a publisher of strings * @return a renderable object */ public static ResponseChunks stringChunks(CharSequence contentType, Charset charset, Publisher<? extends CharSequence> publisher) { return new ResponseChunks(contentType, allocator -> Streams.map(publisher, charSequence -> ByteBufUtil.encodeString(allocator, CharBuffer.wrap(charSequence), charset) ) ); }