use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class EncoderHttpMessageWriter method write.
@Override
public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage, Map<String, Object> hints) {
HttpHeaders headers = outputMessage.getHeaders();
if (headers.getContentType() == null) {
MediaType fallback = this.defaultMediaType;
mediaType = useFallback(mediaType, fallback) ? fallback : mediaType;
if (mediaType != null) {
mediaType = addDefaultCharset(mediaType, fallback);
headers.setContentType(mediaType);
}
}
Flux<DataBuffer> body = this.encoder.encode(inputStream, outputMessage.bufferFactory(), elementType, headers.getContentType(), hints);
return (hints.get(FLUSHING_STRATEGY_HINT) == AFTER_EACH_ELEMENT ? outputMessage.writeAndFlushWith(body.map(Flux::just)) : outputMessage.writeWith(body));
}
use of org.springframework.core.io.buffer.DataBuffer 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 org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method decodePojoWithError.
@Test
public void decodePojoWithError() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\":}"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
StepVerifier.create(flux).verifyError(CodecException.class);
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method decodeToFlux.
@Test
public void decodeToFlux() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
StepVerifier.create(flux).expectNext(new Pojo("f1", "b1")).expectNext(new Pojo("f2", "b2")).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonEncoderTests method encode.
@Test
public void encode() throws Exception {
Flux<Pojo> source = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
ResolvableType type = ResolvableType.forClass(Pojo.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
StepVerifier.create(output).consumeNextWith(stringConsumer("[{\"foo\":\"foo\",\"bar\":\"bar\"},{\"foo\":\"foofoo\",\"bar\":\"barbar\"},{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]")).expectComplete().verify();
}
Aggregations