Created
October 21, 2015 14:54
-
-
Save luismoramedina/ee1af18cbddfa0109545 to your computer and use it in GitHub Desktop.
Override the url in a spring RestTemplate intercepting the request
This file contains hidden or 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
/** | |
* @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