Example usage for org.springframework.http ReactiveHttpOutputMessage writeAndFlushWith

List of usage examples for org.springframework.http ReactiveHttpOutputMessage writeAndFlushWith

Introduction

In this page you can find the example usage for org.springframework.http ReactiveHttpOutputMessage writeAndFlushWith.

Prototype

Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body);

Source Link

Document

Use the given Publisher of Publishers to write the body of the HttpOutputMessage to the underlying HTTP layer, flushing after each Publisher<DataBuffer> .

Usage

From source file:org.springframework.http.codec.EncoderHttpMessageWriter.java

@SuppressWarnings("unchecked")
@Override//from  w w  w.j ava2  s  .c  o m
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType,
        @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {

    MediaType contentType = updateContentType(message, mediaType);

    Flux<DataBuffer> body = this.encoder.encode(inputStream, message.bufferFactory(), elementType, contentType,
            hints);

    if (inputStream instanceof Mono) {
        HttpHeaders headers = message.getHeaders();
        if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
            return Mono.from(body).flatMap(dataBuffer -> {
                headers.setContentLength(dataBuffer.readableByteCount());
                return message.writeWith(Mono.just(dataBuffer));
            });
        }
    }

    return (isStreamingMediaType(contentType) ? message.writeAndFlushWith(body.map(Flux::just))
            : message.writeWith(body));
}