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);
}
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);
});
}
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);
}
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)));
}
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();
}
Aggregations