Skip to content

Instantly share code, notes, and snippets.

@luismoramedina
Created October 21, 2015 14:54
Show Gist options
  • Save luismoramedina/ee1af18cbddfa0109545 to your computer and use it in GitHub Desktop.
Save luismoramedina/ee1af18cbddfa0109545 to your computer and use it in GitHub Desktop.
Override the url in a spring RestTemplate intercepting the request
/**
* @author luismoramedina
*/
public class UrlOverriderInterceptor implements ClientHttpRequestInterceptor {
private final String urlBase;
public UrlOverriderInterceptor(String urlBase) {
this.urlBase = urlBase;
}
private static Logger LOGGER = AppLoggerFactory.getLogger(UrlOverriderInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
URI uri = request.getURI();
LOGGER.warn("overriding {0}", uri);
return execution.execute(new MyHttpRequestWrapper(request), body);
}
private class MyHttpRequestWrapper extends HttpRequestWrapper {
public MyHttpRequestWrapper(HttpRequest request) {
super(request);
}
@Override
public URI getURI() {
try {
return new URI(UrlUtils.composeUrl(urlBase, super.getURI().toString()));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment