Search in sources :

Example 1 with ProtobufDecoder

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

the class BaseDefaultCodecs method initTypedReaders.

/**
 * Reset and initialize typed readers.
 */
protected void initTypedReaders() {
    this.typedReaders.clear();
    if (!this.registerDefaults) {
        return;
    }
    addCodec(this.typedReaders, new DecoderHttpMessageReader<>(new ByteArrayDecoder()));
    addCodec(this.typedReaders, new DecoderHttpMessageReader<>(new ByteBufferDecoder()));
    addCodec(this.typedReaders, new DecoderHttpMessageReader<>(new DataBufferDecoder()));
    if (nettyByteBufPresent) {
        addCodec(this.typedReaders, new DecoderHttpMessageReader<>(new NettyByteBufDecoder()));
    }
    addCodec(this.typedReaders, new ResourceHttpMessageReader(new ResourceDecoder()));
    addCodec(this.typedReaders, new DecoderHttpMessageReader<>(StringDecoder.textPlainOnly()));
    if (protobufPresent) {
        addCodec(this.typedReaders, new DecoderHttpMessageReader<>(this.protobufDecoder != null ? (ProtobufDecoder) this.protobufDecoder : new ProtobufDecoder()));
    }
    addCodec(this.typedReaders, new FormHttpMessageReader());
    // client vs server..
    extendTypedReaders(this.typedReaders);
}
Also used : FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) AbstractDataBufferDecoder(cn.taketoday.core.codec.AbstractDataBufferDecoder) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) ResourceHttpMessageReader(cn.taketoday.http.codec.ResourceHttpMessageReader) ResourceDecoder(cn.taketoday.core.codec.ResourceDecoder) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder)

Example 2 with ProtobufDecoder

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

the class ClientCodecConfigurerTests method maxInMemorySize.

@Test
public void maxInMemorySize() {
    int size = 99;
    this.configurer.defaultCodecs().maxInMemorySize(size);
    List<HttpMessageReader<?>> readers = this.configurer.getReaders();
    assertThat(readers.size()).isEqualTo(12);
    assertThat(((ByteArrayDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((ByteBufferDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((DataBufferDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((NettyByteBufDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((ResourceDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((StringDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((ProtobufDecoder) getNextDecoder(readers)).getMaxMessageSize()).isEqualTo(size);
    assertThat(((FormHttpMessageReader) nextReader(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((Jackson2JsonDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((Jackson2SmileDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
    ServerSentEventHttpMessageReader reader = (ServerSentEventHttpMessageReader) nextReader(readers);
    assertThat(reader.getMaxInMemorySize()).isEqualTo(size);
    assertThat(((Jackson2JsonDecoder) reader.getDecoder()).getMaxInMemorySize()).isEqualTo(size);
    assertThat(((StringDecoder) getNextDecoder(readers)).getMaxInMemorySize()).isEqualTo(size);
}
Also used : Jackson2JsonDecoder(cn.taketoday.http.codec.json.Jackson2JsonDecoder) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) StringDecoder(cn.taketoday.core.codec.StringDecoder) ResourceDecoder(cn.taketoday.core.codec.ResourceDecoder) ByteBufferDecoder(cn.taketoday.core.codec.ByteBufferDecoder) 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) FormHttpMessageReader(cn.taketoday.http.codec.FormHttpMessageReader) NettyByteBufDecoder(cn.taketoday.core.codec.NettyByteBufDecoder) ServerSentEventHttpMessageReader(cn.taketoday.http.codec.ServerSentEventHttpMessageReader) DataBufferDecoder(cn.taketoday.core.codec.DataBufferDecoder) ByteArrayDecoder(cn.taketoday.core.codec.ByteArrayDecoder) Jackson2SmileDecoder(cn.taketoday.http.codec.json.Jackson2SmileDecoder) Test(org.junit.jupiter.api.Test)

Example 3 with ProtobufDecoder

use of cn.taketoday.http.codec.protobuf.ProtobufDecoder 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 4 with ProtobufDecoder

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

the class CancelWithoutDemandCodecTests method cancelWithProtobufDecoder.

// gh-22731
@Test
public void cancelWithProtobufDecoder() throws InterruptedException {
    ProtobufDecoder decoder = new ProtobufDecoder();
    Mono<DataBuffer> input = Mono.fromCallable(() -> {
        Msg msg = Msg.newBuilder().setFoo("Foo").build();
        byte[] bytes = msg.toByteArray();
        DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
        buffer.write(bytes);
        return buffer;
    });
    Flux<Message> messages = decoder.decode(input, ResolvableType.fromType(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    ZeroDemandMessageSubscriber subscriber = new ZeroDemandMessageSubscriber();
    messages.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with ProtobufDecoder

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

the class CancelWithoutDemandCodecTests method cancelWithProtobufDecoder.

// gh-22731
@Test
public void cancelWithProtobufDecoder() throws InterruptedException {
    ProtobufDecoder decoder = new ProtobufDecoder();
    Mono<DataBuffer> input = Mono.fromCallable(() -> {
        Msg msg = Msg.newBuilder().setFoo("Foo").build();
        byte[] bytes = msg.toByteArray();
        DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
        buffer.write(bytes);
        return buffer;
    });
    Flux<Message> messages = decoder.decode(input, ResolvableType.fromType(Msg.class), new MimeType("application", "x-protobuf"), Collections.emptyMap());
    ZeroDemandMessageSubscriber subscriber = new ZeroDemandMessageSubscriber();
    messages.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Msg(cn.taketoday.protobuf.Msg) SecondMsg(cn.taketoday.protobuf.SecondMsg) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) ProtobufDecoder(cn.taketoday.http.codec.protobuf.ProtobufDecoder) MimeType(cn.taketoday.util.MimeType) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ProtobufDecoder (cn.taketoday.http.codec.protobuf.ProtobufDecoder)12 Test (org.junit.jupiter.api.Test)10 ByteArrayDecoder (cn.taketoday.core.codec.ByteArrayDecoder)8 ByteBufferDecoder (cn.taketoday.core.codec.ByteBufferDecoder)8 DataBufferDecoder (cn.taketoday.core.codec.DataBufferDecoder)8 NettyByteBufDecoder (cn.taketoday.core.codec.NettyByteBufDecoder)8 FormHttpMessageReader (cn.taketoday.http.codec.FormHttpMessageReader)8 ResourceHttpMessageReader (cn.taketoday.http.codec.ResourceHttpMessageReader)8 Jackson2JsonDecoder (cn.taketoday.http.codec.json.Jackson2JsonDecoder)8 Jackson2SmileDecoder (cn.taketoday.http.codec.json.Jackson2SmileDecoder)8 ResourceDecoder (cn.taketoday.core.codec.ResourceDecoder)6 StringDecoder (cn.taketoday.core.codec.StringDecoder)6 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)6 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)6 ServerSentEventHttpMessageReader (cn.taketoday.http.codec.ServerSentEventHttpMessageReader)4 Jackson2JsonEncoder (cn.taketoday.http.codec.json.Jackson2JsonEncoder)4 Jackson2SmileEncoder (cn.taketoday.http.codec.json.Jackson2SmileEncoder)4 ProtobufEncoder (cn.taketoday.http.codec.protobuf.ProtobufEncoder)4 ResolvableType (cn.taketoday.core.ResolvableType)2 AbstractDataBufferDecoder (cn.taketoday.core.codec.AbstractDataBufferDecoder)2