Search in sources :

Example 1 with Encoder

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

the class ServerSentEventHttpMessageWriter method applyEncoder.

@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
    ResolvableType elementType = ResolvableType.forClass(data.getClass());
    Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
    return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Map(java.util.Map) Optional(java.util.Optional) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Encoder(org.springframework.core.codec.Encoder) Assert(org.springframework.util.Assert) Encoder(org.springframework.core.codec.Encoder) CodecException(org.springframework.core.codec.CodecException) ResolvableType(org.springframework.core.ResolvableType)

Example 2 with Encoder

use of org.springframework.core.codec.Encoder 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)

Aggregations

ArrayList (java.util.ArrayList)2 Encoder (org.springframework.core.codec.Encoder)2 StandardCharsets (java.nio.charset.StandardCharsets)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Publisher (org.reactivestreams.Publisher)1 ResolvableType (org.springframework.core.ResolvableType)1 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)1 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)1 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)1 CodecException (org.springframework.core.codec.CodecException)1 DataBufferEncoder (org.springframework.core.codec.DataBufferEncoder)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)1 MediaType (org.springframework.http.MediaType)1 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)1 Jackson2ServerHttpMessageWriter (org.springframework.http.codec.Jackson2ServerHttpMessageWriter)1