Example usage for org.springframework.http ReactiveHttpOutputMessage bufferFactory

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

Introduction

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

Prototype

DataBufferFactory bufferFactory();

Source Link

Document

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

Usage

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

@SuppressWarnings("unchecked")
@Override//w w  w  . ja  v a  2  s  .  co  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));
}

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

private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType,
        ReactiveHttpOutputMessage message, Map<String, Object> hints) {

    HttpHeaders headers = message.getHeaders();
    MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);
    headers.setContentType(resourceMediaType);

    if (headers.getContentLength() < 0) {
        long length = lengthOf(resource);
        if (length != -1) {
            headers.setContentLength(length);
        }/*from  w  w  w. j  av a  2 s . c  om*/
    }

    return zeroCopy(resource, null, message, hints).orElseGet(() -> {
        Mono<Resource> input = Mono.just(resource);
        DataBufferFactory factory = message.bufferFactory();
        Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints);
        return message.writeWith(body);
    });
}

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

private Mono<Void> encodeAndWriteRegions(Publisher<? extends ResourceRegion> publisher,
        @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {

    Flux<DataBuffer> body = this.regionEncoder.encode(publisher, message.bufferFactory(), REGION_TYPE,
            mediaType, hints);/*from   w  w  w  . j  a va 2  s .co  m*/

    return message.writeWith(body);
}