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());
}
}
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();
}
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();
}
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();
}
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);
}
Aggregations