Example usage for org.springframework.http ReactiveHttpOutputMessage writeWith

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

Introduction

In this page you can find the example usage for org.springframework.http ReactiveHttpOutputMessage 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.http.codec.EncoderHttpMessageWriter.java

@SuppressWarnings("unchecked")
@Override//from  w w w  . ja v a 2  s .  com
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);
        }/*www  .ja  v  a  2 s  .  c o m*/
    }

    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  ava  2s . c  om

    return message.writeWith(body);
}