Created
June 1, 2012 22:04
-
-
Save lucascs/2855418 to your computer and use it in GitHub Desktop.
Filter for removing blank id parameters
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.io.IOException; | |
import java.util.Collections; | |
import java.util.Enumeration; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import javax.servlet.Filter; | |
import javax.servlet.FilterChain; | |
import javax.servlet.FilterConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletRequest; | |
import javax.servlet.ServletResponse; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletRequestWrapper; | |
import com.google.common.base.Predicate; | |
import com.google.common.base.Strings; | |
import com.google.common.collect.Maps; | |
public class ParametersFilter implements Filter{ | |
public void destroy() { | |
} | |
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, | |
ServletException { | |
chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) req) { | |
@Override | |
public Map<String, String[]> getParameterMap() { | |
return Maps.filterEntries(super.getParameterMap(), new Predicate<Entry<String,String[]>>() { | |
@Override | |
public boolean apply(Entry<String, String[]> entry) { | |
return !entry.getKey().endsWith(".id") | |
|| !Strings.isNullOrEmpty(entry.getValue()[0]); | |
} | |
}); | |
} | |
@Override | |
public Enumeration<String> getParameterNames() { | |
return Collections.enumeration(getParameterMap().keySet()); | |
} | |
}, res); | |
} | |
public void init(FilterConfig arg0) throws ServletException { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment