Search in sources :

Example 1 with ByteArrayEncoder

use of org.springframework.core.codec.ByteArrayEncoder in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method addDefaultHttpMessageWriters.

/**
	 * Adds default converters that sub-classes can call from
	 * {@link #configureMessageWriters(List)}.
	 */
protected final void addDefaultHttpMessageWriters(List<HttpMessageWriter<?>> writers) {
    List<Encoder<?>> sseDataEncoders = new ArrayList<>();
    writers.add(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new DataBufferEncoder()));
    writers.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    writers.add(new ResourceHttpMessageWriter());
    if (jaxb2Present) {
        writers.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
        writers.add(new Jackson2ServerHttpMessageWriter(encoder));
        sseDataEncoders.add(encoder);
        HttpMessageWriter<Object> writer = new ServerSentEventHttpMessageWriter(sseDataEncoders);
        writers.add(new Jackson2ServerHttpMessageWriter(writer));
    } else {
        writers.add(new ServerSentEventHttpMessageWriter(sseDataEncoders));
    }
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ArrayList(java.util.ArrayList) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Jackson2ServerHttpMessageWriter(org.springframework.http.codec.Jackson2ServerHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Encoder(org.springframework.core.codec.Encoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder)

Example 2 with ByteArrayEncoder

use of org.springframework.core.codec.ByteArrayEncoder in project spring-framework by spring-projects.

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(org.springframework.core.codec.ByteBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) MultipartHttpMessageWriter(org.springframework.http.codec.multipart.MultipartHttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ArrayList(java.util.ArrayList) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter)

Example 3 with ByteArrayEncoder

use of org.springframework.core.codec.ByteArrayEncoder in project spring-framework by spring-projects.

the class RSocketMessageHandlerTests method getRSocketStrategiesReflectsCurrentState.

@Test
public void getRSocketStrategiesReflectsCurrentState() {
    RSocketMessageHandler handler = new RSocketMessageHandler();
    // 1. Set properties
    handler.setDecoders(Collections.singletonList(new ByteArrayDecoder()));
    handler.setEncoders(Collections.singletonList(new ByteArrayEncoder()));
    handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
    handler.setMetadataExtractor(new DefaultMetadataExtractor());
    handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
    RSocketStrategies strategies = handler.getRSocketStrategies();
    assertThat(strategies.encoders()).isEqualTo(handler.getEncoders());
    assertThat(strategies.decoders()).isEqualTo(handler.getDecoders());
    assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher());
    assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor());
    assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry());
    // 2. Set properties again
    handler.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
    handler.setEncoders(Collections.singletonList(CharSequenceEncoder.allMimeTypes()));
    handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
    handler.setMetadataExtractor(new DefaultMetadataExtractor());
    handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
    handler.afterPropertiesSet();
    strategies = handler.getRSocketStrategies();
    assertThat(strategies.encoders()).isEqualTo(handler.getEncoders());
    assertThat(strategies.decoders()).isEqualTo(handler.getDecoders());
    assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher());
    assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor());
    assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry());
}
Also used : ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) RSocketStrategies(org.springframework.messaging.rsocket.RSocketStrategies) DefaultMetadataExtractor(org.springframework.messaging.rsocket.DefaultMetadataExtractor) SimpleRouteMatcher(org.springframework.util.SimpleRouteMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 4 with ByteArrayEncoder

use of org.springframework.core.codec.ByteArrayEncoder in project spring-framework by spring-projects.

the class RSocketMessageHandlerTests method getRSocketStrategies.

@Test
public void getRSocketStrategies() {
    RSocketMessageHandler handler = new RSocketMessageHandler();
    handler.setDecoders(Collections.singletonList(new ByteArrayDecoder()));
    handler.setEncoders(Collections.singletonList(new ByteArrayEncoder()));
    handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
    handler.setMetadataExtractor(new DefaultMetadataExtractor());
    handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
    RSocketStrategies strategies = handler.getRSocketStrategies();
    assertThat(strategies).isNotNull();
    assertThat(strategies.encoders()).isEqualTo(handler.getEncoders());
    assertThat(strategies.decoders()).isEqualTo(handler.getDecoders());
    assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher());
    assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor());
    assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry());
}
Also used : ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) RSocketStrategies(org.springframework.messaging.rsocket.RSocketStrategies) DefaultMetadataExtractor(org.springframework.messaging.rsocket.DefaultMetadataExtractor) SimpleRouteMatcher(org.springframework.util.SimpleRouteMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) Test(org.junit.jupiter.api.Test)

Example 5 with ByteArrayEncoder

use of org.springframework.core.codec.ByteArrayEncoder in project spring-framework by spring-projects.

the class DefaultExchangeStrategiesBuilder method defaultWriters.

private void defaultWriters() {
    messageWriter(new EncoderHttpMessageWriter<>(new ByteArrayEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    messageWriter(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    messageWriter(new ResourceHttpMessageWriter());
    messageWriter(new FormHttpMessageWriter());
    if (jaxb2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    }
    if (jackson2Present) {
        messageWriter(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    }
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) FormHttpMessageWriter(org.springframework.http.codec.FormHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder)

Aggregations

ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)7 Test (org.junit.jupiter.api.Test)4 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)4 ByteArrayDecoder (org.springframework.core.codec.ByteArrayDecoder)4 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)4 AntPathMatcher (org.springframework.util.AntPathMatcher)4 SimpleRouteMatcher (org.springframework.util.SimpleRouteMatcher)4 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)3 DataBufferEncoder (org.springframework.core.codec.DataBufferEncoder)3 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)3 DefaultMetadataExtractor (org.springframework.messaging.rsocket.DefaultMetadataExtractor)3 RSocketStrategies (org.springframework.messaging.rsocket.RSocketStrategies)3 ArrayList (java.util.ArrayList)2 FormHttpMessageWriter (org.springframework.http.codec.FormHttpMessageWriter)2 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)2 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)2 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)2 Consumer (java.util.function.Consumer)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1