Search in sources :

Example 1 with EncoderHttpMessageWriter

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

the class CodecConfigurerTests method cloneDefaultCodecs.

@Test
void cloneDefaultCodecs() {
    CodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    ProtobufDecoder protoDecoder = new ProtobufDecoder();
    ProtobufEncoder protoEncoder = new ProtobufEncoder();
    clone.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    clone.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    clone.defaultCodecs().protobufDecoder(protoDecoder);
    clone.defaultCodecs().protobufEncoder(protoEncoder);
    // Clone has the customized the customizations
    List<Decoder<?>> decoders = clone.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    List<Encoder<?>> encoders = clone.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    // Original does not have the customizations
    decoders = this.configurer.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    encoders = this.configurer.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) Decoder(cn.taketoday.core.codec.Decoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) StringDecoder(cn.taketoday.core.codec.StringDecoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Collectors(java.util.stream.Collectors) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Test(org.junit.jupiter.api.Test) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) List(java.util.List) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Encoder(cn.taketoday.core.codec.Encoder) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Encoder(cn.taketoday.core.codec.Encoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Decoder(cn.taketoday.core.codec.Decoder) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) StringDecoder(cn.taketoday.core.codec.StringDecoder) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 2 with EncoderHttpMessageWriter

use of cn.taketoday.http.codec.EncoderHttpMessageWriter in project today-framework by TAKETODAY.

the class ServerHttpResponseTests method monoResponseShouldNotLeakIfCancelled.

@Test
// gh-26232
void monoResponseShouldNotLeakIfCancelled() {
    LeakAwareDataBufferFactory bufferFactory = new LeakAwareDataBufferFactory();
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse(bufferFactory);
    response.setWriteHandler(flux -> {
        throw AbortedException.beforeSend();
    });
    HttpMessageWriter<Object> messageWriter = new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder());
    Mono<Void> result = messageWriter.write(Mono.just(Collections.singletonMap("foo", "bar")), ResolvableType.fromClass(Mono.class), ResolvableType.fromClass(Map.class), null, request, response, Collections.emptyMap());
    StepVerifier.create(result).expectError(AbortedException.class).verify();
    bufferFactory.checkForLeaks();
}
Also used : LeakAwareDataBufferFactory(cn.taketoday.core.io.buffer.LeakAwareDataBufferFactory) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) AbortedException(reactor.netty.channel.AbortedException) Mono(reactor.core.publisher.Mono) Map(java.util.Map) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 3 with EncoderHttpMessageWriter

use of cn.taketoday.http.codec.EncoderHttpMessageWriter in project today-framework by TAKETODAY.

the class DefaultClientRequestBuilderTests method fromCopiesBody.

@Test
public void fromCopiesBody() {
    String body = "foo";
    BodyInserter<String, ClientHttpRequest> inserter = (response, strategies) -> {
        byte[] bodyBytes = body.getBytes(UTF_8);
        DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bodyBytes);
        return response.writeWith(Mono.just(buffer));
    };
    ClientRequest other = ClientRequest.create(POST, DEFAULT_URL).body(inserter).build();
    ClientRequest result = ClientRequest.from(other).build();
    List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
    messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    ExchangeStrategies strategies = mock(ExchangeStrategies.class);
    given(strategies.messageWriters()).willReturn(messageWriters);
    MockClientHttpRequest request = new MockClientHttpRequest(POST, "/");
    result.writeTo(request, strategies).block();
    String copiedBody = request.getBodyAsString().block();
    assertThat(copiedBody).isEqualTo("foo");
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) DELETE(cn.taketoday.http.HttpMethod.DELETE) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) BDDMockito.given(org.mockito.BDDMockito.given) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) URI(java.net.URI) TypeReference(cn.taketoday.core.TypeReference) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Publisher(org.reactivestreams.Publisher) OPTIONS(cn.taketoday.http.HttpMethod.OPTIONS) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) List(java.util.List) POST(cn.taketoday.http.HttpMethod.POST) GET(cn.taketoday.http.HttpMethod.GET) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) BodyInserter(cn.taketoday.web.reactive.function.BodyInserter) Mockito.mock(org.mockito.Mockito.mock) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with EncoderHttpMessageWriter

use of cn.taketoday.http.codec.EncoderHttpMessageWriter in project today-framework by TAKETODAY.

the class CodecConfigurerTests method cloneDefaultCodecs.

