Skip to content

Instantly share code, notes, and snippets.

@chkal
Last active May 16, 2020 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chkal/8d87c04255eed7159a503835c973f542 to your computer and use it in GitHub Desktop.
Save chkal/8d87c04255eed7159a503835c973f542 to your computer and use it in GitHub Desktop.
Workaround for relative location headers via sendRedirect() for Wildfly and other containers
@WebFilter(urlPatterns = "/*")
public class RelativeRedirectsFilter implements Filter {
@Override
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
throws IOException, ServletException {
chain.doFilter( request, new RelativeRedirectResponse( (HttpServletResponse) response ) );
}
private static class RelativeRedirectResponse extends HttpServletResponseWrapper {
public RelativeRedirectResponse( HttpServletResponse response ) {
super( response );
}
@Override
public void sendRedirect( String location ) throws IOException {
if( location.startsWith( "/" ) ) {
setStatus( 302 );
setHeader( "Location", location );
}
else {
super.sendRedirect( location );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment