Search in sources :

Example 11 with DataBufferUtils

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

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

the class PartEventHttpMessageReaderTests method safari.

@Test
public void safari() {
    MockServerHttpRequest request = createRequest(new ClassPathResource("safari.multipart", getClass()), "----WebKitFormBoundaryG8fJ50opQOML0oGD");
    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 13 with DataBufferUtils

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

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

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

the class Jackson2JsonEncoderTests method encodeAsStreamWithCustomStreamingType.

// SPR-15727
@Test
public void encodeAsStreamWithCustomStreamingType() {
    MediaType fooMediaType = new MediaType("application", "foo");
    MediaType barMediaType = new MediaType("application", "bar");
    this.encoder.setStreamingMediaTypes(Arrays.asList(fooMediaType, barMediaType));
    Flux<Pojo> input = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    testEncode(input, ResolvableType.fromClass(Pojo.class), step -> step.consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n").andThen(DataBufferUtils::release)).consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n").andThen(DataBufferUtils::release)).consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n").andThen(DataBufferUtils::release)).verifyComplete(), barMediaType, null);
}
Also used : Pojo(cn.taketoday.http.codec.Pojo) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) MediaType(cn.taketoday.http.MediaType) 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