@Test
void cloneDefaultCodecs() {
    CodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    ProtobufDecoder protoDecoder = new ProtobufDecoder();
    ProtobufEncoder protoEncoder = new ProtobufEncoder();
    clone.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    clone.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    clone.defaultCodecs().protobufDecoder(protoDecoder);
    clone.defaultCodecs().protobufEncoder(protoEncoder);
    // Clone has the customized the customizations
    List<Decoder<?>> decoders = clone.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    List<Encoder<?>> encoders = clone.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    // Original does not have the customizations
    decoders = this.configurer.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    encoders = this.configurer.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
}
Also used : ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) Decoder(cn.taketoday.core.codec.Decoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) StringDecoder(cn.taketoday.core.codec.StringDecoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Collectors(java.util.stream.Collectors) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Test(org.junit.jupiter.api.Test) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) List(java.util.List) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) HttpMessageReader(cn.taketoday.http.codec.HttpMessageReader) MediaType(cn.taketoday.http.MediaType) Encoder(cn.taketoday.core.codec.Encoder) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(cn.taketoday.http.codec.DecoderHttpMessageReader) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) Jackson2SmileEncoder(cn.taketoday.http.codec.json.Jackson2SmileEncoder) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Encoder(cn.taketoday.core.codec.Encoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) Decoder(cn.taketoday.core.codec.Decoder) Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) StringDecoder(cn.taketoday.core.codec.StringDecoder) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) CodecConfigurer(cn.taketoday.http.codec.CodecConfigurer) Jackson2JsonEncoder(cn.taketoday.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 5 with EncoderHttpMessageWriter

use of cn.taketoday.http.codec.EncoderHttpMessageWriter in project today-framework by TAKETODAY.

the class BaseDefaultCodecs method getBaseTypedWriters.

/**
 * Return "base" typed writers only, i.e. common to client and server.
 */
final List<HttpMessageWriter<?>> getBaseTypedWriters() {
    if (!this.registerDefaults) {
        return Collections.emptyList();
    }
    List<HttpMessageWriter<?>> writers = new ArrayList<>();
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    addCodec(writers, new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
    if (nettyByteBufPresent) {
        addCodec(writers, new EncoderHttpMessageWriter<>(new NettyByteBufEncoder()));
    }
    addCodec(writers, new ResourceHttpMessageWriter());
    addCodec(writers, new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()));
    if (protobufPresent) {
        addCodec(writers, new ProtobufHttpMessageWriter(this.protobufEncoder != null ? (ProtobufEncoder) this.protobufEncoder : new ProtobufEncoder()));
    }
    return writers;
}
Also used : ByteBufferEncoder(cn.taketoday.core.codec.ByteBufferEncoder) ProtobufEncoder(cn.taketoday.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(cn.taketoday.http.codec.ServerSentEventHttpMessageWriter) MultipartHttpMessageWriter(cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) 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) ByteArrayEncoder(cn.taketoday.core.codec.ByteArrayEncoder) DataBufferEncoder(cn.taketoday.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(cn.taketoday.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(cn.taketoday.http.codec.protobuf.ProtobufHttpMessageWriter)

Aggregations

EncoderHttpMessageWriter (cn.taketoday.http.codec.EncoderHttpMessageWriter)16 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)14 Test (org.junit.jupiter.api.Test)12 ArrayList (java.util.ArrayList)11 List (java.util.List)8 MockClientHttpRequest (cn.taketoday.http.client.reactive.MockClientHttpRequest)7 ByteBufferEncoder (cn.taketoday.core.codec.ByteBufferEncoder)6 CharSequenceEncoder (cn.taketoday.core.codec.CharSequenceEncoder)6 ResourceHttpMessageWriter (cn.taketoday.http.codec.ResourceHttpMessageWriter)6 ServerSentEventHttpMessageWriter (cn.taketoday.http.codec.ServerSentEventHttpMessageWriter)6 Jackson2JsonEncoder (cn.taketoday.http.codec.json.Jackson2JsonEncoder)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 BDDMockito.given (org.mockito.BDDMockito.given)6 Mockito.mock (org.mockito.Mockito.mock)6 TypeReference (cn.taketoday.core.TypeReference)5 ByteArrayEncoder (cn.taketoday.core.codec.ByteArrayEncoder)4 DataBufferEncoder (cn.taketoday.core.codec.DataBufferEncoder)4 NettyByteBufEncoder (cn.taketoday.core.codec.NettyByteBufEncoder)4 FormHttpMessageWriter (cn.taketoday.http.codec.FormHttpMessageWriter)4 MultipartHttpMessageWriter (cn.taketoday.http.codec.multipart.MultipartHttpMessageWriter)4