import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletContext; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import javax.servlet.http.HttpServletRequestWrapper; public class FailSafeResponseFilter implements Filter { FilterConfig filterConfig; ServletContext servletContext; String contextPath; @Override public void init(FilterConfig filterConfig) { this.filterConfig = filterConfig; this.servletContext = filterConfig.getServletContext(); contextPath = servletContext.getContextPath(); } @Override public void destroy() { } @Override public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; chain.doFilter( new FailSafeRequestWrapper(httpRequest), new FailSafeResponseWrapper(httpResponse) ); } } class FailSafeRequestWrapper extends HttpServletRequestWrapper { HttpServletRequest request; String contextPath; String serverName; public FailSafeRequestWrapper(HttpServletRequest request) { super(request); this.request = request; this.contextPath = request.getContextPath(); this.serverName = request.getServerName(); System.out.println(this + " wrapping " + request); } @Override public String getContextPath() { String result = contextPath; try { result = request.getContextPath(); } catch (Exception e) { System.out.println(e); } System.out.println("FailSafe getContextPath " + result); return result; } @Override public String getServerName() { String result = serverName; try { result = request.getServerName(); } catch (Exception e) { System.out.println(e); } System.out.println("FailSafe getServerName " + result); return result; } } class FailSafeResponseWrapper extends HttpServletResponseWrapper { HttpServletResponse response; public FailSafeResponseWrapper(HttpServletResponse response) { super(response); this.response = response; System.out.println(this + " wrapping " + response); } @Override public String encodeRedirectUrl(String url) { String result = url; try { result = response.encodeRedirectURL(url); } catch (Exception e) { System.out.println(e); } return result; } @Override public String encodeUrl(String url) { String result = url; try { result = response.encodeURL(url); } catch (Exception e) { System.out.println(e); } return result; } @Override public String encodeRedirectURL(String url) { System.out.println("FailSafe encodeRedirectURL " + url); String result = url; try { result = response.encodeRedirectURL(url); } catch (Exception e) { System.out.println(e); } return result; } @Override public String encodeURL(String url) { System.out.println("FailSafe encodeURL " + url); String result = url; try { result = response.encodeURL(url); } catch (Exception e) { System.out.println(e); } return result; } }