Skip to content

Commit c1cb031

Browse files
committed
Avoid triggering lazy resolution in MultipartResolver.cleanupMultipart
Issue: SPR-16640 (cherry picked from commit 10cb2cc)
1 parent f2478cf commit c1cb031

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.web.multipart.MultipartException;
3434
import org.springframework.web.multipart.MultipartHttpServletRequest;
3535
import org.springframework.web.multipart.MultipartResolver;
36+
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
3637
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
3738
import org.springframework.web.util.WebUtils;
3839

@@ -189,7 +190,8 @@ protected String determineEncoding(HttpServletRequest request) {
189190

190191
@Override
191192
public void cleanupMultipart(MultipartHttpServletRequest request) {
192-
if (request != null) {
193+
if (!(request instanceof AbstractMultipartHttpServletRequest) ||
194+
((AbstractMultipartHttpServletRequest) request).isResolved()) {
193195
try {
194196
cleanupFileItems(request.getMultiFileMap());
195197
}

spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -106,6 +106,18 @@ public MultiValueMap<String, MultipartFile> getMultiFileMap() {
106106
return getMultipartFiles();
107107
}
108108

109+
/**
110+
* Determine whether the underlying multipart request has been resolved.
111+
* @return {@code true} when eagerly initialized or lazily triggered,
112+
* {@code false} in case of a lazy-resolution request that got aborted
113+
* before any parameters or multipart files have been accessed
114+
* @since 4.3.15
115+
* @see #getMultipartFiles()
116+
*/
117+
public boolean isResolved() {
118+
return (this.multipartFiles != null);
119+
}
120+
109121

110122
/**
111123
* Set a Map with parameter names as keys and list of MultipartFile objects as values.

spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
* @author Juergen Hoeller
4949
* @author Rossen Stoyanchev
5050
* @since 3.1
51+
* @see StandardServletMultipartResolver
5152
*/
5253
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
5354

@@ -79,8 +80,11 @@ public StandardMultipartHttpServletRequest(HttpServletRequest request) throws Mu
7980
* @param lazyParsing whether multipart parsing should be triggered lazily on
8081
* first access of multipart files or parameters
8182
* @throws MultipartException if an immediate parsing attempt failed
83+
* @since 3.2.9
8284
*/
83-
public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing) throws MultipartException {
85+
public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing)
86+
throws MultipartException {
87+
8488
super(request);
8589
if (!lazyParsing) {
8690
parseRequest(request);

spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,7 @@ public class StandardServletMultipartResolver implements MultipartResolver {
5959
* corresponding exceptions at the time of the {@link #resolveMultipart} call.
6060
* Switch this to "true" for lazy multipart parsing, throwing parse exceptions
6161
* once the application attempts to obtain multipart files or parameters.
62+
* @since 3.2.9
6263
*/
6364
public void setResolveLazily(boolean resolveLazily) {
6465
this.resolveLazily = resolveLazily;
@@ -82,17 +83,20 @@ public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request)
8283

8384
@Override
8485
public void cleanupMultipart(MultipartHttpServletRequest request) {
85-
// To be on the safe side: explicitly delete the parts,
86-
// but only actual file parts (for Resin compatibility)
87-
try {
88-
for (Part part : request.getParts()) {
89-
if (request.getFile(part.getName()) != null) {
90-
part.delete();
86+
if (!(request instanceof AbstractMultipartHttpServletRequest) ||
87+
((AbstractMultipartHttpServletRequest) request).isResolved()) {
88+
// To be on the safe side: explicitly delete the parts,
89+
// but only actual file parts (for Resin compatibility)
90+
try {
91+
for (Part part : request.getParts()) {
92+
if (request.getFile(part.getName()) != null) {
93+
part.delete();
94+
}
9195
}
9296
}
93-
}
94-
catch (Throwable ex) {
95-
LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
97+
catch (Throwable ex) {
98+
LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
99+
}
96100
}
97101
}
98102

0 commit comments

Comments
 (0)