Last active
August 28, 2017 17:07
-
-
Save maciejmalycha/c4a00bfd75329287dbd8 to your computer and use it in GitHub Desktop.
Mutable HttpServletRequest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice