mirror of
https://github.com/apache/struts.git
synced 2026-08-31 11:24:28 +00:00
Adds support to parse collection of parameters
This commit is contained in:
@@ -23,6 +23,8 @@ package org.apache.struts2.dispatcher;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
|
||||
@@ -195,31 +197,64 @@ public abstract class StrutsResultSupport implements Result, StrutsStatics {
|
||||
*/
|
||||
protected String conditionalParse(String param, ActionInvocation invocation) {
|
||||
if (parse && param != null && invocation != null) {
|
||||
return TextParseUtil.translateVariables(param, invocation.getStack(),
|
||||
new TextParseUtil.ParsedValueEvaluator() {
|
||||
public Object evaluate(String parsedValue) {
|
||||
if (encode) {
|
||||
if (parsedValue != null) {
|
||||
try {
|
||||
// use UTF-8 as this is the recommended encoding by W3C to
|
||||
// avoid incompatibilities.
|
||||
return URLEncoder.encode(parsedValue, "UTF-8");
|
||||
}
|
||||
catch(UnsupportedEncodingException e) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn("error while trying to encode ["+parsedValue+"]", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return parsedValue;
|
||||
}
|
||||
});
|
||||
return TextParseUtil.translateVariables(
|
||||
param,
|
||||
invocation.getStack(),
|
||||
new EncodingParsedValueEvaluator());
|
||||
} else {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #conditionalParse(String, ActionInvocation)} but does not
|
||||
* convert found object into String. If found object is a collection it is
|
||||
* returned if found object is not a collection it is wrapped in one.
|
||||
*
|
||||
* @param param
|
||||
* @param invocation
|
||||
* @param excludeEmptyElements
|
||||
* @return
|
||||
*/
|
||||
protected Collection<String> conditionalParseCollection(String param, ActionInvocation invocation, boolean excludeEmptyElements) {
|
||||
if (parse && param != null && invocation != null) {
|
||||
return TextParseUtil.translateVariablesCollection(
|
||||
param,
|
||||
invocation.getStack(),
|
||||
excludeEmptyElements,
|
||||
new EncodingParsedValueEvaluator());
|
||||
} else {
|
||||
Collection<String> collection = new ArrayList<String>(1);
|
||||
collection.add(param);
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link com.opensymphony.xwork2.util.TextParseUtil.ParsedValueEvaluator} to do URL encoding for found values. To be
|
||||
* used for single strings or collections.
|
||||
*
|
||||
*/
|
||||
private final class EncodingParsedValueEvaluator implements TextParseUtil.ParsedValueEvaluator {
|
||||
public Object evaluate(String parsedValue) {
|
||||
if (encode) {
|
||||
if (parsedValue != null) {
|
||||
try {
|
||||
// use UTF-8 as this is the recommended encoding by W3C to
|
||||
// avoid incompatibilities.
|
||||
return URLEncoder.encode(parsedValue, "UTF-8");
|
||||
}
|
||||
catch(UnsupportedEncodingException e) {
|
||||
if (LOG.isWarnEnabled()) {
|
||||
LOG.warn("error while trying to encode ["+parsedValue+"]", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return parsedValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the result given a final location (jsp page, action, etc) and the action invocation
|
||||
* (the state in which the action was executed). Subclasses must implement this class to handle
|
||||
|
||||
@@ -21,6 +21,9 @@
|
||||
|
||||
package org.apache.struts2.dispatcher;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.easymock.EasyMock;
|
||||
|
||||
@@ -109,6 +112,34 @@ public class StrutsResultSupportTest extends StrutsInternalTestCase {
|
||||
EasyMock.verify(mockActionInvocation);
|
||||
}
|
||||
|
||||
public void testConditionalParseCollection() throws Exception {
|
||||
ValueStack stack = ActionContext.getContext().getValueStack();
|
||||
stack.push(new ActionSupport() {
|
||||
public List<String> getList() {
|
||||
return new ArrayList<String>(){{
|
||||
add("val 1");
|
||||
add("val 2");
|
||||
}};
|
||||
}
|
||||
});
|
||||
|
||||
ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
|
||||
mockActionInvocation.getStack();
|
||||
EasyMock.expectLastCall().andReturn(stack);
|
||||
EasyMock.replay(mockActionInvocation);
|
||||
|
||||
InternalStrutsResultSupport result = new InternalStrutsResultSupport();
|
||||
result.setParse(true);
|
||||
result.setEncode(true);
|
||||
|
||||
Collection<String> collection = result.conditionalParseCollection("${list}", mockActionInvocation, true);
|
||||
|
||||
assertNotNull(collection);
|
||||
assertEquals(2, collection.size());
|
||||
assertTrue(collection.contains("val+1"));
|
||||
assertTrue(collection.contains("val+2"));
|
||||
EasyMock.verify(mockActionInvocation);
|
||||
}
|
||||
|
||||
public static class InternalStrutsResultSupport extends StrutsResultSupport {
|
||||
private String _internalLocation = null;
|
||||
|
||||
Reference in New Issue
Block a user