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

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

Introduction

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

Prototype

DataBufferFactory bufferFactory();

Source Link

Document

Return a DataBufferFactory that can be used to create the body.

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();
        }/* ww w. jav a 2 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));
    }));
}

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 .jav  a 2  s  . co  m*/
        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);
    });
}