use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class Jackson2JsonEncoder method encode.
@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
Assert.notNull(inputStream, "'inputStream' must not be null");
Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
Assert.notNull(elementType, "'elementType' must not be null");
if (inputStream instanceof Mono) {
return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
} else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
return Flux.from(inputStream).map(value -> {
DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
buffer.write(new byte[] { '\n' });
return buffer;
});
}
ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class FormHttpMessageWriter method write.
@Override
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = MediaType.APPLICATION_FORM_URLENCODED;
outputMessage.getHeaders().setContentType(contentType);
}
Charset charset = getMediaTypeCharset(contentType);
return Flux.from(inputStream).single().map(form -> generateForm(form, charset)).then(value -> {
ByteBuffer byteBuffer = charset.encode(value);
DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer);
outputMessage.getHeaders().setContentLength(byteBuffer.remaining());
return outputMessage.writeWith(Mono.just(buffer));
});
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method requestBodyError.
@Test
public void requestBodyError() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)).toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> {
assertThat(error, instanceOf(ServerWebInputException.class));
assertSame(EXCEPTION, error.getCause());
}).verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method controllerThrowsException.
@Test
public void controllerThrowsException() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/raise-exception").toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> assertSame(EXCEPTION, error)).verify();
}
use of reactor.core.publisher.Mono in project spring-framework by spring-projects.
the class BodyExtractorsTests method toFormData.
@Test
public void toFormData() throws Exception {
BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8)));
Flux<DataBuffer> body = Flux.just(dataBuffer);
MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
StepVerifier.create(result).consumeNextWith(form -> {
assertEquals("Invalid result", 3, form.size());
assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
List<String> values = form.get("name 2");
assertEquals("Invalid result", 2, values.size());
assertEquals("Invalid result", "value 2+1", values.get(0));
assertEquals("Invalid result", "value 2+2", values.get(1));
assertNull("Invalid result", form.getFirst("name 3"));
}).expectComplete().verify();
}
Aggregations