Search in sources :

Example 1 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-infrastructure by TAKETODAY.

the class AbstractServerHttpResponse method writeWith.

@Override
@SuppressWarnings("unchecked")
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
    // We must resolve value first however, for a chance to handle potential error.
    if (body instanceof Mono) {
        return ((Mono<? extends DataBuffer>) body).flatMap(buffer -> {
            touchDataBuffer(buffer);
            AtomicBoolean subscribed = new AtomicBoolean();
            return doCommit(() -> {
                try {
                    return writeWithInternal(Mono.fromCallable(() -> buffer).doOnSubscribe(s -> subscribed.set(true)).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release));
                } catch (Throwable ex) {
                    return Mono.error(ex);
                }
            }).doOnError(ex -> DataBufferUtils.release(buffer)).doOnCancel(() -> {
                if (!subscribed.get()) {
                    DataBufferUtils.release(buffer);
                }
            });
        }).doOnError(t -> getHeaders().clearContentHeaders()).doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
    } else {
        return new ChannelSendOperator<>(body, inner -> doCommit(() -> writeWithInternal(inner))).doOnError(t -> getHeaders().clearContentHeaders());
    }
}
Also used : Assert(cn.taketoday.lang.Assert) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ResponseCookie(cn.taketoday.http.ResponseCookie) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) Nullable(cn.taketoday.lang.Nullable) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) MultiValueMap(cn.taketoday.core.MultiValueMap) HttpStatus(cn.taketoday.http.HttpStatus) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) Mono(reactor.core.publisher.Mono) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) PooledDataBuffer(cn.taketoday.core.io.buffer.PooledDataBuffer) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer)

Example 2 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-infrastructure by TAKETODAY.

the class DefaultPartHttpMessageReaderTests method noHeaders.

@ParameterizedDefaultPartHttpMessageReaderTest
public void noHeaders(String displayName, DefaultPartHttpMessageReader reader) {
    MockServerHttpRequest request = createRequest(new ClassPathResource("no-header.multipart", getClass()), "boundary");
    Flux<Part> result = reader.read(fromClass(Part.class), request, emptyMap());
    StepVerifier.create(result).consumeNextWith(part -> {
        assertThat(part.headers()).isEmpty();
        part.content().subscribe(DataBufferUtils::release);
    }).verifyComplete();
}
Also used : NettyDataBufferFactory(cn.taketoday.core.io.buffer.NettyDataBufferFactory) StepVerifier(reactor.test.StepVerifier) DataBufferFactory(cn.taketoday.core.io.buffer.DataBufferFactory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Resource(cn.taketoday.core.io.Resource) Retention(java.lang.annotation.Retention) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Collections.singletonMap(java.util.Collections.singletonMap) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) Path(java.nio.file.Path) ResolvableType.fromClass(cn.taketoday.core.ResolvableType.fromClass) MethodSource(org.junit.jupiter.params.provider.MethodSource) Collections.emptyMap(java.util.Collections.emptyMap) Files(java.nio.file.Files) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) UTF_8(java.nio.charset.StandardCharsets.UTF_8) IOException(java.io.IOException) Target(java.lang.annotation.Target) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) ElementType(java.lang.annotation.ElementType) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Nullable(cn.taketoday.lang.Nullable) DataBufferUtils.release(cn.taketoday.core.io.buffer.DataBufferUtils.release) Subscription(org.reactivestreams.Subscription) Execution(org.junit.jupiter.api.parallel.Execution) MediaType(cn.taketoday.http.MediaType) Exceptions(reactor.core.Exceptions) Collections(java.util.Collections) MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) RetentionPolicy(java.lang.annotation.RetentionPolicy) DecodingException(cn.taketoday.core.codec.DecodingException) MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) ClassPathResource(cn.taketoday.core.io.ClassPathResource)

Example 3 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-infrastructure by TAKETODAY.

the class PartEventHttpMessageReaderTests method chrome.

@Test
public void chrome() {
    MockServerHttpRequest request = createRequest(new ClassPathResource("chrome.multipart", getClass()), "----WebKitFormBoundaryEveBLvRT65n21fwU");
    Flux<PartEvent> result = this.reader.read(fromClass(PartEvent.class), request, emptyMap());
    StepVerifier.create(result).assertNext(data(headersFormField("text1"), bodyText("a"), true)).assertNext(data(headersFormField("text2"), bodyText("b"), true)).assertNext(data(headersFile("file1", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file1", "a.txt"), DataBufferUtils::release, true)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, true)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, true)).verifyComplete();
}
Also used : DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 4 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-infrastructure by TAKETODAY.

the class PartEventHttpMessageReaderTests method firefox.

@Test
public void firefox() {
    MockServerHttpRequest request = createRequest(new ClassPathResource("firefox.multipart", getClass()), "---------------------------18399284482060392383840973206");
    Flux<PartEvent> result = this.reader.read(fromClass(PartEvent.class), request, emptyMap());
    StepVerifier.create(result).assertNext(data(headersFormField("text1"), bodyText("a"), true)).assertNext(data(headersFormField("text2"), bodyText("b"), true)).assertNext(data(headersFile("file1", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file1", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file1", "a.txt"), DataBufferUtils::release, true)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, true)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, false)).assertNext(data(headersFile("file2", "b.txt"), DataBufferUtils::release, true)).verifyComplete();
}
Also used : DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 5 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils 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)

Aggregations

DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)16 ClassPathResource (cn.taketoday.core.io.ClassPathResource)11 MockServerHttpRequest (cn.taketoday.http.server.reactive.MockServerHttpRequest)11 Test (org.junit.jupiter.api.Test)11 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 MediaType (cn.taketoday.http.MediaType)7 DataBufferFactory (cn.taketoday.core.io.buffer.DataBufferFactory)6 Nullable (cn.taketoday.lang.Nullable)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)6 MultiValueMap (cn.taketoday.core.MultiValueMap)4 Resource (cn.taketoday.core.io.Resource)4 PooledDataBuffer (cn.taketoday.core.io.buffer.PooledDataBuffer)4 HttpHeaders (cn.taketoday.http.HttpHeaders)4 Assert (cn.taketoday.lang.Assert)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Supplier (java.util.function.Supplier)4 Publisher (org.reactivestreams.Publisher)4