Skip to content

Commit

Permalink
Adds support to parse collection of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed Mar 22, 2014
1 parent c03962c commit a4a7edd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.StrutsStatics;

Expand Down Expand Up @@ -195,31 +197,64 @@ public void execute(ActionInvocation invocation) throws Exception {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -109,6 +112,34 @@ public String getMyLocation() {
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;
Expand Down

0 comments on commit a4a7edd

Please sign in to comment.