Skip to content

Commit

Permalink
WW-4572 Fixes issue with file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Oct 7, 2016
1 parent 7cd3d30 commit 69da41e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ protected void setParameters(final Object action, ValueStack stack, HttpParamete
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ public boolean contains(String name) {
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()) {
Expand All @@ -65,6 +61,11 @@ public Map<String, String[]> toMap() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;

public interface Parameter {

String getName();
Expand Down Expand Up @@ -82,6 +84,12 @@ public Object getObject() {
}
}

class File extends Request {
public File(String name, Object value) {
super(name, value);
}
}

class EmptyHttpParameter implements Parameter {

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_")) {
Expand All @@ -77,13 +77,13 @@ public String intercept(ActionInvocation ai) throws Exception {
// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String intercept(ActionInvocation ai) throws Exception {
}

// 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();
Expand All @@ -110,13 +110,13 @@ public String intercept(ActionInvocation ai) throws Exception {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;

Expand Down Expand Up @@ -294,11 +295,11 @@ public String intercept(ActionInvocation invocation) throws Exception {
}

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -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_")) {
Expand All @@ -56,14 +57,14 @@ public String intercept(ActionInvocation ai) throws Exception {
// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

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;

/**
Expand All @@ -54,12 +56,14 @@ public static void handle(String location, boolean flush) throws Exception {
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);
}
Expand Down

0 comments on commit 69da41e

Please sign in to comment.