mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
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
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+24
@@ -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<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
+12
-7
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
+34
-9
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user