Search in sources :

Example 21 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofPublisher.

@Test
public void ofPublisher() {
    Flux<String> body = Flux.just("foo");
    BodyInserter<Flux<String>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromPublisher(body, String.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBody()).consumeNextWith(buf -> {
        String actual = buf.toString(UTF_8);
        assertThat(actual).isEqualTo("foo");
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 22 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofProducerWithFlux.

@Test
public void ofProducerWithFlux() {
    Flux<String> body = Flux.just("foo");
    BodyInserter<?, ReactiveHttpOutputMessage> inserter = BodyInserters.fromProducer(body, String.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBody()).consumeNextWith(buf -> {
        String actual = buf.toString(UTF_8);
        assertThat(actual).isEqualTo("foo");
    }).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 23 with ReactiveHttpOutputMessage

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

the class BodyInsertersTests method ofResourceRange.

@Test
public void ofResourceRange() throws IOException {
    final int rangeStart = 10;
    Resource body = new ClassPathResource("response.txt", getClass());
    BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
    MockServerHttpRequest request = MockServerHttpRequest.get("/foo").range(HttpRange.createByteRange(rangeStart)).build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, new BodyInserter.Context() {

        @Override
        public List<HttpMessageWriter<?>> messageWriters() {
            return Collections.singletonList(new ResourceHttpMessageWriter());
        }

        @Override
        public Optional<ServerHttpRequest> serverRequest() {
            return Optional.of(request);
        }

        @Override
        public Map<String, Object> hints() {
            return hints;
        }
    });
    StepVerifier.create(result).expectComplete().verify();
    byte[] allBytes = Files.readAllBytes(body.getFile().toPath());
    byte[] expectedBytes = new byte[allBytes.length - rangeStart];
    System.arraycopy(allBytes, rangeStart, expectedBytes, 0, expectedBytes.length);
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo(expectedBytes);
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(cn.taketoday.http.ReactiveHttpOutputMessage) Optional(java.util.Optional) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) ClassPathResource(cn.taketoday.core.io.ClassPathResource) MockServerHttpRequest(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpRequest) ResourceHttpMessageWriter(cn.taketoday.http.codec.ResourceHttpMessageWriter) List(java.util.List) ArrayList(java.util.ArrayList) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) Map(java.util.Map) HashMap(java.util.HashMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 24 with ReactiveHttpOutputMessage

use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure 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);
    if (isDebugEnabled) {
        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 (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 25 with ReactiveHttpOutputMessage

use of cn.taketoday.http.ReactiveHttpOutputMessage in project today-infrastructure 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