Created
November 26, 2019 23:23
-
-
Save cpilsworth/b8beb66cb44b2cade595e799703e1ebf to your computer and use it in GitHub Desktop.
intercept redirects and rewrite
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 org.apache.sling.api.SlingHttpServletResponse; | |
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper; | |
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.HttpServletResponse; | |
import java.io.IOException; | |
public class RedirectFixFilter implements Filter { | |
private static final String PREFIX = "https://www.domain.com"; | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | |
throws IOException, ServletException { | |
final HttpServletResponse response = new RedirectHttpServletResponse((SlingHttpServletResponse) servletResponse); | |
filterChain.doFilter(servletRequest, response); | |
} | |
public class RedirectHttpServletResponse extends SlingHttpServletResponseWrapper { | |
public RedirectHttpServletResponse(SlingHttpServletResponse wrappedResponse) { | |
super(wrappedResponse); | |
} | |
@Override | |
public void sendRedirect(final String location) throws IOException { | |
if (location != null && location.startsWith(PREFIX)) { | |
super.sendRedirect(location.substring(PREFIX.length())); | |
} else { | |
super.sendRedirect(location); | |
} | |
} | |
} | |
@Override | |
public void init(FilterConfig filterConfig) throws ServletException { | |
} | |
@Override | |
public void destroy() { | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment