Search in sources :

Example 1 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class MultipartHttpMessageWriter method encodePart.

@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory) {
    MultipartHttpOutputMessage message = new MultipartHttpOutputMessage(factory);
    HttpHeaders headers = message.getHeaders();
    T body;
    ResolvableType resolvableType = null;
    if (value instanceof HttpEntity) {
        HttpEntity<T> httpEntity = (HttpEntity<T>) value;
        headers.putAll(httpEntity.getHeaders());
        body = httpEntity.getBody();
        Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
        if (httpEntity instanceof ResolvableTypeProvider) {
            resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType();
        }
    } else {
        body = value;
    }
    if (resolvableType == null) {
        resolvableType = ResolvableType.fromClass(body.getClass());
    }
    if (!headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
        if (body instanceof Resource) {
            headers.setContentDispositionFormData(name, ((Resource) body).getName());
        } else if (resolvableType.resolve() == Resource.class) {
            body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> headers.setContentDispositionFormData(name, ((Resource) o).getName()));
        } else {
            headers.setContentDispositionFormData(name, null);
        }
    }
    MediaType contentType = headers.getContentType();
    final ResolvableType finalBodyType = resolvableType;
    Optional<HttpMessageWriter<?>> writer = this.partWriters.stream().filter(partWriter -> partWriter.canWrite(finalBodyType, contentType)).findFirst();
    if (writer.isEmpty()) {
        return Flux.error(new CodecException("No suitable writer found for part: " + name));
    }
    Publisher<T> bodyPublisher = body instanceof Publisher ? (Publisher<T>) body : Mono.just(body);
    // The writer will call MultipartHttpOutputMessage#write which doesn't actually write
    // but only stores the body Flux and returns Mono.empty().
    Mono<Void> partContentReady = ((HttpMessageWriter<T>) writer.get()).write(bodyPublisher, resolvableType, contentType, message, DEFAULT_HINTS);
    // After partContentReady, we can access the part content from MultipartHttpOutputMessage
    // and use it for writing to the actual request body
    Flux<DataBuffer> partContent = partContentReady.thenMany(Flux.defer(message::getBody));
    return Flux.concat(generateBoundaryLine(boundary, factory), partContent, generateNewLine(factory));
}
Also used : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) HttpHeaders(cn.taketoday.http.HttpHeaders) HttpEntity(cn.taketoday.http.HttpEntity) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Resource(cn.taketoday.core.io.Resource) Publisher(org.reactivestreams.Publisher) MediaType(cn.taketoday.http.MediaType) CodecException(cn.taketoday.core.codec.CodecException) ResolvableType(cn.taketoday.core.ResolvableType) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 2 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ServerCodecConfigurerTests method jackson2EncoderOverride.

@Test
public void jackson2EncoderOverride() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
    this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder).isSameAs(decoder);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder).isSameAs(encoder);
    assertThat(findCodec(writers, ServerSentEventHttpMessageWriter.class).getEncoder()).isSameAs(encoder);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) DefaultPartHttpMessageReader(cn.taketoday.http.codec.multipart.DefaultPartHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) MultipartHttpMessageReader(cn.taketoday.http.codec.multipart.MultipartHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) PartEventHttpMessageReader(cn.taketoday.http.codec.multipart.PartEventHttpMessageReader) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) PartHttpMessageWriter(cn.taketoday.http.codec.multipart.PartHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 3 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method jackson2CodecCustomization.

@Test
public void jackson2CodecCustomization() {
    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    this.configurer.defaultCodecs().jackson2JsonDecoder(decoder);
    this.configurer.defaultCodecs().jackson2JsonEncoder(encoder);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder).isSameAs(decoder);
    assertThat(findCodec(readers, ServerSentEventHttpMessageReader.class).getDecoder()).isSameAs(decoder);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder).isSameAs(encoder);
    MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
    actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
    assertThat(actualEncoder).isSameAs(encoder);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method objectMapperCustomization.

@Test
public void objectMapperCustomization() {
    ObjectMapper objectMapper = new ObjectMapper();
    this.configurer.defaultCodecs().configureDefaultCodec(codec -> {
        if (codec instanceof Jackson2CodecSupport) {
            ((Jackson2CodecSupport) codec).setObjectMapper(objectMapper);
        }
    });
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    Jackson2JsonDecoder actualDecoder = findCodec(readers, Jackson2JsonDecoder.class);
    assertThat(actualDecoder.getObjectMapper()).isSameAs(objectMapper);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    Jackson2JsonEncoder actualEncoder = findCodec(writers, Jackson2JsonEncoder.class);
    assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);
    MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
    actualEncoder = findCodec(multipartWriter.getPartWriters(), Jackson2JsonEncoder.class);
    assertThat(actualEncoder.getObjectMapper()).isSameAs(objectMapper);
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2CodecSupport(cn.taketoday.http.codec.json.Jackson2CodecSupport) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 5 with HttpMessageWriter

use of cn.taketoday.http.codec.HttpMessageWriter in project today-infrastructure by TAKETODAY.

the class ClientCodecConfigurerTests method enableLoggingRequestDetails.

@Test
public void enableLoggingRequestDetails() {
    this.configurer.defaultCodecs().enableLoggingRequestDetails(true);
    List<HttpMessageWriter<?>> writers = this.configurer.getWriters();
    MultipartHttpMessageWriter multipartWriter = findCodec(writers, MultipartHttpMessageWriter.class);
    assertThat(multipartWriter.isEnableLoggingRequestDetails()).isTrue();
    FormHttpMessageWriter formWriter = (FormHttpMessageWriter) multipartWriter.getFormWriter();
    assertThat(formWriter).isNotNull();
    assertThat(formWriter.isEnableLoggingRequestDetails()).isTrue();
}
Also used : MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) PartEventHttpMessageWriter(cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) Test(org.junit.jupiter.api.Test)

Aggregations

EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)28 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)28 Test (org.junit.jupiter.api.Test)22 ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)20 FormHttpMessageWriter (cn.taketoday.http.codec.FormHttpMessageWriter)18 MultipartHttpMessageWriter (cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter)16 ProtobufHttpMessageWriter (cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)16 ArrayList (java.util.ArrayList)14 PartEventHttpMessageWriter (cn.taketoday.http.codec.multipart.PartEventHttpMessageWriter)12 Jackson2JsonEncoder (cn.taketoday.http.codec.json.Jackson2JsonEncoder)10 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)8 List (java.util.List)8 MockClientHttpRequest (cn.taketoday.http.client.reactive.MockClientHttpRequest)7 TypeReference (cn.taketoday.core.TypeReference)6 CharSequenceEncoder (cn.taketoday.core.codec.CharSequenceEncoder)6 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)6 ServerSentEventHttpMessageReader (cn.taketoday.http.codec.ServerSentEventHttpMessageReader)6 ServerSentEventHttpMessageWriter (cn.taketoday.http.codec.ServerSentEventHttpMessageWriter)6 Publisher (org.reactivestreams.Publisher)6 Mono (reactor.core.publisher.Mono)6