Skip to content

Instantly share code, notes, and snippets.

@barrypitman
Created January 25, 2013 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barrypitman/4637786 to your computer and use it in GitHub Desktop.
Save barrypitman/4637786 to your computer and use it in GitHub Desktop.
Allow Turbolinks to update the browser address bar correctly after an ajax request is redirected
/**
* Provide the correct response URL to turbolinks in the 'X-XHR-Current-Location' header. Used to update the browser
* history after an ajax request is redirected.
*
* @author barry
* @since 2013/01/24 11:46 AM
*/
public class TurboLinksUrlFilter implements Filter {
/**
* turbolinks checks for this header by default
*/
private static final String RESPONSE_HEADER = "X-XHR-Current-Location";
private static final String REQUEST_HEADER = "X-XHR-Referer";
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletResponse response = (HttpServletResponse) resp;
HttpServletRequest request = (HttpServletRequest) req;
if (!response.isCommitted() && request.getHeader(REQUEST_HEADER) != null) {
response.setHeader(RESPONSE_HEADER, getFullURL(request));
}
chain.doFilter(req, resp);
}
public void destroy() {
}
private String getFullURL(HttpServletRequest request) {
StringBuffer url = request.getRequestURL();
if (request.getQueryString() != null) {
url.append('?');
url.append(request.getQueryString());
}
return url.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment