Support multiple MultipartFile's for 1 request param

Issue: SWF-1422
This commit is contained in:
Rossen Stoyanchev
2014-03-30 16:57:16 -04:00
parent e6d5bfb268
commit 9dffe65dee
5 changed files with 62 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2012 the original author or authors.
* Copyright 2004-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.webflow.context.portlet;
import java.util.Iterator;
import java.util.List;
import javax.portlet.PortletRequest;
@@ -29,7 +30,7 @@ import org.springframework.webflow.core.collection.CollectionUtils;
/**
* Map backed by the Portlet request parameter map for accessing request parameters. Also provides support for
* multi-part requests, providing transparent access to the request "fileMap" as a request parameter entry.
*
*
* @author Keith Donald
* @author Scott Andrews
*/
@@ -51,9 +52,13 @@ public class PortletRequestParameterMap extends StringKeyedMapAdapter<Object> {
protected Object getAttribute(String key) {
if (request instanceof MultipartActionRequest) {
MultipartActionRequest multipartRequest = (MultipartActionRequest) request;
MultipartFile data = multipartRequest.getFileMap().get(key);
if (data != null) {
return data;
List<MultipartFile> data = multipartRequest.getMultiFileMap().get(key);
if (data != null && data.size() > 0) {
if (data.size() == 1) {
return data.get(0);
} else {
return data;
}
}
}
String[] parameters = request.getParameterValues(key);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2012 the original author or authors.
* Copyright 2004-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,19 +16,21 @@
package org.springframework.webflow.context.servlet;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.binding.collection.StringKeyedMapAdapter;
import org.springframework.util.Assert;
import org.springframework.util.CompositeIterator;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.webflow.core.collection.CollectionUtils;
/**
* Map backed by the Servlet HTTP request parameter map for accessing request parameters. Also provides support for
* multi-part requests, providing transparent access to the request "fileMap" as a request parameter entry.
*
*
* @author Keith Donald
*/
public class HttpServletRequestParameterMap extends StringKeyedMapAdapter<Object> {
@@ -49,9 +51,13 @@ public class HttpServletRequestParameterMap extends StringKeyedMapAdapter<Object
protected Object getAttribute(String key) {
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Object data = multipartRequest.getFileMap().get(key);
if (data != null) {
return data;
List<MultipartFile> data = multipartRequest.getMultiFileMap().get(key);
if (data != null && data.size() > 0) {
if (data.size() == 1) {
return data.get(0);
} else {
return data;
}
}
}
String[] parameters = request.getParameterValues(key);
@@ -84,7 +90,6 @@ public class HttpServletRequestParameterMap extends StringKeyedMapAdapter<Object
}
}
@SuppressWarnings("unchecked")
private Iterator<String> getRequestParameterNames() {
return CollectionUtils.toIterator(request.getParameterNames());
}

View File

@@ -19,6 +19,7 @@ import java.io.StringWriter;
import java.io.Writer;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import org.springframework.binding.collection.SharedMapDecorator;
@@ -337,6 +338,16 @@ public class MockExternalContext implements ExternalContext {
getMockRequestParameterMap().put(parameterName, parameterValue);
}
/**
* Puts a multi-valued MultipartFile request parameter into the mock parameter map.
*
* @param parameterName the parameter name
* @param parameterValue the parameter value
*/
public void putRequestParameter(String parameterName, List<MultipartFile> parameterValue) {
getMockRequestParameterMap().put(parameterName, parameterValue);
}
/**
* Sets the id of the event that should be signaled by this context. For use when resuming a flow. This method
* depends on a MockViewFactory being configured for parsing the event id on a resume operation.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2012 the original author or authors.
* Copyright 2004-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.webflow.test;
import java.util.HashMap;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.webflow.core.collection.LocalParameterMap;
@@ -23,11 +24,12 @@ import org.springframework.webflow.core.collection.ParameterMap;
/**
* A extension of parameter map that allows for mutation of parameters. Useful as a stub for testing.
*
*
* @see ParameterMap
*
*
* @author Keith Donald
*/
@SuppressWarnings("serial")
public class MockParameterMap extends LocalParameterMap {
/**
@@ -70,4 +72,15 @@ public class MockParameterMap extends LocalParameterMap {
return this;
}
/**
* Add a new multi-valued multi-part file parameter to this map.
* @param parameterName the parameter name
* @param parameterValues the parameter values
* @return this, to support call chaining
*/
public MockParameterMap put(String parameterName, List<MultipartFile> parameterValues) {
getMapInternal().put(parameterName, parameterValues);
return this;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2012 the original author or authors.
* Copyright 2004-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.webflow.test;
import java.util.List;
import org.springframework.binding.message.DefaultMessageContext;
import org.springframework.binding.message.MessageContext;
import org.springframework.web.multipart.MultipartFile;
@@ -38,10 +40,10 @@ import org.springframework.webflow.execution.View;
/**
* Mock implementation of the <code>RequestContext</code> interface to facilitate standalone flow artifact (e.g. action)
* unit tests.
*
*
* @see org.springframework.webflow.execution.RequestContext
* @see org.springframework.webflow.execution.Action
*
*
* @author Keith Donald
* @author Erwin Vervaet
*/
@@ -299,6 +301,15 @@ public class MockRequestContext implements RequestContext {
getMockExternalContext().putRequestParameter(parameterName, parameterValue);
}
/**
* Puts a multi-valued MultipartFile request parameter into the mock parameter map.
* @param parameterName the parameter name
* @param parameterValue the parameter value
*/
public void putRequestParameter(String parameterName, List<MultipartFile> parameterValue) {
getMockExternalContext().putRequestParameter(parameterName, parameterValue);
}
// convenience accessors
/**