From 0f15f15bf33422ab205bb024d2d9edb03bac6b73 Mon Sep 17 00:00:00 2001 From: Tino Mulanchira Thomas <38363530+tinomthomas@users.noreply.github.com> Date: Mon, 28 May 2018 01:48:42 +0300 Subject: [PATCH] BAEL-1756 (#4349) --- .../org/baeldung/config/RestClientConfig.java | 8 ++-- ...RestTemplateHeaderModifierInterceptor.java | 18 +++++++++ .../RestTemplateLoggingInterceptor.java | 39 ------------------- 3 files changed, 21 insertions(+), 44 deletions(-) create mode 100644 spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateHeaderModifierInterceptor.java delete mode 100644 spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java diff --git a/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java index 3619f43f8a..8743af20fe 100644 --- a/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java +++ b/spring-rest/src/main/java/org/baeldung/config/RestClientConfig.java @@ -3,12 +3,10 @@ package org.baeldung.config; import java.util.ArrayList; import java.util.List; -import org.baeldung.interceptors.RestTemplateLoggingInterceptor; +import org.baeldung.interceptors.RestTemplateHeaderModifierInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.util.CollectionUtils; import org.springframework.web.client.RestTemplate; @@ -17,13 +15,13 @@ public class RestClientConfig { @Bean public RestTemplate restTemplate() { - RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); + RestTemplate restTemplate = new RestTemplate(); List interceptors = restTemplate.getInterceptors(); if (CollectionUtils.isEmpty(interceptors)) { interceptors = new ArrayList(); } - interceptors.add(new RestTemplateLoggingInterceptor()); + interceptors.add(new RestTemplateHeaderModifierInterceptor()); restTemplate.setInterceptors(interceptors); return restTemplate; } diff --git a/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateHeaderModifierInterceptor.java b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateHeaderModifierInterceptor.java new file mode 100644 index 0000000000..fabb904634 --- /dev/null +++ b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateHeaderModifierInterceptor.java @@ -0,0 +1,18 @@ +package org.baeldung.interceptors; + +import java.io.IOException; + +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; + +public class RestTemplateHeaderModifierInterceptor implements ClientHttpRequestInterceptor { + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + ClientHttpResponse response = execution.execute(request, body); + response.getHeaders().add("Foo", "bar"); + return response; + } +} diff --git a/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java b/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java deleted file mode 100644 index 03a9cdc806..0000000000 --- a/spring-rest/src/main/java/org/baeldung/interceptors/RestTemplateLoggingInterceptor.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.baeldung.interceptors; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.util.StreamUtils; - -public class RestTemplateLoggingInterceptor implements ClientHttpRequestInterceptor { - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - logRequest(body); - ClientHttpResponse response = execution.execute(request, body); - logResponse(response); - response.getHeaders().add("Foo", "bar"); - return response; - } - - private void logRequest(byte[] body) { - if (body.length > 0) { - String payLoad = new String(body, StandardCharsets.UTF_8); - System.out.println(payLoad); - } - } - - private void logResponse(ClientHttpResponse response) throws IOException { - long contentLength = response.getHeaders() - .getContentLength(); - if (contentLength != 0) { - String payLoad = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); - System.out.println(payLoad); - } - } -}