Merge branch '5.3.x'

# Conflicts:
#	build.gradle
This commit is contained in:
Juergen Hoeller
2022-01-12 16:38:19 +01:00
6 changed files with 105 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -167,7 +167,7 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod() : null);
Object body = NO_VALUE;
EmptyBodyCheckingHttpInputMessage message;
EmptyBodyCheckingHttpInputMessage message = null;
try {
message = new EmptyBodyCheckingHttpInputMessage(inputMessage);
@@ -194,6 +194,11 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
catch (IOException ex) {
throw new HttpMessageNotReadableException("I/O error while reading input message", ex, inputMessage);
}
finally {
if (message != null && message.hasBody()) {
closeStreamIfNecessary(message.getBody());
}
}
if (body == NO_VALUE) {
if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
@@ -296,6 +301,15 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements
return arg;
}
/**
* Allow for closing the body stream if necessary,
* e.g. for part streams in a multipart request.
*/
void closeStreamIfNecessary(InputStream body) {
// No-op by default: A standard HttpInputMessage exposes the HTTP request stream
// (ServletRequest#getInputStream), with its lifecycle managed by the container.
}
private static class EmptyBodyCheckingHttpInputMessage implements HttpInputMessage {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
@@ -180,4 +182,17 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
return partName;
}
@Override
void closeStreamIfNecessary(InputStream body) {
// RequestPartServletServerHttpRequest exposes individual part streams,
// potentially from temporary files -> explicit close call after resolution
// in order to prevent file descriptor leaks.
try {
body.close();
}
catch (IOException ex) {
// ignore
}
}
}