From c1cb0311aa495e3b2cc9b3ec326c8db0b0768e10 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 27 Mar 2018 00:25:05 +0200 Subject: [PATCH] Avoid triggering lazy resolution in MultipartResolver.cleanupMultipart Issue: SPR-16640 (cherry picked from commit 10cb2cc) --- .../commons/CommonsMultipartResolver.java | 6 +++-- .../AbstractMultipartHttpServletRequest.java | 14 ++++++++++- .../StandardMultipartHttpServletRequest.java | 8 +++++-- .../StandardServletMultipartResolver.java | 24 +++++++++++-------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java b/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java index f4226d5afe..1598bcb0e7 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartResolver.java +++ b/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"); * 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.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest; import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest; import org.springframework.web.util.WebUtils; @@ -189,7 +190,8 @@ public class CommonsMultipartResolver extends CommonsFileUploadSupport @Override public void cleanupMultipart(MultipartHttpServletRequest request) { - if (request != null) { + if (!(request instanceof AbstractMultipartHttpServletRequest) || + ((AbstractMultipartHttpServletRequest) request).isResolved()) { try { cleanupFileItems(request.getMultiFileMap()); } diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java index 4e423e4496..595a9ec112 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/AbstractMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -106,6 +106,18 @@ public abstract class AbstractMultipartHttpServletRequest extends HttpServletReq 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. diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java index 968512a468..2392275ecb 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -48,6 +48,7 @@ import org.springframework.web.multipart.MultipartFile; * @author Juergen Hoeller * @author Rossen Stoyanchev * @since 3.1 + * @see StandardServletMultipartResolver */ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { @@ -79,8 +80,11 @@ public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpSe * @param lazyParsing whether multipart parsing should be triggered lazily on * first access of multipart files or parameters * @throws MultipartException if an immediate parsing attempt failed + * @since 3.2.9 */ - public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing) throws MultipartException { + public StandardMultipartHttpServletRequest(HttpServletRequest request, boolean lazyParsing) + throws MultipartException { + super(request); if (!lazyParsing) { parseRequest(request); diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java index 15e9cc48eb..ae221b7940 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.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"); * you may not use this file except in compliance with the License. @@ -59,6 +59,7 @@ public class StandardServletMultipartResolver implements MultipartResolver { * corresponding exceptions at the time of the {@link #resolveMultipart} call. * Switch this to "true" for lazy multipart parsing, throwing parse exceptions * once the application attempts to obtain multipart files or parameters. + * @since 3.2.9 */ public void setResolveLazily(boolean resolveLazily) { this.resolveLazily = resolveLazily; @@ -82,17 +83,20 @@ public class StandardServletMultipartResolver implements MultipartResolver { @Override public void cleanupMultipart(MultipartHttpServletRequest request) { - // To be on the safe side: explicitly delete the parts, - // but only actual file parts (for Resin compatibility) - try { - for (Part part : request.getParts()) { - if (request.getFile(part.getName()) != null) { - part.delete(); + if (!(request instanceof AbstractMultipartHttpServletRequest) || + ((AbstractMultipartHttpServletRequest) request).isResolved()) { + // To be on the safe side: explicitly delete the parts, + // but only actual file parts (for Resin compatibility) + try { + for (Part part : request.getParts()) { + if (request.getFile(part.getName()) != null) { + part.delete(); + } } } - } - catch (Throwable ex) { - LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); + catch (Throwable ex) { + LogFactory.getLog(getClass()).warn("Failed to perform cleanup of multipart items", ex); + } } }