Search in sources :

Example 1 with ReactiveHttpOutputMessage

use of org.springframework.http.ReactiveHttpOutputMessage in project spring-framework by spring-projects.

the class MultipartHttpMessageWriter method writeMultipart.

private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
    byte[] boundary = generateMultipartBoundary();
    mediaType = getMultipartMediaType(mediaType, boundary);
    outputMessage.getHeaders().setContentType(mediaType);
    LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)"));
    DataBufferFactory bufferFactory = outputMessage.bufferFactory();
    Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()).concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory)).concatWith(generateLastLine(boundary, bufferFactory)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
    if (logger.isDebugEnabled()) {
        body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return outputMessage.writeWith(body);
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Arrays(java.util.Arrays) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Hints(org.springframework.core.codec.Hints) ResolvableTypeProvider(org.springframework.core.ResolvableTypeProvider) Supplier(java.util.function.Supplier) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) LogFormatUtils(org.springframework.core.log.LogFormatUtils) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Map(java.util.Map) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) Resource(org.springframework.core.io.Resource) HttpHeaders(org.springframework.http.HttpHeaders) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) PooledDataBuffer(org.springframework.core.io.buffer.PooledDataBuffer) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Optional(java.util.Optional) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Assert(org.springframework.util.Assert) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) PooledDataBuffer(org.springframework.core.io.buffer.PooledDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with ReactiveHttpOutputMessage

use of org.springframework.http.ReactiveHttpOutputMessage in project spring-framework by spring-projects.

the class ResourceHttpMessageWriter method writeResource.

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);
        }
    }
    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);
        if (logger.isDebugEnabled()) {
            body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
        }
        return message.writeWith(body);
    });
}
Also used : HttpLogging(org.springframework.http.HttpLogging) ZeroCopyHttpOutputMessage(org.springframework.http.ZeroCopyHttpOutputMessage) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Hints(org.springframework.core.codec.Hints) ResourceRegion(org.springframework.core.io.support.ResourceRegion) ResourceRegionEncoder(org.springframework.core.codec.ResourceRegionEncoder) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) MediaTypeFactory(org.springframework.http.MediaTypeFactory) Map(java.util.Map) InputStreamResource(org.springframework.core.io.InputStreamResource) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ResourceDecoder(org.springframework.core.codec.ResourceDecoder) Resource(org.springframework.core.io.Resource) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ResourceEncoder(org.springframework.core.codec.ResourceEncoder) HttpHeaders(org.springframework.http.HttpHeaders) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) HttpRange(org.springframework.http.HttpRange) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) File(java.io.File) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) HttpHeaders(org.springframework.http.HttpHeaders) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 3 with ReactiveHttpOutputMessage

use of org.springframework.http.ReactiveHttpOutputMessage in project spring-framework by spring-projects.

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link org.springframework.web.reactive.function.client.WebClient WebClient} and
 * {@link org.springframework.web.reactive.function.server.ServerResponse ServerResponse}.
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementClass the class of values to be produced
 * @return the inserter to write a producer
 * @since 5.2
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementClass, "'elementClass' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.forClass(elementClass), adapter);
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Collectors(java.util.stream.Collectors) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 4 with ReactiveHttpOutputMessage

use of org.springframework.http.ReactiveHttpOutputMessage in project spring-framework by spring-projects.

the class BodyInserters method writeWithMessageWriters.

private static <M extends ReactiveHttpOutputMessage> Mono<Void> writeWithMessageWriters(M outputMessage, BodyInserter.Context context, Object body, ResolvableType bodyType, @Nullable ReactiveAdapter adapter) {
    Publisher<?> publisher;
    if (body instanceof Publisher) {
        publisher = (Publisher<?>) body;
    } else if (adapter != null) {
        publisher = adapter.toPublisher(body);
    } else {
        publisher = Mono.just(body);
    }
    MediaType mediaType = outputMessage.getHeaders().getContentType();
    return context.messageWriters().stream().filter(messageWriter -> messageWriter.canWrite(bodyType, mediaType)).findFirst().map(BodyInserters::cast).map(writer -> write(publisher, bodyType, mediaType, outputMessage, context, writer)).orElseGet(() -> Mono.error(unsupportedError(bodyType, context, mediaType)));
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Collectors(java.util.stream.Collectors) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) MultipartBodyBuilder(org.springframework.http.client.MultipartBodyBuilder) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Publisher(org.reactivestreams.Publisher)

Example 5 with ReactiveHttpOutputMessage

use of org.springframework.http.ReactiveHttpOutputMessage in project spring-framework by spring-projects.

the class BodyInsertersTests method ofProducerWithMono.

@Test
public void ofProducerWithMono() {
    Mono<User> body = Mono.just(new User("foo", "bar"));
    BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)15 Test (org.junit.jupiter.api.Test)9 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)9 List (java.util.List)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 Publisher (org.reactivestreams.Publisher)6 ResolvableType (org.springframework.core.ResolvableType)6 Resource (org.springframework.core.io.Resource)6 MediaType (org.springframework.http.MediaType)6 Nullable (org.springframework.lang.Nullable)6 Mono (reactor.core.publisher.Mono)6 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)5 Assert (org.springframework.util.Assert)5 MultiValueMap (org.springframework.util.MultiValueMap)5 Flux (reactor.core.publisher.Flux)5 Map (java.util.Map)4 HttpEntity (org.springframework.http.HttpEntity)4 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 Optional (java.util.Optional)3