WW-4572 Fixes issue with file upload

This commit is contained in:
Lukasz Lenart
2016-10-07 10:05:38 +02:00
parent 7cd3d30fdc
commit 69da41eb79
8 changed files with 39 additions and 22 deletions
@@ -203,7 +203,9 @@ public class ParametersInterceptor extends MethodFilterInterceptor {
String name = entry.getKey();
Parameter value = entry.getValue();
try {
if (value.isMultiple()) {
if (value instanceof Parameter.File) {
newStack.setParameter(name, value.getObject());
} else if (value.isMultiple()) {
newStack.setParameter(name, value.getMultipleValues());
} else {
newStack.setParameter(name, value.getValue());
@@ -53,10 +53,6 @@ public class HttpParameters implements Cloneable {
return parameters.containsKey(name);
}
public HttpParameters clone(Map<String, ?> newParams) {
return HttpParameters.createEmpty().withParent(this).withExtraParams(newParams).build();
}
public Map<String, String[]> toMap() {
Map<String, String[]> result = new HashMap<>(parameters.size());
for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
@@ -65,6 +61,11 @@ public class HttpParameters implements Cloneable {
return result;
}
public HttpParameters appendAll(Map<String, Parameter> newParams) {
parameters.putAll(newParams);
return this;
}
public static class Builder {
private Map<String, Object> requestParameterMap;
private HttpParameters parent;
@@ -3,6 +3,8 @@ package org.apache.struts2.dispatcher;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
public interface Parameter {
String getName();
@@ -82,6 +84,12 @@ public interface Parameter {
}
}
class File extends Request {
public File(String name, Object value) {
super(name, value);
}
}
class EmptyHttpParameter implements Parameter {
private String name;
@@ -61,7 +61,7 @@ public class CheckboxInterceptor extends AbstractInterceptor {
public String intercept(ActionInvocation ai) throws Exception {
HttpParameters parameters = ai.getInvocationContext().getParameters();
Map<String, String[]> extraParams = new HashMap<>();
Map<String, Parameter> extraParams = new HashMap<>();
for (String name : parameters.getNames()) {
if (name.startsWith("__checkbox_")) {
@@ -77,13 +77,13 @@ public class CheckboxInterceptor extends AbstractInterceptor {
// is this checkbox checked/submitted?
if (!parameters.contains(checkboxName)) {
// if not, let's be sure to default the value to false
extraParams.put(checkboxName, new String[]{ uncheckedValue });
extraParams.put(checkboxName, new Parameter.Request(checkboxName, uncheckedValue));
}
}
}
ai.getInvocationContext().setParameters(parameters.clone(extraParams));
ai.getInvocationContext().getParameters().appendAll(extraParams);
return ai.invoke();
}
@@ -96,7 +96,7 @@ public class DateTextFieldInterceptor implements Interceptor {
}
// Create all the date objects
Map<String, Object> newParams = new HashMap<>();
Map<String, Parameter> newParams = new HashMap<>();
Set<Entry<String, Map<String, String>>> dateEntries = dates.entrySet();
for (Entry<String, Map<String, String>> dateEntry : dateEntries) {
Set<Entry<String, String>> dateFormatEntries = dateEntry.getValue().entrySet();
@@ -110,13 +110,13 @@ public class DateTextFieldInterceptor implements Interceptor {
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
formatter.setLenient(false);
Date value = formatter.parse(dateValue);
newParams.put(dateEntry.getKey(), value);
newParams.put(dateEntry.getKey(), new Parameter.Request(dateEntry.getKey(), value));
} catch (ParseException e) {
LOG.warn("Cannot parse the parameter '{}' with format '{}' and with value '{}'", dateEntry.getKey(), dateFormat, dateValue);
}
}
ai.getInvocationContext().setParameters(parameters.clone(newParams));
ai.getInvocationContext().getParameters().appendAll(newParams);
return ai.invoke();
}
@@ -30,6 +30,7 @@ import com.opensymphony.xwork2.util.TextParseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.Parameter;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.util.ContentTypeMatcher;
@@ -294,11 +295,11 @@ public class FileUploadInterceptor extends AbstractInterceptor {
}
if (!acceptedFiles.isEmpty()) {
Map<String, Object> newParams = new HashMap<>();
newParams.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));
newParams.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]));
newParams.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]));
ac.setParameters(ac.getParameters().clone(newParams));
Map<String, Parameter> newParams = new HashMap<>();
newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()])));
newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])));
newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()])));
ac.getParameters().appendAll(newParams);
}
}
} else {
@@ -23,6 +23,7 @@ package org.apache.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;
import java.util.HashMap;
import java.util.Map;
@@ -47,7 +48,7 @@ public class MultiselectInterceptor extends AbstractInterceptor {
*/
public String intercept(ActionInvocation ai) throws Exception {
HttpParameters parameters = ai.getInvocationContext().getParameters();
Map<String, Object> newParams = new HashMap<>();
Map<String, Parameter> newParams = new HashMap<>();
for (String name : parameters.getNames()) {
if (name.startsWith("__multiselect_")) {
@@ -56,14 +57,14 @@ public class MultiselectInterceptor extends AbstractInterceptor {
// is this multi-select box submitted?
if (!parameters.contains(key)) {
// if not, let's be sure to default the value to an empty string array
newParams.put(key, new String[0]);
newParams.put(key, new Parameter.Request(key, new String[0]));
}
parameters = parameters.remove(name);
}
}
ai.getInvocationContext().setParameters(parameters.clone(newParams));
ai.getInvocationContext().getParameters().appendAll(newParams);
return ai.invoke();
}
@@ -22,12 +22,14 @@ package org.apache.struts2;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.Parameter;
import org.apache.struts2.views.util.UrlHelper;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import java.util.HashMap;
import java.util.Map;
/**
@@ -54,12 +56,14 @@ public abstract class JSPRuntime {
int i = location.indexOf("?");
if (i > 0) {
//extract params from the url and add them to the request
HttpParameters parameters = ActionContext.getContext().getParameters();
String query = location.substring(i + 1);
Map<String, Object> queryParams = urlHelper.parseQueryString(query, true);
if (queryParams != null && !queryParams.isEmpty()) {
parameters = parameters.clone(queryParams);
ActionContext.getContext().setParameters(parameters);
Map<String, Parameter> newParams = new HashMap<>();
for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
newParams.put(entry.getKey(), new Parameter.Request(entry.getKey(), entry.getValue()));
}
ActionContext.getContext().getParameters().appendAll(newParams);
}
location = location.substring(0, i);
}