Author: lukaszlenart
Date: Tue May 15 14:10:27 2012
New Revision: 1338715

URL: http://svn.apache.org/viewvc?rev=1338715&view=rev
Log:
WW-3802 moves clean up code to implementation of MultiPartRequest interface
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/multipart/JakartaMultiPartRequest.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java
    
struts/struts2/trunk/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.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=1338715&r1=1338714&r2=1338715&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
 Tue May 15 14:10:27 2012
@@ -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();
     }
 
     /**

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java?rev=1338715&r1=1338714&r2=1338715&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java
 Tue May 15 14:10:27 2012
@@ -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 imp
         }
 
         values.add(item);
+        item.delete();
         files.put(item.getFieldName(), values);
     }
 
@@ -336,4 +340,24 @@ public class JakartaMultiPartRequest imp
         };
     }
 
+    /* (non-Javadoc)
+    * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
+    */
+    public void cleanUp() {
+        Set<String> names = files.keySet();
+        for (String name : names) {
+            List<FileItem> 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();
+                }
+            }
+        }
+    }
+
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java?rev=1338715&r1=1338714&r2=1338715&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java
 Tue May 15 14:10:27 2012
@@ -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. <p>
@@ -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();
+
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java?rev=1338715&r1=1338714&r2=1338715&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java
 Tue May 15 14:10:27 2012
@@ -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 ext
 
         return temp.elements();
     }
+
+    public void cleanUp() {
+        if (multi != null) {
+            multi.cleanUp();
+        }
+    }
+
 }

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java?rev=1338715&r1=1338714&r2=1338715&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/ng/PrepareOperations.java
 Tue May 15 14:10:27 2012
@@ -26,7 +26,6 @@ import com.opensymphony.xwork2.util.Valu
 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);

Modified: 
struts/struts2/trunk/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java?rev=1338715&r1=1338714&r2=1338715&view=diff
==============================================================================
--- 
struts/struts2/trunk/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
 (original)
+++ 
struts/struts2/trunk/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java
 Tue May 15 14:10:27 2012
@@ -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 implem
             }
         } 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());
+                        }
+                    }
+                }
+            }
+        }
+    }
+
 }


Reply via email to