From bfa7df79b669e42d03131e1c43fc9cac2799b409 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 15 May 2012 14:10:27 +0000 Subject: [PATCH] WW-3802 moves clean up code to implementation of MultiPartRequest interface git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1338715 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/struts2/dispatcher/Dispatcher.java | 27 +----------- .../multipart/JakartaMultiPartRequest.java | 24 +++++++++++ .../multipart/MultiPartRequest.java | 9 +++- .../multipart/MultiPartRequestWrapper.java | 19 +++++--- .../dispatcher/ng/PrepareOperations.java | 6 --- .../multipart/PellMultiPartRequest.java | 43 +++++++++++++++---- 6 files changed, 79 insertions(+), 49 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index 58ccc4e29..5b9c523da 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -74,7 +74,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -762,35 +761,13 @@ public class Dispatcher { * * @param request the HttpServletRequest object. * @see org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper - * @throws java.io.IOException on any error. */ - public void cleanUpRequest(HttpServletRequest request) throws IOException { + public void cleanUpRequest(HttpServletRequest request) { if (!(request instanceof MultiPartRequestWrapper)) { return; } - MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request; - - Enumeration fileParameterNames = multiWrapper.getFileParameterNames(); - while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { - String inputValue = (String) fileParameterNames.nextElement(); - File[] files = multiWrapper.getFiles(inputValue); - - for (File currentFile : files) { - if (LOG.isInfoEnabled()) { - String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file", Locale.ENGLISH, "no.message.found", new Object[]{inputValue, currentFile}); - LOG.info(msg); - } - - if ((currentFile != null) && currentFile.isFile()) { - if (!currentFile.delete()) { - if (LOG.isWarnEnabled()) { - LOG.warn("Resource Leaking: Could not remove uploaded file '" + currentFile.getCanonicalPath() + "'."); - } - } - } - } - } + multiWrapper.cleanUp(); } /** diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java index 45935694b..93c4e9341 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java @@ -22,6 +22,7 @@ package org.apache.struts2.dispatcher.multipart; import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.LocalizedTextUtil; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.commons.fileupload.FileItem; @@ -42,7 +43,9 @@ import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Set; /** * Multipart form data request adapter for Jakarta Commons Fileupload package. @@ -118,6 +121,7 @@ public class JakartaMultiPartRequest implements MultiPartRequest { } values.add(item); + item.delete(); files.put(item.getFieldName(), values); } @@ -336,4 +340,24 @@ public class JakartaMultiPartRequest implements MultiPartRequest { }; } + /* (non-Javadoc) + * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp() + */ + public void cleanUp() { + Set names = files.keySet(); + for (String name : names) { + List items = files.get(name); + for (FileItem item : items) { + if (LOG.isInfoEnabled()) { + String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file", + Locale.ENGLISH, "no.message.found", new Object[]{name, item}); + LOG.info(msg); + } + if (!item.isInMemory()) { + item.delete(); + } + } + } + } + } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java index 36a1b03f9..4b215b306 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java @@ -21,13 +21,12 @@ package org.apache.struts2.dispatcher.multipart; +import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.List; -import javax.servlet.http.HttpServletRequest; - /** * Abstract wrapper class HTTP requests to handle multi-part data.

@@ -115,4 +114,10 @@ public interface MultiPartRequest { * @return a list of Strings that represent various errors during parsing */ public List getErrors(); + + /** + * Cleans up all uploaded file, should be called at the end of request + */ + public void cleanUp(); + } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java index 1c4989e8a..3fd91d895 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java @@ -21,6 +21,11 @@ package org.apache.struts2.dispatcher.multipart; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.dispatcher.StrutsRequestWrapper; + +import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -30,13 +35,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Vector; -import javax.servlet.http.HttpServletRequest; - -import org.apache.struts2.dispatcher.StrutsRequestWrapper; - -import com.opensymphony.xwork2.util.logging.Logger; -import com.opensymphony.xwork2.util.logging.LoggerFactory; - /** * Parse a multipart request and provide a wrapper around the request. The parsing implementation used @@ -245,4 +243,11 @@ public class MultiPartRequestWrapper extends StrutsRequestWrapper { return temp.elements(); } + + public void cleanUp() { + if (multi != null) { + multi.cleanUp(); + } + } + } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java b/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java index a2d5c4454..c22599da3 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java @@ -26,7 +26,6 @@ import com.opensymphony.xwork2.util.ValueStackFactory; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.struts2.RequestUtils; -import org.apache.struts2.StrutsConstants; import org.apache.struts2.StrutsException; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.mapper.ActionMapper; @@ -102,11 +101,6 @@ public class PrepareOperations { // always clean up the thread request, even if an action hasn't been executed try { dispatcher.cleanUpRequest(request); - } catch (IOException e) { - if (LOG.isWarnEnabled()) { - LOG.warn("Cannot clean up the request, some files can still remain in #0 after upload!", e, - StrutsConstants.STRUTS_MULTIPART_SAVEDIR); - } } finally { ActionContext.setContext(null); Dispatcher.setInstance(null); diff --git a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java index 3241aa877..f1b379529 100644 --- a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java +++ b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java @@ -21,8 +21,14 @@ package org.apache.struts2.dispatcher.multipart; +import com.opensymphony.xwork2.inject.Inject; +import com.opensymphony.xwork2.util.LocalizedTextUtil; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; import http.utils.multipartrequest.ServletMultipartRequest; +import org.apache.struts2.StrutsConstants; +import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -30,14 +36,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.List; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.struts2.StrutsConstants; - -import com.opensymphony.xwork2.inject.Inject; -import com.opensymphony.xwork2.util.logging.Logger; -import com.opensymphony.xwork2.util.logging.LoggerFactory; +import java.util.Locale; /** @@ -159,10 +158,36 @@ public class PellMultiPartRequest implements MultiPartRequest { } } catch (IllegalArgumentException e) { if (LOG.isInfoEnabled()) { - LOG.info("Could not get encoding property 'struts.i18n.encoding' for file upload. Using system default"); + LOG.info("Could not get encoding property 'struts.i18n.encoding' for file upload. Using system default"); } } catch (UnsupportedEncodingException e) { LOG.error("Encoding " + encoding + " is not a valid encoding. Please check your struts.properties file."); } } + + /* (non-Javadoc) + * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp() + */ + public void cleanUp() { + Enumeration fileParameterNames = multi.getFileParameterNames(); + while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { + String inputValue = (String) fileParameterNames.nextElement(); + File[] files = getFile(inputValue); + for (File currentFile : files) { + if (LOG.isInfoEnabled()) { + String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file", Locale.ENGLISH, + "no.message.found", new Object[]{inputValue, currentFile}); + LOG.info(msg); + } + if ((currentFile != null) && currentFile.isFile()) { + if (!currentFile.delete()) { + if (LOG.isWarnEnabled()) { + LOG.warn("Resource Leaking: Could not remove uploaded file [#0]", currentFile.getAbsolutePath()); + } + } + } + } + } + } + }