Skip to content

Commit

Permalink
Decode urls before storing in the state
Browse files Browse the repository at this point in the history
  • Loading branch information
gillarramendi committed Feb 14, 2014
1 parent 7a2cd3b commit b3ec18e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.hdiv.dataComposer;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -371,13 +372,14 @@ protected IParameter composeParameter(String parameterName, String value, boolea
parameter.addValue(decodedValue);
} else {
// create a new parameter and add to the request
parameter = createParameter(parameterName, decodedValue, editable, editableDataType, isActionParam, charEncoding);
parameter = createParameter(parameterName, decodedValue, editable, editableDataType, isActionParam,
charEncoding);
state.addParameter(parameter);
}

return parameter;
}

/**
* Instantiates the parameter
*
Expand Down Expand Up @@ -457,7 +459,7 @@ private String getDecodedValue(String value, String charEncoding) {
String decodedValue = null;
try {
decodedValue = URLDecoder.decode(value, charEncoding);
} catch (Exception e) {
} catch (UnsupportedEncodingException e) {
decodedValue = value;
}

Expand Down Expand Up @@ -498,6 +500,11 @@ public String beginRequest() {
*/
public String beginRequest(String action) {

try {
action = URLDecoder.decode(action, Constants.ENCODING_UTF_8);
} catch (UnsupportedEncodingException e) {
}

// Create new IState
IState state = new State(this.requestCounter);
state.setAction(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,39 @@ public void testSaveStateInCreation() {

dataComposer.endPage();
}

public void testEncodeFormAction() {

// No encoded url
HttpServletRequest request = HDIVUtil.getHttpServletRequest();
IDataComposer dataComposer = this.dataComposerFactory.newInstance(request);
HDIVUtil.setDataComposer(dataComposer, request);

dataComposer.startPage();
dataComposer.beginRequest("test test.do");
String stateId = dataComposer.endRequest();
dataComposer.endPage();

assertNotNull(stateId);

IState state = this.stateUtil.restoreState(stateId);

assertEquals("test test.do", state.getAction());

// Encoded action url
dataComposer = this.dataComposerFactory.newInstance(request);
HDIVUtil.setDataComposer(dataComposer, request);

dataComposer.startPage();
dataComposer.beginRequest("test%20test.do");
stateId = dataComposer.endRequest();
dataComposer.endPage();

assertNotNull(stateId);

state = this.stateUtil.restoreState(stateId);

// State action value is decoded because we store decoded values only
assertEquals("test test.do", state.getAction());
}
}
15 changes: 15 additions & 0 deletions hdiv-core/src/test/java/org/hdiv/filter/ValidatorHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,19 @@ public void testValidateWhitespace() {
RequestWrapper requestWrapper = new RequestWrapper(request);
assertTrue(helper.validate(requestWrapper).isValid());
}

public void testValidateEncoded() {

MockHttpServletRequest request = (MockHttpServletRequest) HDIVUtil.getHttpServletRequest();

this.dataComposer.beginRequest("/path/test%20Action.do");
String pageState = this.dataComposer.endRequest();
this.dataComposer.endPage();

request.setRequestURI("/path/test%20Action.do");
request.addParameter(hdivParameter, pageState);

RequestWrapper requestWrapper = new RequestWrapper(request);
assertTrue(helper.validate(requestWrapper).isValid());
}
}

0 comments on commit b3ec18e

Please sign in to comment.