Author: jafl Date: Mon Jan 31 20:43:25 2011 New Revision: 1065764 URL: http://svn.apache.org/viewvc?rev=1065764&view=rev Log: WW-3490 clean up file uploads when action throws exception
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java?rev=1065764&r1=1065763&r2=1065764&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java Mon Jan 31 20:43:25 2011 @@ -80,6 +80,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.Enumeration; /** * A utility class the actual dispatcher delegates most of its tasks to. Each instance @@ -715,6 +716,40 @@ public class Dispatcher { } /** + * Removes all the files created by MultiPartRequestWrapper. + * + * @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 { + 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()) { + LOG.warn("Resource Leaking: Could not remove uploaded file '" + currentFile.getCanonicalPath() + "'."); + } + } + } + } + } + + /** * Send an HTTP error response code. * * @param request the HttpServletRequest object. Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java?rev=1065764&r1=1065763&r2=1065764&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/FilterDispatcher.java Mon Jan 31 20:43:25 2011 @@ -421,6 +421,7 @@ public class FilterDispatcher implements dispatcher.serviceAction(request, response, servletContext, mapping); } finally { + dispatcher.cleanUpRequest(request); try { ActionContextCleanUp.cleanUp(req); } finally { Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java?rev=1065764&r1=1065763&r2=1065764&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/FileUploadInterceptor.java Mon Jan 31 20:43:25 2011 @@ -311,28 +311,7 @@ public class FileUploadInterceptor exten } // invoke action - String result = invocation.invoke(); - - // cleanup - 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()) { - LOG.info(getTextMessage(action, "struts.messages.removing.file", new Object[]{inputValue, currentFile}, ac.getLocale())); - } - - if ((currentFile != null) && currentFile.isFile()) { - if (!currentFile.delete()) { - LOG.warn("Resource Leaking: Could not remove uploaded file '" + currentFile.getCanonicalPath() + "'."); - } - } - } - } - - return result; + return invocation.invoke(); } /**