Search in sources :

Example 1 with HttpMessageEncoder

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

the class ProtobufHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    try {
        HttpHeaders headers = message.getHeaders();
        Message.Builder builder = getMessageBuilder(elementType.toClass());
        Descriptors.Descriptor descriptor = builder.getDescriptorForType();
        headers.add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
        headers.add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());
        if (inputStream instanceof Flux) {
            if (mediaType == null) {
                headers.setContentType(((HttpMessageEncoder<?>) getEncoder()).getStreamingMediaTypes().get(0));
            } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) {
                Map<String, String> parameters = new HashMap<>(mediaType.getParameters());
                parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE);
                headers.setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters));
            }
        }
        return super.write(inputStream, elementType, mediaType, message, hints);
    } catch (Exception ex) {
        return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex));
    }
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) Flux(reactor.core.publisher.Flux) HttpMessageEncoder(cn.taketoday.http.codec.HttpMessageEncoder) MediaType(cn.taketoday.http.MediaType) DecodingException(cn.taketoday.core.codec.DecodingException) Descriptors(com.google.protobuf.Descriptors) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) HashMap(java.util.HashMap) Map(java.util.Map) DecodingException(cn.taketoday.core.codec.DecodingException)

Example 2 with HttpMessageEncoder

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

the class ProtobufHttpMessageWriter method write.

@Override
public Mono<Void> write(Publisher<? extends Message> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    try {
        HttpHeaders headers = message.getHeaders();
        Message.Builder builder = getMessageBuilder(elementType.toClass());
        Descriptors.Descriptor descriptor = builder.getDescriptorForType();
        headers.add(X_PROTOBUF_SCHEMA_HEADER, descriptor.getFile().getName());
        headers.add(X_PROTOBUF_MESSAGE_HEADER, descriptor.getFullName());
        if (inputStream instanceof Flux) {
            if (mediaType == null) {
                headers.setContentType(((HttpMessageEncoder<?>) getEncoder()).getStreamingMediaTypes().get(0));
            } else if (!ProtobufEncoder.DELIMITED_VALUE.equals(mediaType.getParameters().get(ProtobufEncoder.DELIMITED_KEY))) {
                Map<String, String> parameters = new HashMap<>(mediaType.getParameters());
                parameters.put(ProtobufEncoder.DELIMITED_KEY, ProtobufEncoder.DELIMITED_VALUE);
                headers.setContentType(new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters));
            }
        }
        return super.write(inputStream, elementType, mediaType, message, hints);
    } catch (Exception ex) {
        return Mono.error(new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex));
    }
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Message(com.google.protobuf.Message) Flux(reactor.core.publisher.Flux) HttpMessageEncoder(cn.taketoday.http.codec.HttpMessageEncoder) MediaType(cn.taketoday.http.MediaType) DecodingException(cn.taketoday.core.codec.DecodingException) Descriptors(com.google.protobuf.Descriptors) ConcurrentReferenceHashMap(cn.taketoday.util.ConcurrentReferenceHashMap) HashMap(java.util.HashMap) Map(java.util.Map) DecodingException(cn.taketoday.core.codec.DecodingException)

Aggregations

DecodingException (cn.taketoday.core.codec.DecodingException)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 MediaType (cn.taketoday.http.MediaType)2 ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)2 HttpMessageEncoder (cn.taketoday.http.codec.HttpMessageEncoder)2 ConcurrentReferenceHashMap (cn.taketoday.util.ConcurrentReferenceHashMap)2 Descriptors (com.google.protobuf.Descriptors)2 Message (com.google.protobuf.Message)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2