Skip to content

Instantly share code, notes, and snippets.

@maciejmalycha
Last active August 28, 2017 17:07
Show Gist options
  • Save maciejmalycha/c4a00bfd75329287dbd8 to your computer and use it in GitHub Desktop.
Save maciejmalycha/c4a00bfd75329287dbd8 to your computer and use it in GitHub Desktop.
Mutable HttpServletRequest
public class MutableHttpServletRequest extends HttpServletRequestWrapper {
private Map<String,String[]> parameters = new HashMap<String,String[]>();
public MutableHttpServletRequest(HttpServletRequest request) {
super(request);
}
public void setParameter(String name, String value) {
parameters.put(name, new String[] {value});
}
@Override
public String getParameter(String name) {
if (parameters.get(name) != null) {
return parameters.get(name)[0];
}
HttpServletRequest req = (HttpServletRequest) super.getRequest();
return req.getParameter(name);
}
public Map<String, String[]> getParameterMap() {
Map<String, String[]> result = new HashMap<String, String[]>();
result.putAll(super.getRequest().getParameterMap());
result.putAll(parameters);
return Collections.<String, String[]>unmodifiableMap(result);
}
public Enumeration<String> getParameterNames() {
Set<String> result = new HashSet<String>(Collections.list(super.getRequest().getAttributeNames()));
result.addAll(parameters.keySet());
return new Vector<String>(result).elements();
}
public String[] getParameterValues(String name) {
if (parameters.get(name) != null) {
return parameters.get(name);
}
HttpServletRequest req = (HttpServletRequest) super.getRequest();
return req.getParameterValues(name);
}
}
@valterh4ck3r
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment