Example usage for org.springframework.http.server.reactive ServerHttpResponse writeAndFlushWith

List of usage examples for org.springframework.http.server.reactive ServerHttpResponse writeAndFlushWith

Introduction

In this page you can find the example usage for org.springframework.http.server.reactive ServerHttpResponse 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.cloud.gateway.filter.NettyWriteResponseFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_CONN_ATTR is not added
    // until the NettyRoutingFilter is run
    return chain.filter(exchange).then(Mono.defer(() -> {
        Connection connection = exchange.getAttribute(CLIENT_RESPONSE_CONN_ATTR);

        if (connection == null) {
            return Mono.empty();
        }/*w w  w.  ja v a2  s. c  o  m*/
        log.trace("NettyWriteResponseFilter start");
        ServerHttpResponse response = exchange.getResponse();

        NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();
        // TODO: what if it's not netty

        final Flux<NettyDataBuffer> body = connection.inbound().receive().retain() // TODO:
                // needed?
                .map(factory::wrap);

        MediaType contentType = null;
        try {
            contentType = response.getHeaders().getContentType();
        } catch (Exception e) {
            log.trace("invalid media type", e);
        }
        return (isStreamingMediaType(contentType) ? response.writeAndFlushWith(body.map(Flux::just))
                : response.writeWith(body));
    }));
}