Search in sources :

Example 6 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofDataBuffers.

@Test
public void ofDataBuffers() {
    byte[] bytes = "foo".getBytes(UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBody()).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBuffer(cn.taketoday.core.io.buffer.DefaultDataBuffer) Test(org.junit.jupiter.api.Test)

Example 7 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofProducerWithSingle.

@Test
public void ofProducerWithSingle() {
    Single<User> body = Single.just(new User("foo", "bar"));
    BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 8 with ReactiveHttpOutputMessage

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

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

the class MultipartHttpMessageWriter method writeMultipart.

private Mono<Void> writeMultipart(MultiValueMap<String, ?> map, ReactiveHttpOutputMessage outputMessage, @Nullable MediaType mediaType, Map<String, Object> hints) {
    byte[] boundary = generateMultipartBoundary();
    mediaType = getMultipartMediaType(mediaType, boundary);
    outputMessage.getHeaders().setContentType(mediaType);
    LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Encoding " + (isEnableLoggingRequestDetails() ? LogFormatUtils.formatValue(map, !traceOn) : "parts " + map.keySet() + " (content masked)"));
    DataBufferFactory bufferFactory = outputMessage.bufferFactory();
    Flux<DataBuffer> body = Flux.fromIterable(map.entrySet()).concatMap(entry -> encodePartValues(boundary, entry.getKey(), entry.getValue(), bufferFactory)).concatWith(generateLastLine(boundary, bufferFactory)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
    if (logger.isDebugEnabled()) {
        body = body.doOnNext(buffer -> Hints.touchDataBuffer(buffer, hints, logger));
    }
    return outputMessage.writeWith(body);
}
Also used : Arrays(java.util.Arrays) Assert(cn.taketoday.lang.Assert) ResolvableTypeProvider(cn.taketoday.core.ResolvableTypeProvider) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Resource(cn.taketoday.core.io.Resource) FormHttpMessageWriter(cn.taketoday.http.codec.FormHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodecException(cn.taketoday.core.codec.CodecException) LogFormatUtils(cn.taketoday.util.LogFormatUtils) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HttpHeaders(cn.taketoday.http.HttpHeaders) ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) Hints(cn.taketoday.core.codec.Hints) Map(java.util.Map) CharSequenceEncoder(cn.taketoday.core.codec.CharSequenceEncoder) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) EncoderHttpMessageWriter(cn.taketoday.http.codec.EncoderHttpMessageWriter) Publisher(org.reactivestreams.Publisher) ResolvableType(cn.taketoday.core.ResolvableType) HttpMessageWriter(cn.taketoday.http.codec.HttpMessageWriter) Mono(reactor.core.publisher.Mono) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) Flux(reactor.core.publisher.Flux) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) Optional(java.util.Optional) HttpEntity(cn.taketoday.http.HttpEntity) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 10 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofProducerWithSingle.

@Test
public void ofProducerWithSingle() {
    Single<User> body = Single.just(new User("foo", "bar"));
    BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, User.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\",\"password\":\"bar\"}").expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

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