Search in sources :

Example 6 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-framework 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 7 with DataBufferUtils

use of cn.taketoday.core.io.buffer.DataBufferUtils in project today-framework 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 8 with DataBufferUtils

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

the class DefaultPartHttpMessageReaderTests method noEndBoundary.

@ParameterizedDefaultPartHttpMessageReaderTest
public void noEndBoundary(String displayName, DefaultPartHttpMessageReader reader) {
    MockServerHttpRequest request = createRequest(new ClassPathResource("no-end-boundary.multipart", getClass()), "boundary");
    Flux<Part> result = reader.read(fromClass(Part.class), request, emptyMap());
    StepVerifier.create(result).consumeNextWith(part -> {
        assertThat(part.headers().getFirst("Header")).isEqualTo("Value");
        part.content().subscribe(DataBufferUtils::release);
    }).expectError(DecodingException.class).verify();
}
Also used : DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) MockServerHttpRequest(cn.taketoday.http.server.reactive.MockServerHttpRequest) DecodingException(cn.taketoday.core.codec.DecodingException) ClassPathResource(cn.taketoday.core.io.ClassPathResource)

Example 9 with DataBufferUtils

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

the class PartEventHttpMessageReaderTests method exceedHeaderLimit.

@Test
public void exceedHeaderLimit() {
    Flux<DataBuffer> body = DataBufferUtils.readByteChannel((new ClassPathResource("files.multipart", getClass()))::readableChannel, bufferFactory, 282);
    MediaType contentType = new MediaType("multipart", "form-data", Collections.singletonMap("boundary", "----WebKitFormBoundaryG8fJ50opQOML0oGD"));
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(contentType).body(body);
    this.reader.setMaxHeadersSize(230);
    Flux<PartEvent> result = this.reader.read(fromClass(PartEvent.class), request, emptyMap());
    StepVerifier.create(result).assertNext(data(headersFile("file2", "a.txt"), DataBufferUtils::release, true)).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) MediaType(cn.taketoday.http.MediaType) ClassPathResource(cn.taketoday.core.io.ClassPathResource) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with DataBufferUtils

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

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