Browse Source

Avoid triggering lazy resolution in MultipartResolver.cleanupMultipart

Issue: SPR-16640
pull/1755/head
Juergen Hoeller 8 years ago
parent
commit
10cb2ccaef
  1. 16
      spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java
  2. 14
      spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java
  3. 8
      spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java
  4. 22
      spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest; import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
@ -188,11 +189,14 @@ public class CommonsMultipartResolver extends CommonsFileUploadSupport
@Override @Override
public void cleanupMultipart(MultipartHttpServletRequest request) { public void cleanupMultipart(MultipartHttpServletRequest request) {
try { if (!(request instanceof AbstractMultipartHttpServletRequest) ||
cleanupFileItems(request.getMultiFileMap()); ((AbstractMultipartHttpServletRequest) request).isResolved()) {
} try {
catch (Throwable ex) { cleanupFileItems(request.getMultiFileMap());
logger.warn("Failed to perform multipart cleanup for servlet request", ex); }
catch (Throwable ex) {
logger.warn("Failed to perform multipart cleanup for servlet request", ex);
}
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -108,6 +108,18 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq
return getMultipartFiles(); return getMultipartFiles();
} }
/**
* Determine whether the underlying multipart request has been resolved.
* @return {@code true} when eagerly initialized or lazily triggered,
* {@code false} in case of a lazy-resolution request that got aborted
* before any parameters or multipart files have been accessed
* @since 4.3.15
* @see #getMultipartFiles()
*/
public boolean isResolved() {
return (this.multipartFiles != null);
}
/** /**
* Set a Map with parameter names as keys and list of MultipartFile objects as values. * Set a Map with parameter names as keys and list of MultipartFile objects as values.

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,6 +52,7 @@ import org.springframework.web.multipart.MultipartFile;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.1 * @since 3.1
* @see StandardServletMultipartResolver
*/ */
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
@ -75,9 +76,10 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe
* @param lazyParsing whether multipart parsing should be triggered lazily on * @param lazyParsing whether multipart parsing should be triggered lazily on
* first access of multipart files or parameters * first access of multipart files or parameters
* @throws MultipartException if an immediate parsing attempt failed * @throws MultipartException if an immediate parsing attempt failed
* @since 3.2.9
*/ */
public StandardMultipartHttpServletRequest(HttpServletRequest request, public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing)
boolean lazyParsing) throws MultipartException { throws MultipartException {
super(request); super(request);
if (!lazyParsing) { if (!lazyParsing) {

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

@ -71,6 +71,7 @@ public class StandardServletMultipartResolver implements MultipartResolver {
* corresponding exceptions at the time of the {@link #resolveMultipart} call. * corresponding exceptions at the time of the {@link #resolveMultipart} call.
* Switch this to "true" for lazy multipart parsing, throwing parse exceptions * Switch this to "true" for lazy multipart parsing, throwing parse exceptions
* once the application attempts to obtain multipart files or parameters. * once the application attempts to obtain multipart files or parameters.
* @since 3.2.9
*/ */
public void setResolveLazily(boolean resolveLazily) { public void setResolveLazily(boolean resolveLazily) {
this.resolveLazily = resolveLazily; this.resolveLazily = resolveLazily;
@ -94,17 +95,20 @@ public class StandardServletMultipartResolver implements MultipartResolver {
@Override @Override
public void cleanupMultipart(MultipartHttpServletRequest request) { public void cleanupMultipart(MultipartHttpServletRequest request) {
// To be on the safe side: explicitly delete the parts, if (!(request instanceof AbstractMultipartHttpServletRequest) ||
// but only actual file parts (for Resin compatibility) ((AbstractMultipartHttpServletRequest) request).isResolved()) {
try { // To be on the safe side: explicitly delete the parts,
for (Part part : request.getParts()) { // but only actual file parts (for Resin compatibility)
if (request.getFile(part.getName()) != null) { try {
part.delete(); for (Part part : request.getParts()) {
if (request.getFile(part.getName()) != null) {
part.delete();
}
} }
} }
} catch (Throwable ex) {
catch (Throwable ex) { LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex);
LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); }
} }
} }

Loading…
Cancel
Save