Skip to content

Commit ce5189a

Browse files
committed
Default ServerWebExchange::cleanupMultipart implementation
This commit provided a default implementation for ServerWebExchange::cleanupMultipart. See gh-30590
1 parent 7a4ed38 commit ce5189a

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

spring-web/src/main/java/org/springframework/web/server/ServerWebExchange.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,15 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
146146
* @since 6.0.10
147147
* @see Part#delete()
148148
*/
149-
Mono<Void> cleanupMultipart();
149+
default Mono<Void> cleanupMultipart() {
150+
return getMultipartData()
151+
.onErrorResume(t -> Mono.empty()) // ignore errors reading multipart data
152+
.flatMapIterable(Map::values)
153+
.flatMapIterable(Function.identity())
154+
.flatMap(part -> part.delete()
155+
.onErrorResume(ex -> Mono.empty()))
156+
.then();
157+
}
150158

151159
/**
152160
* Return the {@link LocaleContext} using the configured

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -324,30 +324,28 @@ private static class DelegatingServerWebExchange implements ServerWebExchange {
324324

325325
private final Mono<MultiValueMap<String, Part>> multipartDataMono;
326326

327-
private volatile boolean multipartRead = false;
328-
329-
330327
DelegatingServerWebExchange(ServerHttpRequest request, Map<String, Object> attributes,
331328
ServerWebExchange delegate, List<HttpMessageReader<?>> messageReaders) {
332329

333330
this.request = request;
334331
this.attributes = attributes;
335332
this.delegate = delegate;
336-
this.formDataMono = initFormData(messageReaders);
333+
this.formDataMono = initFormData(request, messageReaders);
337334
this.multipartDataMono = initMultipartData(request, messageReaders);
338335
}
339336

340337
@SuppressWarnings("unchecked")
341-
private Mono<MultiValueMap<String, String>> initFormData(List<HttpMessageReader<?>> readers) {
338+
private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request,
339+
List<HttpMessageReader<?>> readers) {
340+
342341
try {
343-
MediaType contentType = this.request.getHeaders().getContentType();
342+
MediaType contentType = request.getHeaders().getContentType();
344343
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
345344
return ((HttpMessageReader<MultiValueMap<String, String>>) readers.stream()
346345
.filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED))
347346
.findFirst()
348347
.orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader.")))
349-
.readMono(FORM_DATA_TYPE, this.request, Hints.none())
350-
.doOnNext(ignored -> this.multipartRead = true)
348+
.readMono(FORM_DATA_TYPE, request, Hints.none())
351349
.switchIfEmpty(EMPTY_FORM_DATA)
352350
.cache();
353351
}
@@ -400,23 +398,7 @@ public Mono<MultiValueMap<String, Part>> getMultipartData() {
400398
return this.multipartDataMono;
401399
}
402400

403-
@Override
404-
public Mono<Void> cleanupMultipart() {
405-
if (this.multipartRead) {
406-
return getMultipartData()
407-
.onErrorResume(t -> Mono.empty()) // ignore errors reading multipart data
408-
.flatMapIterable(Map::values)
409-
.flatMapIterable(Function.identity())
410-
.flatMap(part -> part.delete()
411-
.onErrorResume(ex -> Mono.empty()))
412-
.then();
413-
}
414-
else {
415-
return Mono.empty();
416-
}
417-
}
418-
419-
// Delegating methods
401+
// Delegating methods
420402

421403
@Override
422404
public ServerHttpResponse getResponse() {

0 commit comments

Comments
 (0)