Skip to content

Commit

Permalink
Default ServerWebExchange::cleanupMultipart implementation
Browse files Browse the repository at this point in the history
This commit provided a default implementation for ServerWebExchange::cleanupMultipart.

See gh-30590
  • Loading branch information
poutsma committed Jun 8, 2023
1 parent 7a4ed38 commit ce5189a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
Expand Up @@ -146,7 +146,15 @@ default <T> T getAttributeOrDefault(String name, T defaultValue) {
* @since 6.0.10
* @see Part#delete()
*/
Mono<Void> cleanupMultipart();
default Mono<Void> cleanupMultipart() {
return getMultipartData()
.onErrorResume(t -> Mono.empty()) // ignore errors reading multipart data
.flatMapIterable(Map::values)
.flatMapIterable(Function.identity())
.flatMap(part -> part.delete()
.onErrorResume(ex -> Mono.empty()))
.then();
}

/**
* Return the {@link LocaleContext} using the configured
Expand Down
Expand Up @@ -324,30 +324,28 @@ private static class DelegatingServerWebExchange implements ServerWebExchange {

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

private volatile boolean multipartRead = false;


DelegatingServerWebExchange(ServerHttpRequest request, Map<String, Object> attributes,
ServerWebExchange delegate, List<HttpMessageReader<?>> messageReaders) {

this.request = request;
this.attributes = attributes;
this.delegate = delegate;
this.formDataMono = initFormData(messageReaders);
this.formDataMono = initFormData(request, messageReaders);
this.multipartDataMono = initMultipartData(request, messageReaders);
}

@SuppressWarnings("unchecked")
private Mono<MultiValueMap<String, String>> initFormData(List<HttpMessageReader<?>> readers) {
private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request,
List<HttpMessageReader<?>> readers) {

try {
MediaType contentType = this.request.getHeaders().getContentType();
MediaType contentType = request.getHeaders().getContentType();
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
return ((HttpMessageReader<MultiValueMap<String, String>>) readers.stream()
.filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED))
.findFirst()
.orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader.")))
.readMono(FORM_DATA_TYPE, this.request, Hints.none())
.doOnNext(ignored -> this.multipartRead = true)
.readMono(FORM_DATA_TYPE, request, Hints.none())
.switchIfEmpty(EMPTY_FORM_DATA)
.cache();
}
Expand Down Expand Up @@ -400,23 +398,7 @@ public Mono<MultiValueMap<String, Part>> getMultipartData() {
return this.multipartDataMono;
}

@Override
public Mono<Void> cleanupMultipart() {
if (this.multipartRead) {
return getMultipartData()
.onErrorResume(t -> Mono.empty()) // ignore errors reading multipart data
.flatMapIterable(Map::values)
.flatMapIterable(Function.identity())
.flatMap(part -> part.delete()
.onErrorResume(ex -> Mono.empty()))
.then();
}
else {
return Mono.empty();
}
}

// Delegating methods
// Delegating methods

@Override
public ServerHttpResponse getResponse() {
Expand Down

0 comments on commit ce5189a

Please sign in to comment.