Created
May 15, 2012 04:57
-
-
Save vvasabi/2699233 to your computer and use it in GitHub Desktop.
A request wrapper that allows modification to parameter map
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Enumeration; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletRequestWrapper; | |
public class CustomRequestWrapper extends HttpServletRequestWrapper { | |
private HttpServletRequest wrapped; | |
private Map<String, String[]> parameterMap; | |
public CustomRequestWrapper(HttpServletRequest wrapped) { | |
super(wrapped); | |
this.wrapped = wrapped; | |
} | |
public void addParameter(String name, String value) { | |
if (parameterMap == null) { | |
parameterMap = new HashMap<String, String[]>(); | |
parameterMap.putAll(wrapped.getParameterMap()); | |
} | |
String[] values = parameterMap.get(name); | |
if (values == null) { | |
values = new String[0]; | |
} | |
List<String> list = new ArrayList<String>(values.length + 1); | |
list.addAll(Arrays.asList(values)); | |
list.add(value); | |
parameterMap.put(name, list.toArray(new String[0])); | |
} | |
@Override | |
public String getParameter(String name) { | |
if (parameterMap == null) { | |
return wrapped.getParameter(name); | |
} | |
String[] strings = parameterMap.get(name); | |
if (strings != null) { | |
return strings[0]; | |
} | |
return null; | |
} | |
@Override | |
public Map<String, String[]> getParameterMap() { | |
if (parameterMap == null) { | |
return wrapped.getParameterMap(); | |
} | |
return Collections.unmodifiableMap(parameterMap); | |
} | |
@Override | |
public Enumeration<String> getParameterNames() { | |
if (parameterMap == null) { | |
return wrapped.getParameterNames(); | |
} | |
return Collections.enumeration(parameterMap.keySet()); | |
} | |
@Override | |
public String[] getParameterValues(String name) { | |
if (parameterMap == null) { | |
return wrapped.getParameterValues(name); | |
} | |
return parameterMap.get(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola, como utilizo esta clase? How I can use this class?