Search in sources :

Example 16 with ReactiveHttpOutputMessage

use of cn.taketoday.http.ReactiveHttpOutputMessage 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)

Example 17 with ReactiveHttpOutputMessage

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

the class BodyInserters method writeWithMessageWriters.

private static <M extends ReactiveHttpOutputMessage> Mono<Void> writeWithMessageWriters(M outputMessage, BodyInserter.Context context, Object body, ResolvableType bodyType, @Nullable ReactiveAdapter adapter) {
    Publisher<?> publisher;
    if (body instanceof Publisher) {
        publisher = (Publisher<?>) body;
    } else if (adapter != null) {
        publisher = adapter.toPublisher(body);
    } else {
        publisher = Mono.just(body);
    }
    MediaType mediaType = outputMessage.getHeaders().getContentType();
    return context.messageWriters().stream().filter(messageWriter -> messageWriter.canWrite(bodyType, mediaType)).findFirst().map(BodyInserters::cast).map(writer -> write(publisher, bodyType, mediaType, outputMessage, context, writer)).orElseGet(() -> Mono.error(unsupportedError(bodyType, context, mediaType)));
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Assert(cn.taketoday.lang.Assert) WebClient(cn.taketoday.web.reactive.function.client.WebClient) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Publisher(org.reactivestreams.Publisher) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) List(java.util.List) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Nullable(cn.taketoday.lang.Nullable) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) TypeReference(cn.taketoday.core.TypeReference) MediaType(cn.taketoday.http.MediaType) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) MediaType(cn.taketoday.http.MediaType) Publisher(org.reactivestreams.Publisher)

Example 18 with ReactiveHttpOutputMessage

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

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link WebClient WebClient} and
 * {@link cn.taketoday.web.reactive.function.server.ServerResponse ServerResponse}.
 *
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementTypeRef the type of values to be produced
 * @return the inserter to write a producer
 * @since 4.0
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, TypeReference<?> elementTypeRef) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementTypeRef, "'elementTypeRef' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.fromType(elementTypeRef), adapter);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Assert(cn.taketoday.lang.Assert) WebClient(cn.taketoday.web.reactive.function.client.WebClient) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Publisher(org.reactivestreams.Publisher) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) List(java.util.List) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Nullable(cn.taketoday.lang.Nullable) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) TypeReference(cn.taketoday.core.TypeReference) MediaType(cn.taketoday.http.MediaType) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter)

Example 19 with ReactiveHttpOutputMessage

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

the class BodyInserters method fromProducer.

/**
 * Inserter to write the given producer of value(s) which must be a {@link Publisher}
 * or another producer adaptable to a {@code Publisher} via
 * {@link ReactiveAdapterRegistry}.
 * <p>Alternatively, consider using the {@code body} shortcuts on
 * {@link WebClient WebClient} and
 * {@link cn.taketoday.web.reactive.function.server.ServerResponse ServerResponse}.
 *
 * @param <T> the type of the body
 * @param producer the source of body value(s).
 * @param elementClass the class of values to be produced
 * @return the inserter to write a producer
 * @since 4.0
 */
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, Class<?> elementClass) {
    Assert.notNull(producer, "'producer' must not be null");
    Assert.notNull(elementClass, "'elementClass' must not be null");
    ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
    Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
    return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.fromClass(elementClass), adapter);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Assert(cn.taketoday.lang.Assert) WebClient(cn.taketoday.web.reactive.function.client.WebClient) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Publisher(org.reactivestreams.Publisher) ReactiveAdapterRegistry(cn.taketoday.core.ReactiveAdapterRegistry) ResolvableType(cn.taketoday.core.ResolvableType) Resource(cn.taketoday.core.io.Resource) MultipartBodyBuilder(cn.taketoday.http.client.MultipartBodyBuilder) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) Mono(reactor.core.publisher.Mono) Collectors(java.util.stream.Collectors) List(java.util.List) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Nullable(cn.taketoday.lang.Nullable) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) TypeReference(cn.taketoday.core.TypeReference) MediaType(cn.taketoday.http.MediaType) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) ReactiveAdapter(cn.taketoday.core.ReactiveAdapter)

Example 20 with ReactiveHttpOutputMessage

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

the class ResourceHttpMessageWriter method writeResource.

private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) {
    HttpHeaders headers = message.getHeaders();
    MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);
    headers.setContentType(resourceMediaType);
    if (headers.getContentLength() < 0) {
        long length = lengthOf(resource);
        if (length != -1) {
            headers.setContentLength(length);
        }
    }
    return zeroCopy(resource, null, message, hints).orElseGet(() -> {
        Mono<Resource> input = Mono.just(resource);
        DataBufferFactory factory = message.bufferFactory();
        Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints);
        if (logger.isDebugEnabled()) {
            body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
        }
        return message.writeWith(body);
    });
}
Also used : DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) ResourceDecoder(cn.taketoday.core.codec.ResourceDecoder) ZeroCopyHttpOutputMessage(cn.taketoday.http.ZeroCopyHttpOutputMessage) HttpHeaders(cn.taketoday.http.HttpHeaders) ResourceEncoder(cn.taketoday.core.codec.ResourceEncoder) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) ResourceRegion(cn.taketoday.core.io.ResourceRegion) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MimeTypeUtils(cn.taketoday.util.MimeTypeUtils) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) File(java.io.File) HttpRange(cn.taketoday.http.HttpRange) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerHttpRequest(cn.taketoday.http.server.reactive.ServerHttpRequest) Nullable(cn.taketoday.lang.Nullable) HttpLogging(cn.taketoday.http.HttpLogging) MediaTypeFactory(cn.taketoday.http.MediaTypeFactory) Logger(cn.taketoday.logging.Logger) Optional(java.util.Optional) HttpStatus(cn.taketoday.http.HttpStatus) ResourceRegionEncoder(cn.taketoday.core.codec.ResourceRegionEncoder) MediaType(cn.taketoday.http.MediaType) InputStreamResource(cn.taketoday.core.io.InputStreamResource) HttpHeaders(cn.taketoday.http.HttpHeaders) Resource(cn.taketoday.core.io.Resource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) MediaType(cn.taketoday.http.MediaType) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Aggregations

ReactiveHttpOutputMessage (cn.taketoday.http.ReactiveHttpOutputMessage)30 MockServerHttpResponse (cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse)18 Test (org.junit.jupiter.api.Test)18 Resource (cn.taketoday.core.io.Resource)12 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)12 MediaType (cn.taketoday.http.MediaType)12 List (java.util.List)12 MultiValueMap (cn.taketoday.core.MultiValueMap)10 ResolvableType (cn.taketoday.core.ResolvableType)10 Nullable (cn.taketoday.lang.Nullable)10 Publisher (org.reactivestreams.Publisher)10 Flux (reactor.core.publisher.Flux)10 Mono (reactor.core.publisher.Mono)10 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)8 HttpEntity (cn.taketoday.http.HttpEntity)8 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)8 ServerHttpResponse (cn.taketoday.http.server.reactive.ServerHttpResponse)8 Assert (cn.taketoday.lang.Assert)8 Map (java.util.Map)8 ReactiveAdapter (cn.taketoday.core.ReactiveAdapter)6