Search in sources :

Example 46 with DataBuffer

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));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 47 with DataBuffer

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));
    });
}
Also used : Iterator(java.util.Iterator) 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) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) URLEncoder(java.net.URLEncoder) List(java.util.List) Charset(java.nio.charset.Charset) Map(java.util.Map) ResolvableType(org.springframework.core.ResolvableType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) ByteBuffer(java.nio.ByteBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 48 with DataBuffer

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 49 with DataBuffer

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();
}
Also used : Pojo(org.springframework.http.codec.Pojo) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 50 with DataBuffer

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();
}
Also used : Pojo(org.springframework.http.codec.Pojo) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Aggregations

DataBuffer (org.springframework.core.io.buffer.DataBuffer)70 Test (org.junit.Test)44 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)21 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)17 ResolvableType (org.springframework.core.ResolvableType)15 ByteBuffer (java.nio.ByteBuffer)11 Flux (reactor.core.publisher.Flux)11 Mono (reactor.core.publisher.Mono)11 List (java.util.List)9 Publisher (org.reactivestreams.Publisher)9 HttpHeaders (org.springframework.http.HttpHeaders)9 MediaType (org.springframework.http.MediaType)8 DecoderHttpMessageReader (org.springframework.http.codec.DecoderHttpMessageReader)8 HttpMessageReader (org.springframework.http.codec.HttpMessageReader)8 Pojo (org.springframework.http.codec.Pojo)8 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)8 StringDecoder (org.springframework.core.codec.StringDecoder)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)6