Since the solutions proposed by Bojo are not entirely satisfactory for my needs, I wrote a filter that does exactly what I want. Not sure if problems may arise in future cases, but until then feel free to use my implementation:
@Service public class RedirectFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String queryString = ((HttpServletRequest) request).getQueryString(); if (queryString != null) { RedirectAwareResponseWrapper res = new RedirectAwareResponseWrapper((HttpServletResponse) response); chain.doFilter(request, res); if (res.isRedirected()) { ((HttpServletResponse) response).sendRedirect(res.getLocation() + "?" + queryString); } } else { chain.doFilter(request, response); } } @Override public void destroy() { } class RedirectAwareResponseWrapper extends HttpServletResponseWrapper { private boolean redirected = false; private String location; public RedirectAwareResponseWrapper(HttpServletResponse response) { super(response); } @Override public void sendRedirect(String location) throws IOException { redirected = true; this.location = location;
source share