@ Override public void doFilter ( ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException , ServletException { final ServletRequest r1 = request ; chain . doFilter ( request , new HttpServletResponseWrapper ( ( HttpServletResponse ) response ) { @ SuppressWarnings ( "unchecked" ) public void setHeader ( String name , String value ) { ArrayList < String > headersToRemove = new ArrayList < String > ( ) ; if ( r1 . getAttribute ( "com.groupon.odo.removeHeaders" ) != null ) headersToRemove = ( ArrayList < String > ) r1 . getAttribute ( "com.groupon.odo.removeHeaders" ) ; boolean removeHeader = false ; // need to loop through removeHeaders to make things case insensitive for ( String headerToRemove : headersToRemove ) { if ( headerToRemove . toLowerCase ( ) . equals ( name . toLowerCase ( ) ) ) { removeHeader = true ; break ; } } if ( ! removeHeader ) { super . setHeader ( name , value ) ; } } } ) ; }
This looks at the servlet attributes to get the list of response headers to remove while the response object gets created by the servlet