Making a request parameter binding case insensitive
Spring has a LinkedCaseInsensitiveMap
Which you could use to do case insensitive lookups.
An implementation could look like the following.
package biz.deinum.web.filter;
import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
/**
* Wrapper for an {@link HttpServletRequest} to make the lookup of parameters case insensitive. The functionality
* is achieved by using the {@link LinkedCaseInsensitiveMap} from Spring.
*
* @author Marten Deinum
*/
public class CaseInsensitiveRequestFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
filterChain.doFilter(new CaseInsensitiveHttpServletRequestWrapper(request), response);
}
private static class CaseInsensitiveHttpServletRequestWrapper extends HttpServletRequestWrapper {
private final LinkedCaseInsensitiveMap<String[]> params = new LinkedCaseInsensitiveMap<>();
/**
* Constructs a request object wrapping the given request.
*
* @param request
* @throws IllegalArgumentException if the request is null
*/
private CaseInsensitiveHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
params.putAll(request.getParameterMap());
}
@Override
public String getParameter(String name) {
String[] values = getParameterValues(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
@Override
public Map<String, String[]> getParameterMap() {
return Collections.unmodifiableMap(this.params);
}
@Override
public Enumeration<String> getParameterNames() {
return Collections.enumeration(this.params.keySet());
}
@Override
public String[] getParameterValues(String name) {
return (String[])params.get(name);
}
}
}
You could write a servlet filter that does this. But it does need some coding work.
Here is the link to the code - http://www.acooke.org/cute/Forcinglow0.html
Something like this - in this servlet filter convert parameters to lower case
public final class LowerCaseParametersFilter implements Filter {
@Override
public void doFilter(final ServletRequest request,
final ServletResponse response,
final FilterChain chain)
throws IOException, ServletException {
if (request instanceof HttpServletRequest) {
LOG.debug("Wrapping request");
chain.doFilter(new LowerCaseRequest((HttpServletRequest) request),
response);
} else {
LOG.warn(format("Not wrapping request: %s", request.getClass()));
chain.doFilter(request, response);
}
}
}
Here is the xml config - u wuld need
<bean id="delegatingFilter"
class="org.springframework.web.filter.DelegatingFilterProxy"
p:targetBeanName="lowerParams"/>
<bean id="lowerParams"
class="com.isti.bss.mvc.LowerCaseParametersFilter"/>
I did some research and found this Case-insensitive query string request paramters
public class HttpCustomParamFilter implements Filter
{
private static class HttpServletRequestCustomeWrapper extends HttpServletRequestWrapper
{
private String[] parameterValues;
@Override
public String[] getParameterValues(String name)
{
Map<String, String[]> localParameterMap = super.getParameterMap();
// Handle case insensitivity of http request paramters like start, count, query, sort, filter etc.
if (localParameterMap != null && !localParameterMap.isEmpty())
{
parameterValues = new String[localParameterMap.size()];
for (String key : localParameterMap.keySet())
{
if (name.equalsIgnoreCase(key))
parameterValues = localParameterMap.get(key);
else
parameterValues = null;
}
}
return parameterValues;
}