I have a filter that authenticates/authorizes REST calls. This filters needs to access the request parameters so I have written a custom HTTPServletRequestWrapper for this.
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class WrapperRequest extends HttpServletRequestWrapper {
private Map<String, String[]> requestParams = null;
public WrapperRequest(final ServletRequest request) {
super((HttpServletRequest) request);
}
@Override
public String getParameter(final String name) {
if (getParameterMap().get(name) != null) {
return getParameterMap().get(name)[0];
} else {
getParameterMap().get(name)[0] = super.getParameter(name);
requestParams.put(name, getParameterMap().get(name));
return requestParams.get(name)[0];
}
}
@Override
public Map<String, String[]> getParameterMap() {
if (requestParams == null) {
requestParams = new HashMap<String, String[]>();
requestParams.putAll(super.getParameterMap());
}
return Collections.unmodifiableMap(requestParams);
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(getParameterMap().keySet());
}
@Override
public String[] getParameterValues(final String name) {
return getParameterMap().get(name);
}
}
In my filter doFilter method:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
final WrapperRequest wrappedRequest = new WrapperRequest(request);
Map<String, String[]> paramMap = wrappedRequest.getParameterMap();
chain.doFilter(wrappedRequest, response);
But I am getting the below warning
WARNING: A servlet request, to the URI , contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
I am deploying this in Tomcat. Help!