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

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

Introduction

In this page you can find the example usage for org.springframework.http.server.reactive ServerHttpResponse writeWith.

Prototype


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

Source Link

Document

Use the given Publisher to write the body of the message to the underlying HTTP layer.

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();
        }//from  ww w .j ava2s. 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));
    }));
}

From source file:org.springframework.cloud.gateway.filter.WebClientWriteResponseFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added
    // until the WebHandler is run
    return chain.filter(exchange).then(Mono.defer(() -> {
        ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR);
        if (clientResponse == null) {
            return Mono.empty();
        }//from w w w  .  j a v a  2  s  .co m
        log.trace("WebClientWriteResponseFilter start");
        ServerHttpResponse response = exchange.getResponse();

        return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers()))
                .log("webClient response");
    }));
}

From source file:org.springframework.cloud.gateway.filter.WriteResponseFilter.java

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
    // NOTICE: nothing in "pre" filter stage as CLIENT_RESPONSE_ATTR is not added
    // until the WebHandler is run
    return chain.filter(exchange).then(() -> {
        HttpClientResponse clientResponse = getAttribute(exchange, CLIENT_RESPONSE_ATTR,
                HttpClientResponse.class);
        if (clientResponse == null) {
            return Mono.empty();
        }// w  w w. j  av  a 2s .  c om
        log.trace("WriteResponseFilter start");
        ServerHttpResponse response = exchange.getResponse();

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

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

        return response.writeWith(body);
    });
}