I am using a javax.servlet.http.HttpServletRequest to implement a web application.
I have no problem to get the parameter of a request using the getParamete
The missing getParameterMap override ended up being a real problem for me. So this is what I ended up with:
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/***
* Request wrapper enabling the update of a request-parameter.
*
* @author E.K. de Lang
*
*/
final class HttpServletRequestReplaceParameterWrapper
extends HttpServletRequestWrapper
{
private final Map keyValues;
@SuppressWarnings("unchecked")
HttpServletRequestReplaceParameterWrapper(HttpServletRequest request, String key, String value)
{
super(request);
keyValues = new HashMap();
keyValues.putAll(request.getParameterMap());
// Can override the values in the request
keyValues.put(key, new String[] { value });
}
@SuppressWarnings("unchecked")
HttpServletRequestReplaceParameterWrapper(HttpServletRequest request, Map additionalRequestParameters)
{
super(request);
keyValues = new HashMap();
keyValues.putAll(request.getParameterMap());
for (Map.Entry entry : additionalRequestParameters.entrySet()) {
keyValues.put(entry.getKey(), new String[] { entry.getValue() });
}
}
@Override
public String getParameter(String name)
{
if (keyValues.containsKey(name)) {
String[] strings = keyValues.get(name);
if (strings == null || strings.length == 0) {
return null;
}
else {
return strings[0];
}
}
else {
// Just in case the request has some tricks of it's own.
return super.getParameter(name);
}
}
@Override
public String[] getParameterValues(String name)
{
String[] value = this.keyValues.get(name);
if (value == null) {
// Just in case the request has some tricks of it's own.
return super.getParameterValues(name);
}
else {
return value;
}
}
@Override
public Map getParameterMap()
{
return this.keyValues;
}
}